Creating a sorted directory listing with complete alpha sort

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
montanan
Junior Coder
Posts: 49
Joined: Mon Jul 09, 2007 3:44 pm
Location: San Jose, CA

Creating a sorted directory listing with complete alpha sort

Post by montanan » Wed Dec 23, 2009 6:44 pm

If there is a built-in command in Macro Scheduler that can do this, please let me know.

In the meantime, I am using...

Code: Select all

Run Program>CMD /c dir C:\photos\*.* /s /b /o:n > C:\filer\MyDirOutput.txt
... and I get a result that looks like this (not sorted as desired)...

c:\photos\2008\myphoto1.jpg
c:\photos\2008


I've tried various combination of the DOS sort command...

/o
/o:n
/on
/o:gn

... but no matter what I do I cannot get the output to show the directories before the files.

I'm trying to create an output file with a complete alpha sort that looks like the following:

c:\photos\2008
c:\photos\2008\myphoto1.jpg
c:\photos\2008\myphoto2.jpg
c:\photos\2008\myphoto3.jpg
c:\photos\2009
c:\photos\2009\myphoto1.jpg
c:\photos\2009\myphoto2.jpg
c:\photos\2009\myphoto3.jpg
c:\photos\2009\Zoo
c:\photos\2009\Zoo\myphoto1.jpg
c:\photos\2009\Zoo\myphoto2.jpg


Any direction here would be a big help!

-Richard

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 23, 2009 7:15 pm

Here is a concept, no time for code ...

Create the output file as you did in your example, that is a good source file.
Read the output file lines from that source file..
Use RegEx to append the folder lines to a new file: folders.txt. (Lines that do not end with ".xxx").
Use RegEx to append the filename lines to a different file, names.txt. (Lines that end with ".xxx").
Sort each of those two files.
Now make a loop to read the folders.txt file and grab the first line and append it to a new summary file, sortedfiles.txt.
Now read the filenames file names.txt until a new folder is shown, appending each of the new lines to the summary file sortedfiles.txt.
When the folder changes, go back to the folder file and read the next line, repeat these steps until both folders have been read.
The sortedfiles.txt file should be the results you described.

This probably sounds more difficult than it really will be.
Someone may also have a better command utility that can sort the file as you want it.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 23, 2009 9:23 pm

I'd do it slightly differently. no time for code either.

In psuedo code:

run dir /on /ad /b /s > file
read file one line at a time in a loop (each line contains a directory name in sorted order)
within the loop
run dir directory name\*.* /on /b > a second file
another loop read each line of the second file
write the directory name\the second file line (a file name) to a third and final file
end second loop
delete second file
end first loop

delete first file

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 23, 2009 10:02 pm

You provided more info than I did JRL. I also had a few internal loops in my concept but did not identify them so explicitly. But we both have a similar approach, must be some merit in one of them. :wink:

New idea ...

I just remembered the TREE command vs. the DIR command.
That might be exactly what you want, as is.

Here is sample output from TREE /F > tempfile.txt:
├───NetInfo
│ │ NetInfo Help.lnk
│ │ NetInfo License Agreement.lnk
│ │ NetInfo Read Me.lnk
│ │ NetInfo.lnk
│ │
│ ├───PamFax
│ │ PamFax Portal.lnk
│ │ Send PamFax.lnk
│ │
│ ├───Passware
│ │ Office Key.lnk
│ │ Passware Kit Help.lnk
│ │
│ ├───Popims Animator
│ │ Popims Animator.lnk
│ │ Popims on the web.lnk
│ │ Uninstall.lnk
│ │
│ ├───ReaConverter 4.0 Pro
│ │ └───Magic
│ │ Magic 1.lnk
│ │ Magic 2.lnk
Just edit the output file to remove the line characters.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

montanan
Junior Coder
Posts: 49
Joined: Mon Jul 09, 2007 3:44 pm
Location: San Jose, CA

Thank you for the help with this.

Post by montanan » Thu Dec 24, 2009 4:42 pm

I am going to try the TREE function next, but here is what I found worked pretty well.

I borrowed some VB Script code that Marcus had done for sorting columnar data in Excel and called it as follows...

Code: Select all

SRT>RenameTextFileAsExcel
 RenameFile>c:\filer\photolisting.txt,c:\filer\photolisting.xls
End>RenameTextFileAsExcel

SRT>SortExcelFile
VBSTART
Sub SortExcelFile(file,sort_column)
  Set XLApp = CreateObject("Excel.Application")
  XLApp.Workbooks.Open(file)
  XLApp.ActiveWorkbook.ActiveSheet.Range("A1").Sort XLApp.ActiveWorkbook.ActiveSheet.Range(sort_column),,,,,,,0
  XLApp.ActiveWorkBook.Save
  XLApp.ActiveWorkBook.Close false
  XLApp.quit
End Sub
VBEND
VBRun>SortExcelFile,c:\filer\photolisting.xls,A1
End>SortExcelFile

SRT>RenameTextFileAsText
 RenameFile>c:\filer\photolisting.xls,c:\filer\photolisting.txt
End>RenameTextFileAsText
For this particular application, I need to do some further manipulation in Excel anyway, so didn't necessarily need to convert back to .txt

-Richard

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts