0

I have to run a VB script which has 2 arguments. So I am running a command below.

Delete_Dummy1.vb
s C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv C:\Users\c6342\Deskto
p\XML_to_CSV\Temp_Files\xml1.txt
**VB Script Sample - not working:** 
**sourceloc** = WScript.Arguments.Item(0)
**destloc** = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = objFSO.OpenTextFile("**sourceloc**")
Set newFile = objFSO.CreateTextFile("**destloc**")

It throws the error as file not found. But if I hard code the sourceloc and destloc and remove the arguments it is working fine. it is throwing error only when i am using arguments.

**Working VB Script sample:**
Set oTextStream = objFSO.OpenTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv")
Set newFile = objFSO.CreateTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt")

This works fine. But as per my project requirement, I cant hard code these file locations. I can pass as parameters from command.

4
  • Sorry Please ignore ** in the code snippets. I just highlighted important things with **. Commented Feb 25, 2016 at 14:22
  • You need to wrap the arguments with spaces in with double quotes (").
    – user692942
    Commented Feb 25, 2016 at 14:23
  • I tried with double quotes also. But anyways there are no spaces in my source and destination file locations. Commented Feb 25, 2016 at 14:32
  • I understand you saying enclosing each value in quotes didn't work? Check out my answer below I go into a bit more detail about how the space is being interpreted.
    – user692942
    Commented Feb 25, 2016 at 14:35

1 Answer 1

2

After a length discussion in the comments think it will be best to just update my answer.

The reason for the Arguments not working is you never pass them to the OpenTextFile() and CreateTextFile() methods. Instead you are passing literal strings containing the variable name instead of the actual variables.

sourceloc = WScript.Arguments.Item(0)
destloc = WScript.Arguments.Item(1)

Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Pass the variables not a string literal
Set oTextStream = objFSO.OpenTextFile(sourceloc)
Set newFile = objFSO.CreateTextFile(destloc)

As it stands VBScript keeps trying to locate a file called sourceloc and destloc instead of the actual file names from the passed Arguments collection. Which is what likely causes the

Microsoft VBScript Runtime Error: File Not Found


Note: Below is based on initial question which has since been revised.


This comes down to how you are passing the arguments to the script, any spaces in the values will be treated as new arguments. At the moment this is how the arguments are passed;

0. C:\Users\enter
1. code
2. herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
3. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt

I'm sure this isn't what you expect. To avoid this enclose each argument in double quotes ("...").

Delete_Dummy1.vbs "C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv" "C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt"

That way you get more what you expected

0. C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
1. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt
13
  • Delete_Dummy1.vbs "C:\Users\chandu\Desktop\XML_to_CSV\Temp_Files\xml.csv" "C:\Users\chandu\Desktop\XML_to_CSV\Temp_Files\xml1.txt" I tried this. but still not working. Commented Feb 25, 2016 at 14:40
  • @chandrasekhar I can only go off what you posted, either way using double quotes is always a good idea to avoid these types of problems. I'm not sure what posting that line as a comment accomplishes without an explanation? This is what you initially posted Delete_Dummy1.vb s C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv C:\Users\c6342\Deskto p\XML_to_CSV\Temp_Files\xml1.txt. I'm guessing the enter code herec6342 was a typo, that probably caused your original File Not Found error.
    – user692942
    Commented Feb 25, 2016 at 14:42
  • Sorry. my user name is c6342. after posting my question, stackoverflow automatically converted c6342 into enter code herec6342. that's why i removed that code and replaced with chandu while commenting to avoid confusion. Commented Feb 25, 2016 at 14:52
  • @chandrasekhar If you want to do that you would be better editing the question.
    – user692942
    Commented Feb 25, 2016 at 14:59
  • 1
    Thanks @Lankymart. It works... Thanks for your time and help. Commented Feb 25, 2016 at 16:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.