run 2 dos commands using vbScript as a single command

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 02:18:46


Hi,
I am trying to change the directory and then run another command all
in 1 line. But for some strange reason I can't even get the first
command to work. I need help!!

SA


Here's the code
===========
dim objShell
dim inFile, outFile, projFile,folderC ,cmd1,cmd2

'setting variuos paths
folderC ="C:\temp\Projects\21038Ax1aDx"
inFile = "C:\Documents and Settings\smitha\My Documents\BOM
\partlist.ini"
outFile = "out.lst"
projFile = "10297200"

'2 commands
cmd1= "cd folderC"
cmd2="partlist projFile -input inFile -outFile outFile"

set objshell = createobject("Wscript.shell")
objshell.Run "%compsec% /k cmd1 & cmd2"
 
 
 

run 2 dos commands using vbScript as a single command

Post by MV » Sun, 07 Sep 2008 02:28:17


Replace the line
objshell.Run "%compsec% /k cmd1 & cmd2" with this line:
wscript.echo "%compsec% /k cmd1 & cmd2"

and you will immediately see that the command cannot possibly run, because
"cd folderC" gets lumped together with "partlist".

You probably have a second problem too. Where is the command processor
supposed to find "partlist"? In the current folder? What is the current
folder? In scripts you MUST state the drive, folder, file name and
extension!

 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 02:55:01


Partlist command works from anywhere in the cmd window. But this needs
to be in the directory just for the -input attribute to work correctly
as it is a custom program.

I tested this exact command in the cmd window with "&" and it works
fine. I am perplexed as to how to solve this problem.

SA
 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 03:07:46

On Sep 5, 10:55m, " XXXX@XXXXX.COM " < XXXX@XXXXX.COM >






Oops I forgot to write down the command I used in the Command Console
C:\>cd "C:\temp\Projects\21038Ax1aDx" & partlist "10297200" -input "C:
\Documents and Settings\sragarwal\My Documents\BOM\partlist.ini" -
output "out.lst"


But in my script I want to be able to use variables as the values will
change based on user inputs.

THanks,
SA
 
 
 

run 2 dos commands using vbScript as a single command

Post by MV » Sun, 07 Sep 2008 03:34:42


On Sep 5, 10:55 am, " XXXX@XXXXX.COM " < XXXX@XXXXX.COM >






Oops I forgot to write down the command I used in the Command Console
C:\>cd "C:\temp\Projects\21038Ax1aDx" & partlist "10297200" -input "C:
\Documents and Settings\sragarwal\My Documents\BOM\partlist.ini" -
output "out.lst"


But in my script I want to be able to use variables as the values will
change based on user inputs.

THanks,
SA

====================

Running commands with embedded spaces in the command line and the parameters
is an FAQ with lots of examples on Google. The easiest way to deal with them
is to start with a simple command without a parameter, then add a parameter,
then add some spaces to the parameter and so on. In other words, try to walk
before you run. By joining two console commands you actually tried to leap
straight away!
 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 03:47:46

n Sep 5, 11:34m, "Pegasus \(MVP\)" < XXXX@XXXXX.COM > wrote:

You are right. Following on your advice I tested the simple code below
and I got the following error:

C:\test.vbs(3, 1) (null): The system cannot find the file specified.

Please shed some light.

Thanks,
SA

dim objShell
set objshell = CreateObject("WScript.Shell")
objshell.Exec "%compsec% /k dir"


 
 
 

run 2 dos commands using vbScript as a single command

Post by Tom Laveda » Sun, 07 Sep 2008 03:57:32

n Sep 5, 2:47m, " XXXX@XXXXX.COM " < XXXX@XXXXX.COM >
wrote:

Typo ...

objshell.Exec "%comPSec% /k dir"

should read ...

objshell.Exec "%comSPec% /k dir"

BTW, the major source of your problem is with concatinating variables
to build a string. For example, ...

cmd1= "cd folderC"

should be ...

cmd1= "cd " & folderC

This concatenates the contents of the variable folderC to the string
"cd " to create the desired result. Otherwise, the contents of the
variable cmd1 is just the literal string "cd folderC", which is not
the desired result.

There are many more instances of this problem in the script you
posted.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 04:29:51

n Sep 5, 11:57m, Tom Lavedas < XXXX@XXXXX.COM > wrote:
>> > should be ...> >> > cmd1= "cd " & folder>
> This concatenates the contents of the variable folderC to the strin>
> "cd " to create the desired result. therwise, the contents of th>
> variable cmd1 is just the literal string "cd folderC", which is no>
> the desired result>
> There are many more instances of this problem in the script yo>
> posted>
> Tom Laveda>
> ===========http://members.cox.net/tglbatch/wsh/- Hide quoted text >
> - Show quoted text -


Thank you Tom. All your suggestions were very very useful. I know now
--- why the dir command was not working. I am inexperienced with
vbScript but believe me I am learning (and I did figure out the
literal string issue)

So how would I use a variable instead of a direct command:

dim objShell
cmd1 ="cd C:\temp\Projects\21038Ax1aDx"
set objshell = CreateObject("WScript.Shell")
objshell.run "%comspec% /k cmd1"

 
 
 

run 2 dos commands using vbScript as a single command

Post by MV » Sun, 07 Sep 2008 04:52:18

< XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
On Sep 5, 11:57 am, Tom Lavedas < XXXX@XXXXX.COM > wrote:


Thank you Tom. All your suggestions were very very useful. I know now
--- why the dir command was not working. I am inexperienced with
vbScript but believe me I am learning (and I did figure out the
literal string issue)

So how would I use a variable instead of a direct command:

dim objShell
cmd1 ="cd C:\temp\Projects\21038Ax1aDx"
set objshell = CreateObject("WScript.Shell")
objshell.run "%comspec% /k cmd1"

===================

Perhaps like so:
cmd1 ="cd C:\temp\Projects\21038Ax1aDx"
set objshell = CreateObject("WScript.Shell")
objshell.run "%comspec% /k " & cmd1

As I suggested before: using wscript.echo instead of objshell.run is a
wonderfully simple and highly effective way of debugging this type of
problem. Use it!


 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 05:19:06

n Sep 5, 12:52m, "Pegasus \(MVP\)" < XXXX@XXXXX.COM > wrote:

THank you Pegasus. I took your suggestion and it worked!!

THanks,
SA
 
 
 

run 2 dos commands using vbScript as a single command

Post by Tom Laveda » Sun, 07 Sep 2008 05:43:17

On Sep 5, 4:19m, " XXXX@XXXXX.COM " < XXXX@XXXXX.COM >



{snip}

Remember, you need to concatenate the strings, not put them together
as literals. That's what Pegasus is trying to get you to see.

So, to do what you first wanted to do - two commands executed in a
single Run line, it would look look this ...

objshell.run "%comspec% /k " & cmd1 & "&" & cmd2

Tom Lavedas
===========
http://www.yqcomputer.com/
 
 
 

run 2 dos commands using vbScript as a single command

Post by karsagarwa » Sun, 07 Sep 2008 06:24:10


Yes. I got it. Thanks a lot. Could not have done it without your help.

Thanks again,
SA