listing directories using ls

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

listing directories using ls

Hi,

 

So this is similar but different from previous posts.

 

I am trying to create a list of directories (not files) in a DIS project.

In DIS I can get a list of all the folder contents using x ls -lp &file_path. >/tmp/folderlisting.csv; 

 

In Bash script ls -d */ "filepath" works perfectly. However the */ is not working in SAS as it treats this as a comment end. Quoting this has not helped.

 

So, any help I could get in finding a solution or alternative to x ls -d */ "&filepath." >/tmp/folderlisting.csv; would be appreciated.

 

I am aware that could use multiple lines of code to search through the full output and create a new file but i am looking for an efficient streamlined solution.

 

NB I have also tried | grep ^d but again DIS doesn't like this.


Accepted Solutions
Solution
a week ago
Super User
Posts: 23,724

Re: listing directories using ls

Posted in reply to JordanWood
Try SYSEXEC or CALL SYSTEM instead of an X command. You can enclose your command in quotes which will help it process correctly.

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 23,724

Re: listing directories using ls

Posted in reply to JordanWood
Try SYSEXEC or CALL SYSTEM instead of an X command. You can enclose your command in quotes which will help it process correctly.
Occasional Contributor
Posts: 6

Re: listing directories using ls

Thanks for the help. I tried lots of the variants suggested but the one that worked is ;

 

%sysexec (ls -l "&file_path" | grep ^d  > /tmp/folderlisting.csv);

 

also had to do some double quoting of the variable file_path when setting it but that is all part of the differences in EG and DIS.

Super User
Posts: 10,239

Re: listing directories using ls

Posted in reply to JordanWood

Use the filename pipe method:

filename oscmd pipe "ls -d */ '&filepath.' 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

Then look at the log and see how you can use the output of the ls.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,111

Re: listing directories using ls

[ Edited ]
Posted in reply to JordanWood

I wouldn't use the X command, but if you do then add quotes around the command you want to run so that the command does not get interpreted by SAS or the SAS macro processor.

filename list temp;

x "ls -d ~/*/ > '%sysfunc(pathname(list))'" ;
data _null_;
  infile list ;
  input;
  put _infile_;
run;

Or easier still just use the PIPE engine and skip the temporary file.

data _null_;
  infile "ls -d ~/*/" pipe ;
  input ;
  put _infile_;
run;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 67 views
  • 1 like
  • 4 in conversation