Question on getting file lists from a folder, changing drive

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

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

DOS vs VB directory list speed test

Post by JRL » Tue May 23, 2006 10:35 pm

To anyone curious about whether DOS or VBScript generates a directory list faster, I have written a script that will let you test the two for side-by-side comparison. My personal results are varied. I have a diectory on a network server that contains 2800 directories. DOS compiles a list in 12 seconds and VB compiles the same list in 63 seconds. If I check my entire C:\ drive DOS compiles the list in 9 seconds and VB does it in 16 seconds. However if I check just C:\Documents and Settings, DOS takes 3 seconds and VB takes only 2.

A couple of things about this script.

You can run this over and over again without closing the main program and the times should be accurate for each run for each process. However, the VB window will continuously append to the displayed directories list until you actually close the script and restart it. I don't know Visual Basic so I don't know how to prevent the list from growing other than to close the program and start it again.

If you browse to a folder that will be the starting point for the list, you must select a file in that folder, The dialog browse fuction will not let you select a directory, you can only select a file. The script will strip the filename off before it runs, you do not need to do it manually. If you type in a path and directory name, you do not need to type in a file name.

Hope someone finds this useful,
Dick

Code: Select all

Let>title=Path Selection
Dialog>Dialog1
   Caption=%title%
   Width=285
   Height=212
   Top=50
   Left=CENTER
   Label=Directory to process,32,88
   Label= ,32,1
   CheckBox=msCheckBox1,Get Directories using DOS,32,32,153,False
   CheckBox=msCheckBox2,Get Directories using VB,32,56,145,False
   Edit=msEdit1,32,104,121,
   Button=Process,32,152,75,25,3
   Button=Cancel,168,152,75,25,2
   Button=Browse,168,104,75,25,4
   FileBrowse=Browse,msEdit1
   Default=Process
EndDialog>Dialog1

Show>dialog1
GWP>%title%,psX,psY
Let>DOSx=%psX%-310
Let>DOSy=%psY%+25
Let>VBx=%psX%+295
Let>VBy=%psY%+25

Dialog>Dialog2
   Caption=DOS List
   Width=300
   Height=400
   Top=%DOSy%
   Left=%DOSx%
   Label=DOS Directory List,15,5
EndDialog>Dialog2

Dialog>Dialog3
   Caption=VB List
   Width=300
   Height=400
   Top=%VBy%
   Left=%VBx%
   Label=VB Directory List,15,5
EndDialog>Dialog3

Let>cleartest=0
Let>null=

