Hi,
Is there a way to delete certain files from a folder? I have a conversion program that creates a bunch of csv files. However, I would like to delete all the files that begin with "DES" since they're not needed. Is there a step I can add to the conversion program to delete the DES files?
Any help is truly appreciated.
Thanks
Hello,
It depends on what environment your working on. Windows or Unix/Linux. Did you try the X-commands (run system commands from SAS).
In UNIX/LINUX:
X "find -name . 'DES*' -exec rm {} \;";
In Windows:
X "Get-ChildItem -include DES* -recurse -force | Remove-Item -force";
In some environments (Unix) SAS users are restricted to run commands, in that case you may try:
/* Read files in a folder */
%let path=/user/files;
FILENAME _folder_ "%bquote(&path.)";
data filenames(keep=memname);
handle=dopen( '_folder_' );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
if substr(memname,1,3)='Wra' then output filenames;
end;
end;
rc=dclose(handle);
run;
filename _folder_ clear;
/* delete files identified in above step */
data _null_;
set filenames;
fname = 'todelete';
rc = filename(fname, quote(cats("&path",'/',memname)));
rc = fdelete(fname);
rc = filename(fname);
run;
It looks like FDELETE() does not support wildcards so you're best off using SYSEXEC() and a command instead. That would be like:
Be careful with the quotes there.
x 'rm "path to file\DES*.extension"';
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!
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.
Ready to level-up your skills? Choose your own adventure.