Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 列出UNIX SAS中的文件_List_Unix_Directory_Sas - Fatal编程技术网

List 列出UNIX SAS中的文件

List 列出UNIX SAS中的文件,list,unix,directory,sas,List,Unix,Directory,Sas,我发现了一个代码,其中列出了路径中的所有目录和子目录。 但它只带来目录和文件名。 你们能帮我把文件的主人和大小告诉我吗 %macro list_files(dir); %local filrf rc did memcnt name i; %let rc=%sysfunc(filename(filrf,&dir)); %let did=%sysfunc(dopen(&filrf)); %if &did eq 0 %then %do;

我发现了一个代码,其中列出了路径中的所有目录和子目录。 但它只带来目录和文件名。 你们能帮我把文件的主人和大小告诉我吗

%macro list_files(dir);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&dir));
  %let did=%sysfunc(dopen(&filrf));      

   %if &did eq 0 %then %do; 
    %put Directory &dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&did));   

   %let name=%qsysfunc(dread(&did,&i));

   %if %index(%qscan(&name,-1,'/'),.) gt 0   %then %do;

        data _tmp;
          length dir $512 name $100;
          dir=symget("dir");
          name=symget("name");
        run;
        proc append base=want data=_tmp;
        run;quit;

      %end;
      %else %if %qscan(&name,2,.) = %then %do;        
        %list_files(&dir/&name)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&did));
   %let rc=%sysfunc(filename(filrf));     

%mend list_files;

DOPTNAME
是你的朋友

请阅读SAS文档了解

此示例使用fileref MYDIR打开目录,检索所有依赖于系统的目录信息项,将它们写入SAS日志,然后关闭目录:

%let rc=%sysfunc(filename(filrf, physical-name));
%let did=%sysfunc(dopen(&filrf));
%let infocnt=%sysfunc(doptnum(&did));
%do j=1 %to &infocnt;
   %let opt=%sysfunc(doptname(&did, &j));
   %put Directory information=&opt;
%end;
%let rc=%sysfunc(dclose(&did));

%macro test; 
 %let filrf=mydir;
 %let rc=%sysfunc(filename(filrf, physical-name));
 %let did=%sysfunc(dopen(&filrf));
 %let infocnt=%sysfunc(doptnum(&did));
  %do j=1 %to &infocnt;
   %let opt=%sysfunc(doptname(&did, &j));
   %put Directory information=&opt;
  %end;
 %let rc=%sysfunc(dclose(&did));
%mend test;

%test
使用
finfo()
函数。您可以在一个
数据
步骤中完成这一切。

/* Macro variable to store the directory. Do not keep ending slash. */
%let directory = /my/directory;

filename mydir "&directory";

data file_list;

    length directory
           filetype $15.
           filename $1000.
           owner    $100.
           size     8.
    ;

    directory = "&directory/";

    /* Open the directory */
    did = dopen("mydir");

    /* If the directory exists, loop through all files in the directory */
    if(did) then do;
        do i = 1 to dnum(did);

            /* Get the filename */
            filename = dread(did, i);

            /* Create a filename variable and create a file ID to read its attributes */
            rc  = filename('infile', cats(directory,filename));
            fid = fopen('infile');

            owner         = finfo(fid, 'Owner Name');
            size          = finfo(fid, 'File Size (bytes)');

            /* Flag if it's a directory or file */
            if(missing(size)) then filetype = 'Directory';
                else filetype = 'File';

            /* Close the file */
            rc = fclose(fid);

            output;
        end;
    end;

    /* Close the directory */
    rc = close(did);

    keep directory filename owner size filetype;
run;