<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Unix command to get file extension in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917582#M44358</link>
    <description>&lt;P&gt;Hello Kurt,&lt;/P&gt;
&lt;P&gt;I have used exactly a script similar to yours but I found that some files have very large file extension that I am not famillar with. What's the maximum length for a file extension.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am scanning files that have been created since 2010, therefore I am getting very large file extension, and some file name does not have a dot. So when I scan for file extension, I am getting the complete file name.&amp;nbsp; Any idea how to solve that issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank in advance for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Feb 2024 14:10:43 GMT</pubDate>
    <dc:creator>alepage</dc:creator>
    <dc:date>2024-02-23T14:10:43Z</dc:date>
    <item>
      <title>Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917573#M44356</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have made a listing of all the folders&amp;nbsp; and all the files from the root (/folder1/...) using two different find command.&amp;nbsp; But after, reviewing the files listing, I have found that some files have does not have file extension.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a Unix command that I can use in SAS to get the file extension.&lt;/P&gt;
&lt;P&gt;Please provide a sample example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname dest1 base "/.../Data_Retention/data";
filename folderls pipe "find /folder1/sasdata/ -type d  ";
data dest1.folderslisting;
length text $2000.;
infile folderls;
input;
text=_infile_;
run;
filename filelist pipe "find /folder1/sasdata/ -type f ";
data dest1.fileslisting;
length text $1000.;
infile filelist;
input;
text=_infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 13:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917573#M44356</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-02-23T13:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917577#M44357</link>
      <description>&lt;P&gt;Do it in SAS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename filelist pipe "find /folder1/sasdata/ -type f ";
data dest1.fileslisting;
length text $1000. extension $10;
infile filelist;
input;
text=_infile_;
if countw(scan(text,-1,"/"),".") &amp;gt; 1 then extension = scan(text,-1,".");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;On the UNIX side, you would need to apply the &lt;FONT face="courier new,courier"&gt;cut&lt;/FONT&gt; filter command.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 14:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917577#M44357</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-23T14:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917582#M44358</link>
      <description>&lt;P&gt;Hello Kurt,&lt;/P&gt;
&lt;P&gt;I have used exactly a script similar to yours but I found that some files have very large file extension that I am not famillar with. What's the maximum length for a file extension.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am scanning files that have been created since 2010, therefore I am getting very large file extension, and some file name does not have a dot. So when I scan for file extension, I am getting the complete file name.&amp;nbsp; Any idea how to solve that issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank in advance for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 14:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917582#M44358</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-02-23T14:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917584#M44359</link>
      <description>&lt;P&gt;File extension are just a convention and not a requirement even on Windows.&amp;nbsp; Certainly since the original DOS filename limitations of 8 characters before the period and 3 after.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And on Unix they have even less meaning/impact.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you can have filenames with no period in them.&amp;nbsp; Or filenames with multiple periods in them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And on Unix if you start the filename with a period it is a "hidden" file.&amp;nbsp; Hidden in the sense that you have to add an option to the ls command if you want them to appear in the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code I use in SAS to extract the extension from a filename.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if index(filename,'.')&amp;gt;1 then extension=scan(filename,-1,'.');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 14:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917584#M44359</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-23T14:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917585#M44360</link>
      <description>&lt;P&gt;If you don't have a dot, then you don't have an extension by definition.&lt;/P&gt;
&lt;P&gt;The max filename length (space reserved in the directory file) is 255 for UNIX/Linux. You can modify my code to have an ELSE branch where the whole string after the last slash is taken.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 14:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917585#M44360</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-23T14:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917742#M44362</link>
      <description>&lt;P&gt;The printf action of the find command lets you select what information in what structure you want returned.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I couldn't test below script but I have used similar code in the past.&lt;/P&gt;
&lt;P&gt;With below syntax for each folder or file the find command will return a pipe delimited string with a line feed at the end - which in turn makes it easy to read into SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename ls pipe "find / -printf %nrstr('%h|%f|%y\n')";
/*filename ls pipe "find / -printf %nrstr('%h|%f|%y\n') 2&amp;gt;/dev/null";*/
data work.listing;
  infile ls lrecl=1024 truncover dlm='|' dsd;
  input path:$900. file_name:$200. file_type:$1.;
