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

Executing dos commands in vbscript

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I am trying to write a vbscript file that I can run from the command prompt using cscript.

Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("c:")
Set objScriptExec = objShell.Exec("cd \")
Set objScriptExec = objShell.Exec("dir /w")
strIpConfig = objScriptExec.StdOut.ReadAll
WScript.Echo strIpConfig

For some reason it is erroring on line 2 saying access denied. I have full admin rights over my PC.

Any ideas?

Thanks very much

Ed
 
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("CMD /C c: & cd \ & dir /w")
strIpConfig = objScriptExec.StdOut.ReadAll
WScript.Echo strIpConfig

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hey PHV, would your .Exec command need to be..

Code:
Set objScriptExec = objShell.Exec("CMD /C c:" & vbCRLF & "cd \" & vbCRLF & "dir /w")

needing the extra line feeds between the commands?



Thanks

John Fuhrman
Titan Global Services
 
No, the ampersand is interpreted as a command separator by cmd.exe

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

so something like this..

"c:\temp\inst\InstData\VM\install.exe" & " -f " & "installer.properties"

would get interpreted as..

c:\temp\inst\InstData\VM\install.exe CRLF
-f CRLF
installer.properties CRLF

correct??



Thanks

John Fuhrman
Titan Global Services
 
If the ampersand is interpreted by the script engine, then it acts as the concatenation operator.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ahhhhh, I see says the blind man!!!

Because the whole string value [highlight]("CMD /C c: & cd \ & dir /w")[/highlight] is between quotes the ampersands are sent to the command interpreter which then uses them as CRLF's between each command.


Thanks that did clarify! PVH

Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top