1

i am trying to pass the arguments from the batch file to vb script file.

set InputFile ="C:\Temp\InputFile_mod.csv"
set outPutFile = "C:\Temp\outputFile.dat"

i tried to pass the variables as below but i am not getting anything to the vbscript file.

cscript SFEVBMacro.vbs  InputFile outPutFile

but if i pass direct path of the files like below it is working fine

cscript SFEVBMacro.vbs  C:\Temp\InputFile_mod.csv C:\Temp\outputFile.dat.

is there anything wrong in passing the arguments with variables?

VBSCRIPT Code:

Set args = WScript.Arguments
inputFile = WScript.Arguments.Unnamed(0)
createdFilePath = WScript.Arguments.Unnamed(1)

Sub ReadCSVFile()
   Set objFileToRead = objFSO.OpenTextFile(inputFile,1)
   REM Read the csv file
End Sub
ReadCSVFile

can anyone can help please.

2
  • Ryan I added vbscript code. Commented Aug 29, 2016 at 7:07
  • Remove the spaces around the = sign in the set command lines as they become part of the variable names and values otherwise... when expanding (reading) variables you need to put its name in between a pair of % signs... Commented Aug 29, 2016 at 7:39

2 Answers 2

2

There are two problems in your code. The first one is the variable assignment

set InputFile ="C:\Temp\InputFile_mod.csv"
             ^
set outPutFile = "C:\Temp\outputFile.dat"
              ^ ^

The spaces indicated are included in the variable name (when placed in the left side of the equal) and in the value stored (right side of the equal)

It is a good habit to quote value assignment to prevent problems with special characters, but the recomended syntax is

set "InputFile=C:\Temp\InputFile_mod.csv"
set "outPutFile=C:\Temp\outputFile.dat"

where quotes protect the assignment, there are no spaces around the equal sign and note that, with this syntax, the quotes are not stored in the variable value.

The second problem is how you use the variables. Your vbscript code does not expect two variable names, but two file names. So, you don't have to pass the variable names, but the values stored inside them. In batch files this is done using %varname%.

set "InputFile=C:\Temp\InputFile_mod.csv"
set "outPutFile=C:\Temp\outputFile.dat"

cscript SFEVBMacro.vbs "%InputFile%" "%outPutFile%"

The batch parser will replace the variable read operation with the value inside the variable and then execute the command.

If you prefer to pass the variable names (your original code) instead of the variable values, you can do it, but then your vbscript should be changed to retrieve the variables contents

With WScript.CreateObject("WScript.Shell").Environment("PROCESS")
    inputFile = .Item( WScript.Arguments.Unnamed(0) )
    createdFilePath = .Item( WScript.Arguments.Unnamed(1) )
End With

As the code receives environment variable names, we need to use the Environment property of a WScript.Shell instance to retrieve the variable contents from the process environment.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem might be in the quotes. Try to surround the first two lines you posted with double quotes instead of the strings.
This might help, as if you are passing the filepaths without quotes it works, but in the first example, you set them with the quotes.
Other way would be to trim the double quotes when the parameters get into the vbscript.

2 Comments

I tried but no luck. but while passing the variable i observed that variable name is passed not the value of it.
Oh... Yeah :D You have to put % around the variable names: %varname%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.