Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA in Excel to Shell Bat file and Wait for finish 2

Status
Not open for further replies.

PBAPaul

Programmer
Aug 3, 2002
140
GB
I have a batch file C_Dir.Bat that has the following script. It simply gets all the directories in C and pipes it to a text file.
Code:
Dir C:\ /S/B/AD > C:\C_Dir.txt

I searched for threads containing "Shell Wait" and I have tried numerous techniques shown but nothing seems to work as the batch will not operate properly.

I have tried the following:
Code:
Public Sub ExecCmd(cmdline$)
 Dim proc As PROCESS_INFORMATION
 Dim start As STARTUPINFO
 Dim ReturnValue As Integer

 start.cb = Len(start)

 ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

 Do
   ReturnValue = WaitForSingleObject(proc.hProcess, 0)
   DoEvents
 Loop Until ReturnValue <> 258

 ReturnValue = CloseHandle(proc.hProcess)
End Sub

Sub example()

ExecCmd "C:\Documents and Settings\Paul\My Documents\My Bits & Pieces\C_Dir.bat"

End Sub

If I step through the macro then I get a flash of the black command screen then I see a ReturnValue = 0 and so it stops!

Please can someone tell me what I am doing wrong?
 
Why don't you use the File System Object and get the subfolders of the C: root folder yourself? You'll need to set a reference for the Microsoft Scripting Runtime.

Dim objFS as FileSystemObject
Dim objFolder as Folder
Dim objSubFolder as folder

set objFS = new FileSystemObject
set objFolder = objFS.GetFolder("C:\")
for each objSubFolder in objFolder.subfolders
msgbox objSubFolder.name
next objSubFolder
 
Thanks jcrater

Using the batch file, I am able to get ALL directories in C:. Your code only gets the first level directories.

I have used a recursive script to get the first level directories, then get any second level of these first level, then third level etc.

MS XP & Office XP have ten levels of directory and so running a recursive script takes much longer than running the batch file and then reading in the folder names.

Thanks for the suggestion.
 
And what about this ?
Cmd = """C:\Documents and Settings\Paul\My Documents\My Bits & Pieces\C_Dir.bat"""
Set sh = CreateObject("WScript.Shell")
RC = sh.Run(Cmd, 1, True)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PH

Hace a star! So simple - I knew that the command string must be enclosed in quotes, but as the string is already in quotes I thought that was it!

Now works perfectly.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top