Label>start
GetDialogAction>Dialog1,r
Wait>0.01
If>r=2,exit
If>r=3,Process
If>r=4,Pick
If>{(%Dialog1.msEdit1%<null>Dialog1.mslabel1=%SPACE%
  ResetDialogAction>Dialog1
  Let>cleartest=0
EndIF
goto>start

Label>Process
If>Dialog1.msEdit1=
  Let>Dialog1.mslabel1=No Directory Selected
  ResetDialogAction>Dialog1
  Let>cleartest=1
  Goto>start
EndIF
IfDirExists>%Dialog1.msEdit1%
  Goto>dirpassed
Else
  separate>%Dialog1.msEdit1%,\,testval
    Let>tk=0
	Let>testdir=
	sub>testval_count,1
	Repeat>tk
	  add>tk,1
	  Let>value=testval_%tk%
	  Concat>testdir,value
	  Concat>testdir,\
	Until>tk,testval_count
    IfDirExists>%testdir%
	  Let>Dialog1.msEdit1=%testdir%
	  Goto>dirpassed
	EndIf
  Let>Dialog1.mslabel1=%Dialog1.msEdit1%%CRLF%Is not a valid directory name
  Let>Dialog1.msEdit1=
  ResetDialogAction>Dialog1
  Let>cleartest=1
  Goto>start
EndIF
Label>dirpassed
LEN>%Dialog1.msEdit1%,chars
Midstr>%Dialog1.msEdit1%,chars,1,lastchar
If>lastchar=\
  //DoNothing
Else
  concat>Dialog1.msEdit1,\
EndIf

If>Dialog1.msCheckBox1=True
  Let>path=%Dialog1.msEdit1%
  GetTime>starttime
  GoSub>DOS
EndIF

If>Dialog1.msCheckBox2=True
  Let>path=%Dialog1.msEdit1%
  GetTime>starttime
  GoSub>VB
EndIF

goto>start

SRT>DOS
Let>RP_WAIT=1
Let>RP_WINDOWMODE=2
Run>cmd /c dir "%path%*.*" /s /ad /b > %TEMP_DIR%~directorylist~.txt
ReadFile>%TEMP_DIR%~directorylist~.txt,list
GetTime>endtime
Show>dialog2
Let>Dialog2.mslabel0=DOS Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF% Generating Directory count,  Please Wait...%CRLF%%CRLF%%list%
ResetDialogAction>Dialog1
ResetDialogAction>Dialog2
Separate>list,%CRLF%,Cval
sub>cval_count,1
Let>Dialog2.mslabel0=DOS Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF%%Cval_count% Directories under %Dialog1.msEdit1%%CRLF%%CRLF%%list%
ResetDialogAction>Dialog2
Let>list=
END>DOS

SRT>VB
VBSTART
Dim fs, outList

Sub GetAllSubFolders(foldername)
  On Error Resume Next
  Dim f, fc
  Set f = fs.GetFolder(foldername)
  Set fc = f.SubFolders
  For Each f in fc
    outList = outList & f.Path & ";"
    GetAllSubFolders foldername & "\" & f.name
  Next
End Sub

Function DoGetAllSubs(foldername)
  Set fs = CreateObject("Scripting.FileSystemObject")
  GetAllSubFolders foldername
  DoGetAllSubs = Mid(outList,1,Len(outList)-1)
End Function
VBEND

VBEval>DoGetAllSubs("%path%"),list
GetTime>endtime
RPL>list,;,%CRLF%,list
Show>dialog3
Let>Dialog3.mslabel0=VB Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF% Generating Directory count,  Please Wait...%CRLF%%CRLF%%list%
ResetDialogAction>Dialog1
ResetDialogAction>Dialog3
Separate>list,%CRLF%,Cval
Show>dialog3
Let>Dialog3.mslabel0=VB Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF%%Cval_count% Directories under %Dialog1.msEdit1%%CRLF%%CRLF%%list%
ResetDialogAction>Dialog3
Let>list=
END>VB

Label>exit

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed May 24, 2006 6:41 am

If you are running the test more than once this script will recreate the VBScript code every time which may slow it down a bit. Try this version instead:

Code: Select all

//Set up VBScript for later use
VBSTART
Dim fs, outList

Sub GetAllSubFolders(foldername)
  On Error Resume Next
  Dim f, fc
  Set f = fs.GetFolder(foldername)
  Set fc = f.SubFolders
  For Each f in fc
    outList = outList & f.Path & ";"
    GetAllSubFolders foldername & "\" & f.name
  Next
End Sub

Function DoGetAllSubs(foldername)
  Set fs = CreateObject("Scripting.FileSystemObject")
  GetAllSubFolders foldername
  DoGetAllSubs = Mid(outList,1,Len(outList)-1)
End Function
VBEND

Let>title=Path Selection
Dialog>Dialog1
   Caption=%title%
   Width=285
   Height=212
   Top=50
   Left=CENTER
   Label=Directory to process,32,88
   Label= ,32,1
   CheckBox=msCheckBox1,Get Directories using DOS,32,32,153,False
   CheckBox=msCheckBox2,Get Directories using VB,32,56,145,False
   Edit=msEdit1,32,104,121,
   Button=Process,32,152,75,25,3
   Button=Cancel,168,152,75,25,2
   Button=Browse,168,104,75,25,4
   FileBrowse=Browse,msEdit1
   Default=Process
EndDialog>Dialog1

Show>dialog1
GWP>%title%,psX,psY
Let>DOSx=%psX%-310
Let>DOSy=%psY%+25
Let>VBx=%psX%+295
Let>VBy=%psY%+25

Dialog>Dialog2
   Caption=DOS List
   Width=300
   Height=400
   Top=%DOSy%
   Left=%DOSx%
   Label=DOS Directory List,15,5
EndDialog>Dialog2

Dialog>Dialog3
   Caption=VB List
   Width=300
   Height=400
   Top=%VBy%
   Left=%VBx%
   Label=VB Directory List,15,5
EndDialog>Dialog3

Let>cleartest=0
Let>null=

Label>start
GetDialogAction>Dialog1,r
Wait>0.01
If>r=2,exit
If>r=3,Process
If>r=4,Pick
If>{(%Dialog1.msEdit1%<null>Dialog1.mslabel1=%SPACE%
  ResetDialogAction>Dialog1
  Let>cleartest=0
EndIF
goto>start

Label>Process
If>Dialog1.msEdit1=
  Let>Dialog1.mslabel1=No Directory Selected
  ResetDialogAction>Dialog1
  Let>cleartest=1
  Goto>start
EndIF
IfDirExists>%Dialog1.msEdit1%
  Goto>dirpassed
Else
  separate>%Dialog1.msEdit1%,\,testval
    Let>tk=0
	Let>testdir=
	sub>testval_count,1
	Repeat>tk
	  add>tk,1
	  Let>value=testval_%tk%
	  Concat>testdir,value
	  Concat>testdir,\
	Until>tk,testval_count
    IfDirExists>%testdir%
	  Let>Dialog1.msEdit1=%testdir%
	  Goto>dirpassed
	EndIf
  Let>Dialog1.mslabel1=%Dialog1.msEdit1%%CRLF%Is not a valid directory name
  Let>Dialog1.msEdit1=
  ResetDialogAction>Dialog1
  Let>cleartest=1
  Goto>start
EndIF
Label>dirpassed
LEN>%Dialog1.msEdit1%,chars
Midstr>%Dialog1.msEdit1%,chars,1,lastchar
If>lastchar=\
  //DoNothing
Else
  concat>Dialog1.msEdit1,\
EndIf

If>Dialog1.msCheckBox1=True
  Let>path=%Dialog1.msEdit1%
  GetTime>starttime
  GoSub>DOS
EndIF

If>Dialog1.msCheckBox2=True
  Let>path=%Dialog1.msEdit1%
  GetTime>starttime
  GoSub>VB
EndIF

goto>start

SRT>DOS
Let>RP_WAIT=1
Let>RP_WINDOWMODE=2
Run>cmd /c dir "%path%*.*" /s /ad /b > %TEMP_DIR%~directorylist~.txt
ReadFile>%TEMP_DIR%~directorylist~.txt,list
GetTime>endtime
Show>dialog2
Let>Dialog2.mslabel0=DOS Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF% Generating Directory count,  Please Wait...%CRLF%%CRLF%%list%
ResetDialogAction>Dialog1
ResetDialogAction>Dialog2
Separate>list,%CRLF%,Cval
sub>cval_count,1
Let>Dialog2.mslabel0=DOS Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF%%Cval_count% Directories under %Dialog1.msEdit1%%CRLF%%CRLF%%list%
ResetDialogAction>Dialog2
Let>list=
END>DOS

SRT>VB
VBEval>DoGetAllSubs("%path%"),list
GetTime>endtime
RPL>list,;,%CRLF%,list
Show>dialog3
Let>Dialog3.mslabel0=VB Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF% Generating Directory count,  Please Wait...%CRLF%%CRLF%%list%
ResetDialogAction>Dialog1
ResetDialogAction>Dialog3
Separate>list,%CRLF%,Cval
Show>dialog3
Let>Dialog3.mslabel0=VB Directory List%CRLF%Ran From %starttime% To %endtime%%CRLF%%CRLF%%Cval_count% Directories under %Dialog1.msEdit1%%CRLF%%CRLF%%list%
ResetDialogAction>Dialog3
Let>list=
END>VB

Label>exit
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

parkerthompson
Newbie
Posts: 8
Joined: Mon May 01, 2006 6:27 pm

Post by parkerthompson » Thu May 25, 2006 11:13 pm

Dick, what a difference even with mtettmar’s revision, It's and excellent example, and illustrates a good point.

With dos’s dir, I’m guessing its one continuous process which just dumps as it goes. Sort of like a train without any stops. I figure if you’re using the VBS script to create an array you may gain some time back instead of reading the file that gets generated by the dos dir.

I’m not sure why the VB piece takes so much over head it must be the way it reads a dir and then publishes the results.

I really like the idea of having Mjtnet include a new function.

I think the reason I keep coming back to Macro Scheduler to automate processes and now to write exe files is, the program has a really great help system, an excellent debugger, its straight forward and forgiving. In my mind trying to limit the need for VB will only add to its excellent qualities.

Regards,
Frank

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