Hello
I want to delete a folder( directory) that included files.
In this action I want to delete all files in this folder( directory) and delete the folder( directory) too.
I know how to do it by delete each file and then delete the directory
But if i have 1000 files then i need to run 1000 procedures and then 1 procedure to delete directory.
Is there a way to do it please?
Thanks
Yakov
please use the below code
x 'rm -r foldername or path';
Thank you
I am trying to use the code but get an error
x "/Path/Folder1/";
The error is
ERROR: Shell escape is not valid in this SAS session.
its working for me, i tried the below code
x 'rm -r ~path/foldername';
This is the path
"/usr/local/SAS/SASUsers/LabRet/UserDir/udclk88/TYOTA/"
Can you update the code please and send me ?
thank you so much
yes.name of folder to be deleted is TYOTA
please try the below code to delete TYOTA
X "rm -r /usr/local/SAS/SASUsers/LabRet/UserDir/udclk88/TYOTA";
thank you so much.
I get an error ERROR: Shell escape is not valid in this SAS session.
26 X "rm -R /usr/local/SAS/SASUsers/LabRet/UserDir/udclk88/TYOTA"
26 ! ;
I believe the X commands are not allowed in your environment. Because of which the X command is not working.
Alternatively you need to establish the X command to work. please follow below steps and then try running the code
http://support.sas.com/kb/41/058.html
@Jagadishkatam wrote:
its working for me, i tried the below code
x 'rm -r ~path/foldername';
The error indicates that the option noxcmd is active.
@Ronein With xmcd disabled deleting a folder and its contents is somewhat annoying: you have to to use fdelete to delete each file and finally the same function can be used to delete the empty folder. That approach has one major advantage: the code is independent of the sas-servers operating-system. But it is annoying amount of code, so asking the admins why xcmd is disabled seems to be a good first step.
In the meantime try:
%let folderToDelete = insert_folder_name_here;
data work.FilesToDelete;
length Name $ 100;
keep Name;
rc = filename("folder", "&folderToDelete.");
dirId = dopen("folder");
do i = 1 to dnum(dirID);
Name = dread(dirId, i);
output;
end;
rc = dclose(dirId);
run;
data _null_;
set work.FilesToDelete end=lastDeleted;
put "Deleting " Name;
rc = filename("delfile", cats("&folderToDelete./", Name));
rc = fdelete("delfile");
put "del file " rc=;
rc = filename("delfile");
if lastDeleted then do;
put "Deleting the folder '&folderToDelete.'";
rc = filename("folder", "&folderToDelete.");
rc = fdelete("folder");
put "del folder " rc=;
rc = filename("folder");
end;
run;
@Ronein wrote:
Thank you
I am trying to use the code but get an error
x "/Path/Folder1/";
The error is
ERROR: Shell escape is not valid in this SAS session.
Which means you operate under NOXCMD. This is the default setting of a SAS BI Server installation.
Either have XCMD enabled by the SAS administrator, or run one of the many codes turned up by a google search for "sas delete all files in a directory".
The functions to use from a data step are filename() dopen(), dnum(), dread() and fdelete():
data _null_;
dirname = 'directory path';
dref = 'mydir';
filref = 'myfile';
length fname $200;
rc = filename(dref,dirname);
did = dopen(dref);
do i = 1 to dnum(did);
fname = dread(did,i);
rc = filename(filref,catx('/',dirname,fname));
rc = fdelete(filref);
end;
rc = dclose(did);
rc = fdelete(dref);
run;
Note that this will work only on a one-level directory. If you have further subdirectories in there, the whole thing needs to be done recursively with macro code. And once you've done that, you'll feel the urge to shoot the admin who refused to enable XCMD.
Edit: changed the delete for the directory to use the dref.
PS a way to recursively find files in a directory tree with a macro was just posted here:
I added a method to save the result in a dataset.
It shouldn't be difficult to develop a data step that deletes the files and, when detecting a change in the directory part of the filename, the directory.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.