run;
filename ls clear

/**
-printf: Possible values for directive %y:
  f: Regular file
  d: Directory
  l: Symbolic link
  p: Named pipe (FIFO)
  c: Character special file
  b: Block special file
  s: Socket
**/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The filename in comment with &lt;EM&gt;2&amp;gt;/dev/null&lt;/EM&gt; is something you could use for dealing with messages in case of insufficient permissions for listing directory content.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2024 22:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/917742#M44362</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-02-24T22:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919363#M44411</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76331"&gt;@alepage&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Your question is&amp;nbsp; "&lt;SPAN&gt;&lt;EM&gt;Is there a Unix command that I can use in SAS to get the file extension.&lt;/EM&gt;"&lt;BR /&gt;Well there is . Assume that the file has an extension then use the following command to get the extension.&lt;BR /&gt;Note &lt;STRONG&gt;filename&lt;/STRONG&gt; is the variable holding the name of the file.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;${filename##*.}&lt;/PRE&gt;
&lt;P&gt;The following example shows the usage&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1709843257586.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94469i56136CEC902DDBE4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1709843257586.png" alt="Sajid01_0-1709843257586.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 20:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919363#M44411</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2024-03-07T20:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919364#M44412</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; Note that method will have the same trouble as SCAN() does with filenames that do no contain a period.&amp;nbsp; So you would want to first test that the name actually has a period (and perhaps that it does not start with a period) before using that method to extract the characters after the period.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 20:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919364#M44412</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-07T20:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: Unix command to get file extension</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919365#M44413</link>
      <description>&lt;P&gt;You already have a lot of interesting answers but, just for fun, one more.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could use &lt;A href="https://github.com/SASPAC/baseplus" target="_self"&gt;BasePlus&lt;/A&gt; package's &lt;A href="https://github.com/SASPAC/baseplus/blob/1.36.0/baseplus.md#dirsandfiles-macro-6" target="_self"&gt;%dirsAndFiles()&lt;/A&gt; macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's OS independent, provides data in wide or long format, with details on files an directories or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EXAMPLE 1.&amp;nbsp;Get list of files and directories:&lt;/P&gt;
&lt;DIV class="highlight highlight-source-sas notranslate position-relative overflow-auto" dir="auto"&gt;
&lt;PRE&gt;%dirsAndFiles(C:\SAS_WORK\,&lt;SPAN class="pl-c1"&gt;ODS&lt;/SPAN&gt;=&lt;SPAN class="pl-c1"&gt;work&lt;/SPAN&gt;.result1)&lt;/PRE&gt;
&lt;DIV class="zeroclipboard-container"&gt;EXAMPLE 2.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Get detailed info:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="highlight highlight-source-sas notranslate position-relative overflow-auto" dir="auto"&gt;
&lt;PRE&gt;%dirsAndFiles(~/,&lt;SPAN class="pl-c1"&gt;ODS&lt;/SPAN&gt;=&lt;SPAN class="pl-c1"&gt;work&lt;/SPAN&gt;.result2,details=&lt;SPAN class="pl-c1"&gt;1&lt;/SPAN&gt;)&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. To install and use &lt;A href="https://github.com/SASPAC/baseplus" target="_self"&gt;basePlus&lt;/A&gt; package do:&lt;/P&gt;
&lt;UL dir="auto"&gt;
&lt;LI&gt;Enable the framework [first time only]:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/main/SPF/SPFinit.sas";
%include SPFinit; /* enable the framework */&lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Install the framework and the package on your machine in the folder you created:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;"; 
%installPackage(SPFinit BasePlus) &lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;From now on, in your SAS session just run it like this:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;";
%include packages(SPFinit.sas);

%loadPackage(basePlus)  &lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;A title="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" href="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" target="_self" rel="nofollow noopener noreferrer"&gt;Link to details&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 20:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unix-command-to-get-file-extension/m-p/919365#M44413</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-07T20:43:23Z</dc:date>
    </item>
  </channel>
</rss>

