PDA

View Full Version : How to run DOS commands in Run Script


jmm
08-18-2004, 07:46 PM
I have written a Run Script step (in VBScript) to emulate our existing DOS batch file based build system. I assumed that since the DOSCMD macro shows up via the "Insert Macro" button, that I could use it to execute DOS commands within my VBScript code. But when I try this:

%DOSCMD% "echo hello"

I get:

Error at Line 327, Column 4 (Expected statement)
Code: C:\WINDOWS\system32\cmd.exe /C "echo hello"

Do I have to use a "Run Program" step to execute DOS commands? If so, how do I do that programatically within my VBScript?

kevina
08-19-2004, 04:01 PM
The recommended way would be to use the Builder.RunProgramEx method.

Example: Builder.RunProgramEx "calc.exe"

See the help file for details (also available online at: http://www.kinook.com/VisBuildPro/Manual/?runprogramexmethod.htm)

jmm
09-02-2004, 07:47 PM
Thanks for the reply!

First, when I search the VBP Forum for the keyword "RunProgramEx", why do I get no results?

Second, while I can run calc.exe in your example without problem, I can't use Builder.RunProgramEx the way I need to. I get what I believe to be file-exclusively-opened access problems with the bat-file I wish to run via Builder.RunProgramEx. In the following sub I create a bat-file to execute using Builder.RunProgramEx:

sub RunCmds(cmds, ProjectDir)

Dim MyFso, tf, BatFile, DosCmd

BatFile = "c:\build\RunBuild2.bat"
Set MyFso = vbld_FSO()
Set tf = MyFso.CreateTextFile(BatFile, true)
tf.Write("cd " & ProjectDir & vbCrLf & cmds)
tf.Close

DosCmd = BatFile
Builder.RunProgramEx DosCmd

end sub

The first time I run the job, I get this rather quiet error:
...
1 of 1 project(s) updated
9/2/2004 5:25:18 PM: Build successfully completed.
Error at Line 318, Column 2
Step 'Parse top-level build.bat' failed
Build ended.

Note: Line 318 is "Builder.RunProgramEx DosCmd".

Now when I run it again I get this error:
...
1 of 1 project(s) updated
9/2/2004 5:35:55 PM: Build successfully completed.
Error at Line 312, Column 2 (Permission denied)
Step 'Parse top-level build.bat' failed
Build ended.

Note: Line 312 is "Set tf = MyFso ...".

If I try to delete or change the bat-file outside of VBP, I get access denied errors until I exit VBP.

kinook
09-02-2004, 10:35 PM
When I search the forums for RunProgramEx, I get one result: this thread.

Batch files must be executed under the command shell (see 'Run Program Action' in the help index for details). Try

DosCmd = "%DOSCMD% call """ & BatFile & """"

jmm
09-03-2004, 12:52 PM
I am confused by this post. As the thread title implies, I am trying to run a DOS command from within VBScript. How would I use 'Run Program Action' from within VBScript?

Your previous reply was to use the Builder.RunProgramEx method. That solution seems to work at a basic level. I was seeking help re file-exclusively-opened access problems in creating and using the bat-file in the Builder.RunProgramEx method. See my posting from 9/2/04 for details.

kinook
09-03-2004, 01:35 PM
The reference to the Run Program Action help topic was only pointing out where to look for a more detailed explanation of how to invoke batch files from a Run Program Action or RunProgramEx.

The permission denied / access error is most likely occurring because you aren't invoking the batch file like you need to, leading to an error which prevents the .Close code from being executed. Again, try:

DosCmd = "%DOSCMD% call """ & BatFile & """"

instead of

DosCmd = BatFile

in your script code.