cancel
Showing results for 
Search instead for 
Did you mean: 

how to delete all files in data step using fdelete function

knveraraju91
Barite | Level 11

how to delete all files in data step using fdelete function

FILENAME spec "%sysfunc(pathname(fptotb,f))/have.rtf"  ;

  DATA _NULL_ ;
  rc = FDELETE('spec') ;
  RUN ;

Hi In my folder, there are rft, lst, log, 7bdat are created with same name during a data step which i need to delete. for example, the temp file are  have.rtf, have.log, have.lst, have.7bdat.  With above pgm I am able to delete only rtf.  How to remove all in step.  Please suggest. Thank you

22 REPLIES 22
yabwon
Amethyst | Level 16

Re: how to delete all files in data step using fdelete function

Hi,

 

maybe if you do know files extensions in advance you can loop:

 

data _null_;
  fname="tempfile";
  length ext $ 20;
  do ext = "rtf", "lst", "log", "sas7bdat", "txt";

    rc=filename(fname, "%sysfunc(pathname(work))/have." !! strip(ext));
    if rc = 0 and fexist(fname) then
       rc=fdelete(fname);
    rc=filename(fname);
  end;
run;

 

all the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Reeza
Super User

Re: how to delete all files in data step using fdelete function

Do you have x commands enabled? In this case, if you do, a system command will accept wildcards which would help.
ChrisNZ
Tourmaline | Level 20

Re: how to delete all files in data step using fdelete function

If you must use function fdelete(), then this works:

%* create files;
data _null_; 
 file "%sysfunc(pathname(work))/have.txt" ; put 'a';
 file "%sysfunc(pathname(work))/have.csv" ; put 'a';
run;

filename SPEC "%sysfunc(pathname(work))/have.*" ;

%* List files;
data FILENAMES;
  length _FILENAME $ 256; 
  infile SPEC filename=_FILENAME;
  input ; 
  FILENAME = _FILENAME;
run;
% Delete files;
data _null_; 
  set FILENAMES;  
  rc=filename ('TMPFREF',FILENAME); 
  rc=fdelete  ('TMPFREF') ;          
  rc=filename ('TMPFREF');           
run;

 

ChrisNZ
Tourmaline | Level 20

Re: how to delete all files in data step using fdelete function

If you needn't use function fdelete(), then this is easier:

data _null_; 
 file "%sysfunc(pathname(work))/have.txt" ; put 'a';
 file "%sysfunc(pathname(work))/have.csv" ; put 'a';
run;

filename DEL pipe "del ""%sysfunc(pathname(work))\have.*"" " ;

data FILENAMES;
  infile DEL ;
  input ; 
  put _infile_;
run;

Any error messages generated during deletion go to the log window here, feel free to keep them in a table instead.

 

Ksharp
Super User

Re: how to delete all files in data step using fdelete function

You can get the all the file name by the following code ,and write CALL EXECUTE() to delete them one by one.

 


  DATA _NULL_ ;
  rc = filename('spec','c:\temp') ;
  did=dopen('spec');
  do i=1 to dnum(did);
   file_name=dread(did,i);
   put file_name= ;
  end;
  RUN ;
Highlighted
elwayfan446
Barite | Level 11

Re: how to delete all files in data step using fdelete function

@Ksharp , I stumbled upon this post looking for an alternate way to delete files from my directory on a server than using the x command.  Normally, if I use an x command with a wildcard to delete *.xlsx files it works, however, we are running into issues where it won't work for some reason.

 

Reading your reply, I got this syntax to work to generate a list of my files in that directory.  Can you advise on how I would use the CALL EXECUTE() to delete the files now?

 

DATA _NULL_ ;
  rc = filename('spec','c:\temp') ;
  did=dopen('spec');
  do i=1 to dnum(did);
   file_name=dread(did,i);
   put file_name= ;
  end;
  RUN ;

 

Reeza
Super User

Re: how to delete all files in data step using fdelete function

