BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
EyeNeedHelp
Calcite | Level 5

Hello. I am trying to delete a file based on it's size.

 

I want to write a code that will look inside a folder, grab all the .txt files from it and if a .txt file is bigger than 135KB for example, I want it to be moved to a different folder.

 

This is what I currently have so far.

 

%let path=x;
filename filrf "&path.";

data _null_;
did = dopen('filrf');
memcount = dnum(did);
do while (memcount>0);
fname = dread(did,memcount);
size = round(finfo(fid,'File Size (bytes)')/1024);
if scan(lowcase(fname),2,'.')='txt'
and size > 135 then do;
rcref = filename('fref',catx('\',"&path.",fname));
rcdel = fdelete('fref');
end;
memcount+-1;
end;
stop;
run;

 

I was able to move the file to another folder using code, but I am not able to delete from first folder.

 

Maybe this is not the right way to do it, so any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

FYI - I modified your posted code into a code block and formatted it for legibility in your previous post.

 

I think you can simplify your last few steps, see below for the rough idea. You can test and post the log if you're having issues. Note that you should test each step individually, ie assigning file refs, copy, delete and saving these stats to a data set helps track that. Once it's working, then change the data step to a data _null_ step.

 

 

/*Pull list of files from folder and their last save date*/
DATA ALL_FILES_DATES (KEEP=fname modified size ext);
	LENGTH fref $8;

	IF filename(fref, "/home/fkhurshed/Demo1")=0 THEN
		DO;
			did=dopen(fref);

			IF did ne 0 THEN
				DO;
					dnum=dnum(did);

					DO i=1 TO dnum;
						fname=dread(did, i);
						fid=mopen(did, fname);

						IF fid ne 0 THEN
							DO;
								modified=finfo(fid, 'Last Modified');
								size=round(finfo(fid, 'File Size (bytes)')/1024);
								ext = scan(fname, -1, ".");
								OUTPUT;
								fid=dclose(fid);
							END;
					END;
					did=dclose(did);
				END;
			rc=filename(fref);
		END;
RUN;

PROC SQL NOPRINT;
	CREATE TABLE ALL_FILES_DATES_SIZE AS SELECT * FROM ALL_FILES_DATES WHERE  ext='txt';
QUIT;

Proc sort data=ALL_FILES_DATES_SIZE(keep=fname size);
	by fname;
run;

options msglevel=i;

data temp;
	set ALL_FILES_DATES_SIZE;
	length msg $ 384;
	i=filename('ii', "/home/fkhurshed/Demo1/" || strip(fname), "DISK", "RECFM=N");
	o=filename('oo', "/home/fkhurshed/Demo2/" || strip(fname), "DISK", "RECFM=N");
	rc_copy=fcopy('ii', 'oo');
	msg_copy=sysmsg();

	if rc_copy=0 then do;
	     put 'Copied _bcin to _bcout.';
		 rc_delete=fdelete('ii');
		 msg_delete = sysmsg();
	end;	
		
	else
		do;
			
			put rc_copy= msg_copy=;
		end;
	i=filename('ii');
	o=filename('oo');
run;

EDIT: This is tested and works for txt files. You can add in the size filter to the PROC SQL query to have it work on the sizes of interest but i didn't happen to have any files big enough so just left it as txt files. That seems to have dropped off somewhere in your first set of programs and the latest.

 

Update your paths to get this working for you accordingly and to your OS - your paths are not specified properly in your code most, FOLDER_1 should be the full file path for SAS.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

... but I am not able to delete from first folder.

 

What happens? Is there an error message in the log? If so, show us the entire log for this DATA step. What do you see that indicates you are not able to delete it?

--
Paige Miller
EyeNeedHelp
Calcite | Level 5

Looks hard to read and I am not sure how to put it in a better format. I am novice to SAS so I am trying to use code that was used for other things as well.

 

/*Pull list of files from folder and their last save date*/
DATA ALL_FILES_DATES (KEEP=fname modified size);
	LENGTH fref $8;

	IF filename(fref, "FOLDER_1")=0 THEN
		DO;
			did=dopen(fref);

			IF did ne 0 THEN
				DO;
					dnum=dnum(did);

					DO i=1 TO dnum;
						fname=dread(did, i);
						fid=mopen(did, fname);

						IF fid ne 0 THEN
							DO;
								modified=finfo(fid, 'Last Modified');
								size=round(finfo(fid, 'File Size (bytes)')/1024);
								OUTPUT;
								fid=dclose(fid);
							END;
					END;
					did=dclose(did);
				END;
			rc=filename(fref);
		END;
RUN;

PROC SQL NOPRINT;
	CREATE TABLE ALL_FILES_DATES_SIZE AS SELECT * FROM ALL_FILES_DATES WHERE 
		SIZE > 135;
QUIT;

Proc sort data=ALL_FILES_DATES_SIZE out=loop1 (keep=fname size);
	by fname;
run;

options msglevel=i;

data _null_;
	set loop1;
	length msg $ 384;
	i=filename('ii', 'FOLDER_1' || strip(fname), "DISK", "RECFM=N");
	o=filename('oo', '\\FOLDER_2' || strip(fname), "DISK", "RECFM=N");
	rc=fcopy('ii', 'oo');

	if rc=0 then
		put 'Copied _bcin to _bcout.';
	else
		do;
			msg=sysmsg();
			put rc=msg=;
		end;
	i=filename('ii');
	o=filename('oo');
run;

%let path=FOLDER_1;
filename filrf "&path.";

data _null_;
	did=dopen('filrf');
	memcount=dnum(did);

	do while (memcount>0);
		fname=dread(did, memcount);
		size=round(finfo(fid, 'File Size (bytes)')/1024);

		if scan(lowcase(fname), 2, '.')='txt' and size > 135 then
			do;
				rcref=filename('fref', catx('\', "&path.", fname));
				rcdel=fdelete('fref');
			end;
		memcount+-1;
	end;
	stop;
run;

 

Again, this might not be the right way to do it, but ultimately I want to delete all files from a folder if the file size is over 135KB.

PaigeMiller
Diamond | Level 26

This is not the log. Copy the log as text and paste it into the window that appears when you click on the </> icon

PaigeMiller_0-1663012019648.png

 

And you need to answer my questions ... is there an error in the log? What happens that makes you think the file was not deleted?

--
Paige Miller
EyeNeedHelp
Calcite | Level 5

 

       
85         %let path=FOLDER_1
85       ! ;
86         filename filrf "&path.";
87         data _null_;
88           did = dopen('filrf');
89           memcount = dnum(did);
90           do while (memcount>0);
91             fname = dread(did,memcount);
92         	size = round(finfo(fid,'File Size (bytes)')/1024);
93             if scan(lowcase(fname),2,'.')='txt'
94         	and size > 135 then do;
95                 rcref = filename('fref',catx('\',"&path.",fname));
96                 rcdel = fdelete('fref');
97             end;
98             memcount+-1;
99           end;
100          stop;
101        run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      92:15   
INFO: Character variables have defaulted to a length of 200 at the places given by: (Line):(Column). Truncation can result.
      91:5     fname
NOTE: Variable fid is uninitialized.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
4                                                          The SAS System                          18:30 Wednesday, November 9, 2022

NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
NOTE: Argument 1 to function FINFO(.,'File Size (b'[12 of 17 characters shown]) at line 92 column 15 is invalid.
did=1 memcount=0 fname=AEPOH_Demand_20221109_060717.txt size=. fid=. rcref=. rcdel=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      49 at 92:9    49 at 92:45   
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

102        
103        GOPTIONS NOACCESSIBLE;
104        %LET _CLIENTTASKLABEL=;
105        %LET _CLIENTPROJECTPATH=;
106        %LET _CLIENTPROJECTNAME=;
107        %LET _SASPROGRAMFILE=;
108        
109        ;*';*";*/;quit;run;
110        ODS _ALL_ CLOSE;
111        
112        
5                                                          The SAS System                          18:30 Wednesday, November 9, 2022

113        QUIT; RUN;
114        

 

 

Reeza
Super User

FYI - I modified your posted code into a code block and formatted it for legibility in your previous post.

 

I think you can simplify your last few steps, see below for the rough idea. You can test and post the log if you're having issues. Note that you should test each step individually, ie assigning file refs, copy, delete and saving these stats to a data set helps track that. Once it's working, then change the data step to a data _null_ step.

 

 

/*Pull list of files from folder and their last save date*/
DATA ALL_FILES_DATES (KEEP=fname modified size ext);
	LENGTH fref $8;

	IF filename(fref, "/home/fkhurshed/Demo1")=0 THEN
		DO;
			did=dopen(fref);

			IF did ne 0 THEN
				DO;
					dnum=dnum(did);

					DO i=1 TO dnum;
						fname=dread(did, i);
						fid=mopen(did, fname);

						IF fid ne 0 THEN
							DO;
								modified=finfo(fid, 'Last Modified');
								size=round(finfo(fid, 'File Size (bytes)')/1024);
								ext = scan(fname, -1, ".");
								OUTPUT;
								fid=dclose(fid);
							END;
					END;
					did=dclose(did);
				END;
			rc=filename(fref);
		END;
RUN;

PROC SQL NOPRINT;
	CREATE TABLE ALL_FILES_DATES_SIZE AS SELECT * FROM ALL_FILES_DATES WHERE  ext='txt';
QUIT;

Proc sort data=ALL_FILES_DATES_SIZE(keep=fname size);
	by fname;
run;

options msglevel=i;

data temp;
	set ALL_FILES_DATES_SIZE;
	length msg $ 384;
	i=filename('ii', "/home/fkhurshed/Demo1/" || strip(fname), "DISK", "RECFM=N");
	o=filename('oo', "/home/fkhurshed/Demo2/" || strip(fname), "DISK", "RECFM=N");
	rc_copy=fcopy('ii', 'oo');
	msg_copy=sysmsg();

	if rc_copy=0 then do;
	     put 'Copied _bcin to _bcout.';
		 rc_delete=fdelete('ii');
		 msg_delete = sysmsg();
	end;	
		
	else
		do;
			
			put rc_copy= msg_copy=;
		end;
	i=filename('ii');
	o=filename('oo');
run;

EDIT: This is tested and works for txt files. You can add in the size filter to the PROC SQL query to have it work on the sizes of interest but i didn't happen to have any files big enough so just left it as txt files. That seems to have dropped off somewhere in your first set of programs and the latest.

 

Update your paths to get this working for you accordingly and to your OS - your paths are not specified properly in your code most, FOLDER_1 should be the full file path for SAS.

EyeNeedHelp
Calcite | Level 5

Correct, I have the correct paths, just didn't put into this forum.

 

I tested the code and it looks like it moved all the txt files not just the one that is bigger than 135KB

 

 

EyeNeedHelp
Calcite | Level 5

It worked! Thank you very much everyone!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1236 views
  • 1 like
  • 4 in conversation