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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing a DOS command from VBScript

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello everyone-
I am trying to execute a DOS command from a VBScript file. I basically want to be able to build a string and then execute it. For some reason my code will not work with my string. Let me just show you my code, it will make more sense.

Code:
DIM objShell
DIM iReturn
set objShell = wscript.createObject("wscript.shell")

dim command
command = "CMD /C DIR p:\ /s > "
dim path
path = "C:\Documents and Settings\User\Desktop\"
dim fileName
fileName = "FILE.TXT"
dim finalCommand
finalCommand = command & path & fileName
''so at this point the finalCommand is this:
''"CMD /C DIR p:\ /S > C:\Documents and Settings\User\Desktop\FILE.TXT"


iReturn = objShell.Run(finalCommand, , True)

The above code does not work. When I hard code that line in there, as in below it works fine:

Code:
DIM objShell
DIM iReturn
set objShell = wscript.createObject("wscript.shell")
'iReturn = objShell.Run("CMD /C DIR p:\ /S > C:\Documents and Settings\User\Desktop\FILE.TXT", , True)

Does anyone know how to get it to work the first way, so that I can build the command I want to execute into a string?

Thanks!
-Mark
 
Nevermind, I figured it out. It has something to do with permissions. I was able to use my code above and place the text file in C:\Temp instead. Thanks anyway.
 
basically you are trying pipe the results to a file that has a space in the path statement, if you amend the finalcommand statement it should work:

finalcommand = command & """" & path & filename & """"

or you could simply use

command = "CMD /c DIR p:\ /S > ""C:\Documents and Settings\User\Desktop\FILE.TXT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top