作用是取得一个文件夹下所有文件和文件夹的信息
- %macro GetFileAndSubDirInfoInDir(InDirPath,Filter,TargetTable,OutFilePath,Expand);
- /**********************************************************************/
- /* 此宏用于获得目标文件夹下所有文件及子文件夹的信息,并将此信息保存至 */
- /* SAS表格或导出至txt文件中。其中,InDirPath是指定的目标文件夹路径, */
- /* 路径最后要加上\;Filter是文件过滤设置,支持*和?通配符,若需要导出 */
- /* 所有文件列表,可以设置为空;TargetTable是保存文件信息的SAS表格,若 */
- /* 不需要生成,可以设为空;OutFilePath是导出txt文件的路径,若不需要导 */
- /* 出,可以设为空;Expand是标记变量,=Yes表示展开所有文件夹,列表最后 */
- /* 包含文件及其所处文件夹信息,=NO表示只列出目标文件夹下第一层文件和 */
- /* 文件夹的信息。 */
- /* */
- /* 最终得到的是含有目标文件夹下所有文件及子文件夹信息的SAS表格和存于 */
- /* 指定路径的txt文件。 */
- /* */
- /* 注意运行此宏可能会产生数量不等的错误信息,这是由于利用DIR命令得到 */
- /* 结果表格纵向格式不一致引起的,但不会影响结果的正确性。 */
- /* */
- /**********************************************************************/
- /* 确保InDirPath后接\符号 */
- %if %SYSFUNC(FIND(%SYSFUNC(REVERSE(&InDirPath)),\)) NE 1 %then %let InDirPath=&InDirPath.\;
- /* 关闭显示LOG信息 */
- options nosource nonotes errors=0;
- /* 第一步:首先将InDirPath文件夹下所有文件的信息导出至OutFilePath文件中 */
- /* 情形1:参数OutFilePath为完整文件路径情形 */
- %if %SYSFUNC(FIND(&OutFilePath,%STR(.))) NE 0 %then %do;
- /* 情形1-1:展开所有文件夹 */
- %if %UPCASE(&Expand) EQ YES %then %do;
- %ChkFile(&OutFilePath);
- options noxwait xsync;
- x "dir &InDirPath.&Filter /s /a > &OutFilePath";
- %end;
- /* 情形1-2:不展开文件夹,只包含第一层子文件夹和文件的信息 */
- %else %if %UPCASE(&Expand) EQ NO %then %do;
- %ChkFile(&OutFilePath);
- options noxwait xsync;
- x "dir &InDirPath.&Filter > &OutFilePath";
- %end;
- /* 情形1-3:错误输入Expand参数情形 */
- %else %do;
- %put ERROR: The last parameter should be Yes or No, case insensitive and without quotes.;
- %goto exit;
- %end;
- %end;
- /* 情形2:参数OutFilePath为空情形 */
- %else %if %UPCASE(&OutFilePath) EQ %STR() %then %do;
- /* 情形2-1:展开所有文件夹 */
- %if %UPCASE(&Expand) EQ YES %then %do;
- options noxwait xsync;
- x "dir &InDirPath.&Filter /s /a > d:\GFASDIID_temp.txt";
- %let OutFilePath=d:\GFASDIID_temp.txt;
- %end;
- /* 情形2-2:不展开文件夹,只包含第一层子文件夹和文件的信息 */
- %else %if %UPCASE(&Expand) EQ NO %then %do;
- options noxwait xsync;
- x "dir &InDirPath.&Filter > d:\GFASDIID_temp.txt";
- %let OutFilePath=d:\GFASDIID_temp.txt;
- %end;
- /* 情形2-3:错误输入Expand参数情形 */
- %else %do;
- %put ERROR: The Expand should be Yes or No, case insensitive and without quotes.;
- %goto exit;
- %end;
- %end;
- /* 情形3:错误输入OutFilePath参数情形 */
- %else %do;
- %put ERROR: The OutFilePath should contain the full path of directory, including filename and filename extension.;
- %goto exit;
- %end;
- /* 第二步:接着将OutFilePath文件的信息导入SAS数据表中 */
- /* 需要生成SAS数据表情形 */
- %if %UPCASE(&TargetTable) NE %STR() %then %do;
- /* 情形1:展开所有文件夹,此时从第4行开始读 */
- %if %UPCASE(&Expand) EQ YES %then %do;
- data GFASDIID_temp;
- infile "&OutFilePath." firstobs=4 truncover;
- input DataString $ 1-100 @; /* 读取的第一个变量,变量长度为10,@表示光标不下移,继续读取此行 */
- input @1 Date YYMMDD10. @13 Time TIME. @19 Bytes_temp $17. @37 FileName $64.;
- if (FIND(DataString,'<DIR>') EQ 0) AND (FIND(DataString,'\') NE 0 OR Date NE .);
- format Date YYMMDD10. Time HHMM.;
- run;
- data &TargetTable(drop=DataString Bytes_temp);
- retain Date Time Bytes FileName DirPath;
- set GFASDIID_temp;
- Bytes=INPUT(Bytes_temp,COMMA17.);
- format Bytes COMMA17.;
- if FIND(DataString,'\') NE 0 then DirPath=COMPRESS(DataString,'的目录');
- if Date NE .;
- run;
- %end;
- /* 情形2:不展开文件夹,只包含第一层子文件夹和文件的信息,此时从第8行开始读 */
- %else %do;
- data GFASDIID_temp(drop=DataString);
- infile "&OutFilePath" firstobs=8 truncover;
- input DataString $ 1-10 @; /* 读取的第一个变量,变量长度为10,@表示光标不下移,继续读取此行 */
- input @1 Date YYMMDD10. @13 Time TIME. @19 Bytes_temp $17. @37 FileName $64.;
- if Date NE .;
- format Date YYMMDD10. Time HHMM.;
- run;
- data &TargetTable(drop=Bytes_temp);
- retain FileName Dir Bytes Date Time;
- set GFASDIID_temp;
- Bytes=INPUT(Bytes_temp,COMMA17.);
- format Bytes COMMA17.;
- if Bytes EQ . then Dir='<DIR>';
- else Dir='';
- run;
- %end;
- %end;
- /* 如需要在SAS的Output窗口中打印文件列表,请取消如下注释 */
- /*proc print data=&TargetTable;*/
- /*title1 "Files identified through saved file";*/
- /*run;*/
- /* 删除不必要的表格 */
- proc delete data=GFASDIID_temp;
- run;
- /* 恢复显示LOG信息 */
- options source notes errors=5;
- %if &OutFilePath=d:\GFASDIID_temp.txt %then x "erase d:\GFASDIID_temp.txt";
- %exit:
- %mend;
- %macro Demo();
- %let InDirPath=e:\Works\;
- %let Filter=*.sas; /* 文件过滤设置,若需要导出所有文件列表,则设置为空即可,即Filter=; */
- %let TargetTable=FileList; /* 若不需要生成包含文件列表的SAS表格,则设为空,大小写不敏感 */
- %let OutFilePath=; /* 若不需要导出文件列表txt文件,则设为空,大小写不敏感 */
- %let Expand=Yes; /* =Yes表示展开所有文件夹,列表最后包含文件及其所处文件夹信息,否则=No,大小写不敏感 */
- %GetFileAndSubDirInfoInDir(&InDirPath,&Filter,&TargetTable,&OutFilePath,&Expand);
- %mend;


雷达卡



京公网安备 11010802022788号







