AWK - Directory to Rows

This AWK script parses the output of the Windows "DIR *.* /s" command and puts in columnar format with column titles. This makes it easy to open in your favorite spreadsheet program using a fixed column format.



#
BEGIN {
   record="  ";                               # Start output record at 2 spaces
   for (i = 1; i <= 7; ++i) {record=record record;}   # Expand output record to 256 spaces
   record=repl(record,1,14,"Date");                   # Date goes starts at column 1
   record=repl(record,12,26,"Size");                  # File size starts at column 12
   record=repl(record,28,87,"Filename");              # File name starts in column 28
   record=repl(record,89,148,"Fullpath");             # Full path starts in column 89
   print record;                                      # Print the header line
}
function repl(s,f,t,v)                                 # Custom function to add data to the output record
{ return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) }
{
if ( substr($0,2,9) == "Directory" ) {                 # If this line contains the directory name,
   dir=substr($0,15,60);                              # save the name
   }
else if (substr($0,3,1) == "/" &&                   
        substr($0,6,1) == "/" &&
        substr($0,25,1) != "<")   { # If this line contains a file ...
   date=substr($0,1,10);                              # Date
   size=substr($0,24,15);                             # File size
   file=substr($0,40,60);                             # File name
   name=dir "\\" file;                                # Full path
   record="  ";                               # Start output record at 2 spaces
   for (i = 1; i <= 7; ++i) {record=record record;}   # Expand output record to 256 spaces
   record=repl(record,1,10,date);                     # Date goes starts at column 1
   record=repl(record,12,26,size);                    # File size starts at column 12
   record=repl(record,28,87,file);                    # File name starts in column 28
   record=repl(record,89,148,dir);                    # Full path starts in column 89
   print record;                                      # Print the output line
   }
}

No comments:

Post a Comment

Upload CSV Files to Google BigQuery

This Google App Script gets each file in the "ToBeUploaded" folder in Google Drive and creates a Google BigQuery load job with tha...