我需要从目录中删除所有文本文件。以下程序适用于列出的文件(例如:file.txt),但是当我尝试使用* .txt时,它不起作用。我错过了什么,或者有更好的方法来删除目录中的所有txt文件。
data _null_;
fname = "_files";
rc = filename(fname,"&path\file.txt");
if rc = 0 and fexist(fname) then
rc = fdelete(fname);
rc = filename(fname);
run;
答案 0 :(得分:4)
如果您是宏的粉丝..下面的代码也应该这样做。
options mlogic;
%macro delete_all_txt_files_in_folder(folder);
filename filelist "&folder";
data _null_;
dir_id = dopen('filelist');
total_members = dnum(dir_id);
do i = 1 to total_members;
member_name = dread(dir_id,i);
if scan(lowcase(member_name),2,'.')='txt' then do;
file_id = mopen(dir_id,member_name,'i',0);
if file_id > 0 then do;
freadrc = fread(file_id);
rc = fclose(file_id);
rc = filename('delete',member_name,,,'filelist');
rc = fdelete('delete');
end;
rc = fclose(file_id);
end;
end;
rc = dclose(dir_id);
run;
%mend;
%delete_all_txt_files_in_folder(C:\try)
答案 1 :(得分:1)
您不能在fdelete中使用通配符。您需要遍历目录中的所有文件,或者您可以使用x命令
x 'del &path.\*.txt';
或类似的取决于您的操作系统(但它取决于操作系统,并且需要XCMD
权限。
这是循环:
%let path=d:\temp;
filename filrf "&path.";
data _null_;
did = dopen('filrf');
memcount = dnum(did);
do while (memcount>0);
fname = dread(did,memcount);
if scan(lowcase(fname),2,'.')='txt' then do;
rcref = filename('fref',catx('\',"&path.",fname));
rcdel = fdelete('fref');
end;
memcount+-1;
end;
stop;
run;