INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a
Computer / IT professional?
Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Do you enjoy these
Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Folders and Files
How do get a recursive list of files, and wait for a DOS prompt to finish. (This easy way)
Posted: 17 Aug 00
How do I get a recursive list of files in any simple way?
Use the DOS "dir /A:-D /B /S" command.
How do I get my program to wait until the DOS prompt is finished before continuing without the extra code?
Use the example I will provide in this simple FAQ.
The other day I posted a question for discussion that deals with figuring out how get a Visual Basic program to recursively list all the files in a directory and its subdirectories. We came across a couple painful solutions to it, and there was the ever-present DOS "dir" command. I figured that it's faster with that to get a list of files than with any of the other solutions, not to mention, less mind-boggling. My biggest problem was with the program not waiting for the command prompt to finish execution, which I found a simple, easy solution to as well. But anyways, here's my quick and easy solution: (I did this in VB6 under NT/2000 btw, but it should work basically the same in 95/98)
Objects you'll need:
The VBA.FileSystem Objects
1. Global Variables
You'll need a global variable or some variable to store a "ticker" for the timer which I will explain later.
In your Module1 (or where you put your global vars):
Global TimeTicker as numericaltype
Set numericaltype to Integer or Long if you don't know what to do about it.
2. The Form
All you need to do here is put a new timer into the form, which will be our wait timer.
Set Timer1.interval = 1000* in it's
properties (You may have to change this if the
program you're running in DOS takes a long time
before it puts output on the screen. *1000 = 1
3. The Subroutines
Your program only needs one routine for this operation, but if you want to or have to, you can split the code up a little.
First of all is the Timer. This is really easy. All you do is put this into the Timer1 routine:
Private Sub Timer1_Timer()
TimerTicker = TimerTicker + 1
'If you don't plan on disabling the timer after the
'Routine is done with it, you will need to add the
'line below. This will prevent an overflow of the
If TimerTicker > 1000000 then TimerTicker = 0
Next you will need a function to run until TimerTicker reaches a certain, specified value. Do so by doing this:
Private Sub WaitTimer (TimeWait)
' TimeWait, if the interval is set to 1000 will be
' in seconds. If so this will wait for about
' TimeWait seconds depending on when the
' function is called. Disabling the timer until use
' will make this run more accurately.
Do Until TimeTicker >= TimeWait
Once you have that done you need to write a routine or function to run the DOS prompt and wait for the prompt to finish completely. To do this we have to have the program called from the command prompt to send it’s output to a file. (Remember: I was writing this program to recursively retrieve a list of files underneath a certain directory and it’s sub-directories) We will do this in a special way so that even when the command prompt is closed, it will not continue until all the data from the program is finished writing to a the file. If the program doesn’t put data into a file very fast, you may have to increase the interval of the timer to compensate for the delays. Here’s how I did this:
Private Sub Command1_Click ()
'Disable the button until finished
Form1.Command1.Enabled = False
'set our output file for the data from the prompt
OutputFile = MyPath + “filename.txt”
'set the path of where we want the file list to
'start it's recursive listing.
PathOfDirectory = “c:\windows\”
'First delete any old instance of the output file
'To safeguard any error, have the program rewrite
'the file which will make a new file if the file
Open OutputFile For Output As #99
Print #99, "Deleting"
'Delete the file.
'Here’s a tricky part. This calls the command
'Prompt to run a program and exit when finished.
'Run a command prompt on your own time and type:
' for 95/98 users “help command”
' for NT/2k users “help cmd” (or just use my
' example since it’s done in NT/2k)
'put the command to be called by the shell in
'this variable below. The command I ran is:
' “dir /A:-D /B /S [I]D:\Path\*.*[/I]
'This command will recursively list all the files
'in the directory and it’s subdirectories and not
'the directory names (/A:-D = No directories)
'Putting the “> “+OutputFile at the end of the
'command will output to the OutputFile.
ShellScript = "cmd /C dir /A:-D /B /S " + _
PathOfDirectory + "*.* > " + OutputFile
'Call the shell command
'Now wait here until an output file starts to be
'Change 8 to whatever you want as long as it's
'greater than 0
If fs.FileExists(ProgramSettingsPath _
+ "FileList.txt") And If FileLen _
(ProgramSettingsPath + "FileList.txt") _
> 8 Then Exit Do
'Now wait until the file doesn’t change for a couple
'seconds. Since this is a “dir” command, the output is
'instant and doesn’t have any pauses in output. Change the
'wait timer to a larger number so that it waits longer
'before checking to see if there is more data being put in
OldLenOfFile = 0
Open OutputFile For Input As #111
LenOfFile = LOF(111) 'check length of file.
If LenOfFile = OldLenOfFile Then Exit Do
'If there isn’t anymore data, then exit
OldLenOfFile = LenOfFile
'Add anything you want to run after the routine is
'over in here, or call another routine.
Add in anything at the end of the routine that you want and you're done. You can run the program and it will call a command prompt, write a list of the files or an output from a program you decided to use instead, and wait until the listing is done being written.
Back to Visual Basic(Microsoft): Version 5 & 6 FAQ Index
Back to Visual Basic(Microsoft): Version 5 & 6 Forum
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
- Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close