Wrap that in a macro function and call it via CALL EXECUTE the same way I wrote that last program for you.
elwayfan446
Barite | Level 11

Re: how to delete all files in data step using fdelete function

What I don't understand is how the files actually get deleted. Where & how in that code do I tell it to delete each of those files?

That last program to help me cat the variables in the file name was great. Thank you for that.
Reeza
Super User

Re: how to delete all files in data step using fdelete function

Add the FDELETE function into the loop using the fileref definition or path. file_name gave you the filename and you have the path so you can create the full path if needed.
elwayfan446
Barite | Level 11

Re: how to delete all files in data step using fdelete function

Message contains a hyperlink Message contains an image

I have tried a few ways and can't get it to work correctly.

I could only get the directory name working if I created a variable.  I think that's probably an extra step but couldn't get it to work otherwise.  If I run this code with the put fname uncommented, I see the complete path and file name in the SAS log. 

 

%let directoryname = "/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/";

DATA _NULL_ ;
  rc = filename('spec',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/") ;
  did=dopen('spec');
  do i=1 to dnum(did);
   file_name=dread(did,i);
   /*put file_name= ;*/
   fname=cats(&directoryname,file_name);
   put fname;
   /*rc=fdelete(fname);*/
  end;
RUN ;

elwayfan446_0-1608662078535.png

 

I am thinking, "OK, these are the files I need deleted, let's go". 

 

I run the code with rc=fdelete(fname);

DATA _NULL_ ;
  rc = filename('spec',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/") ;
  did=dopen('spec');
  do i=1 to dnum(did);
   file_name=dread(did,i);
   /*put file_name= ;*/
   fname=cats(&directoryname,file_name);
   /*put fname;*/
   rc=fdelete(fname);
  end;
RUN ;

I now get the following notes in the log and the files have not been deleted:

elwayfan446_1-1608662266407.png

 

I have been referencing this documentation from SAS as well and just can't seem to put it all together.

https://documentation.sas.com/?cdcId=vdmmlcdc&cdcVersion=8.1&docsetId=lefunctionsref&docsetTarget=p0...

 

 

Reeza
Super User

Re: how to delete all files in data step using fdelete function

It seems to want the fileref not the filename. You can create a fileref using the FILENAME function, similar to the first line of code.
elwayfan446
Barite | Level 11

Re: how to delete all files in data step using fdelete function

I tried this according to the SAS documentation I found.  I just can't put it all together.

 

%let directoryname = "/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/";

DATA _NULL_ ;
  rc = filename('spec',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/") ;
  did=dopen('spec');
  do i=1 to dnum(did);
    file_name=dread(did,i);
   /*put file_name= ;*/
   fname=cats(&directoryname,file_name);
   /*put fname;*/
   /*rc=fdelete(fname);*/
  end;
RUN ;


%macro test;                                                                                                                            
 %let filrf= "/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/";                                                                                                                     
 %let rc=%sysfunc(filename(filrf, file_name=));                                                                                  
 %if &rc ne 0 %then %put %sysfunc(sysmsg());                                                                                            
 %let rc=%sysfunc(filename(filrf));                                                                                                     
%mend test;   
Reeza
Super User

Re: how to delete all files in data step using fdelete function

Get it working for a single folder/file first. Then figure out how to loop it.
elwayfan446
Barite | Level 11

Re: how to delete all files in data step using fdelete function

Message contains an image

Sorry for all of the back and forth.  I promise I am not someone who needs their homework answered, I have just been racking my brain on this one and the other regarding the excel export files for a week.  I try not to post for help unless it is a last resort.

 

I am trying it with a single file in that directory.  When I try what looks right (and probably isn't at this point), I can %put the filrf but when I %put rc it gives me what looks like a number in the log.

 

DATA _NULL_ ;
  %let filrf="/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/";	
  %let rc=%sysfunc(filename(filrf, 202011_360_Mortgage__LLC.xlsx));
  %put &filrf;
  %put &rc;
RUN ;

 

elwayfan446_1-1608675621515.png