Hi,
I need to extract conent of some folder on server, this folder can contain any files(txt,pdf etc.) not only SAS tables and views, so proc content isn't suitable for this goal.
Off course can be created some .bat file that will run windows script file(.vbs) that will provide needed info, but I need all this folder content information in SAS table so more comfortable will be have have all functionality that extratc this info in SAS code.
In another word I need some procedure similar to proc content but this procedure should extract statistic(size, create date etc.) from all type of files.
May be exists some view in dictioary or SASHelp, but I didn't sow needed info in list of these views...
Thanks!
The following code works for my specific folder, please note, to make it simpler, I did not acount for folders:
data have;
infile test ;
format date mmddyy10.;
input date :mmddyy10.
@13 time :$8.
@31 size :comma9.
@40 name :$ 40-300; *using column input to get whatever contents you want. You may need to skip the headlines;
if _n_ <2 then delete;
run;
You probably need to tweak the column number to be able to read your files. And I have not figured out a way to not read the last two records, which are summaries.
Regards,
Haikuo
Assume you are under Windows, you can either invoke 'x' command like this:
Options noxwait;
x 'dir *.* x:\ /b /d >x:\test.txt';
filename test "c:\test.txt";
or use PIPE method to avoid permanent text file:
%let path=x:;
filename test pipe "dir /b &path\*.* " ;
Then use a data step to retrieve the data:
data have;
infile test;
input ; *using column input to get whatever contents you want. You may need to skip the headlines;
run;
Regards,
Haikuo
Hello,
Can you please append at least one column to input statemant in data step that you described higher?
I ask becouse I have already made similar solution with x command that run .bat file that run .vbs file(Visual script) file that just look into some folder get all sub file and needed property then store it in csv file and then I read this file to sas data set by infile statement but I feel that should be easier and more optimal solution...
So can you please tune your code in last data step:
data have;
infile test;
input ; *using column input to get whatever contents you want. You may need to skip the headlines;
run;
Thanks!
The following code works for my specific folder, please note, to make it simpler, I did not acount for folders:
data have;
infile test ;
format date mmddyy10.;
input date :mmddyy10.
@13 time :$8.
@31 size :comma9.
@40 name :$ 40-300; *using column input to get whatever contents you want. You may need to skip the headlines;
if _n_ <2 then delete;
run;
You probably need to tweak the column number to be able to read your files. And I have not figured out a way to not read the last two records, which are summaries.
Regards,
Haikuo
Hi again,
Thanks, it's probably what I needed.
Only one small remark - data step return not all files that are on folder, just 4-5, plus some header and footer, as you wrote higher...Nut offcourse it can be simply deleted.
Thanks!
Since they were extracted from older codes, so for both of the following, I have option '/b', meaning only the name will be shown. Just remove that option, you will have everything within that folder.
Options noxwait;
x 'dir *.* x:\ /b /d >x:\test.txt';
filename test "c:\test.txt";
or use PIPE method to avoid permanent text file:
%let path=x:;
filename test pipe "dir /b &path\*.* " ;
Remove the BOLD part, and try again. FWIW, I suggest you follow Art's link and learn it thoroughly.
Regards,
Haikuo
Hi,
I use the macro below to get all the file names in a directory. you may be able to modify it to get all the information you need.
%macro test(mydir);;
data want;
%let bb=%sysfunc(filename(filrf,&mydir));
%let did=%sysfunc(dopen(&filrf));
%let flname=;
%let memcount=%sysfunc(dnum(&did));
%if &memcount > 0 %then %do;
%do i=1 %to &memcount;
%let flname&i=%qsysfunc(dread(&did,&i));
file=symget("flname&i");
output;
%end; %end;
%let rc=%sysfunc(dclose(&did));
run;
%mend;
Linlin
Hi Lin,
Thanks for your anwer, I tried and sub-file names retriview correct, but I need not just file name, I need also file size and date of last modification.
File size probably can be found by FINFO function, but this func can't return date of last modification...
May be this property can be found by some different function etc.?
I have already made solution with .vbs(Visual script) file that just look into some folder get all sub file and needed property then store it in csv file and then I read it to sas by infile statement but I feel that should be easier and more optimal solution...
Thanks!
Hi Yura2301,
the modified code retrieves file name, size, and date of last modification.
%macro test(mydir);
data want(drop=fid ff filrfb rc);
%let bb=%sysfunc(filename(filrf,&mydir));
%let did=%sysfunc(dopen(&filrf));
%let flname=;
%let memcount=%sysfunc(dnum(&did));
%if &memcount > 0 %then %do;
%do i=1 %to &memcount;
%let flname&i=%qsysfunc(dread(&did,&i));
filrfb='temp';
ff=filename(filrfb,"&mydir\&&flname&i");
fid=fopen(filrfb);
dte=finfo(fid,'Last Modified');
size=finfo(fid,'File Size (bytes)');
file=symget("flname&i");
output;
rc=fclose(fid);
%end; %end;
%let rc=%sysfunc(dclose(&did));
run;
%mend;
%test(c:\temp)
Linlin
Hi again Lin,
I suppose you are using SAS version 9.2 or later, in this case your macro will work perfect, but I have SAS 9.1.3 so in my version FINFO function isn't so flexible as on later version, I can't extract filesize and another attribtes using this function in SAS 9.1.3.
But anyway thanks for your answer:)!
Expanding on Hai.Kuo's suggestion, take a look at: http://support.sas.com/kb/24/820.html
I prefer to keep my utility macros minimalist. Here is a macro to read the output of the Windows DIR command.
It does not attempt to perform the DIR command, perform subsetting or do any reports, it just creates a SAS dataset.
It probably could be made even simplier now that SAS has more datetime input formats.
Users might need to watch out for Windows settings that could effect the format of the date and time values in the output of the DIR command.
Or if you are still running Netware disks the format of the DIR command on those volumes used to be different.
Thanks Tom!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.