2

I've got a problem with some VB scripting - it doesn't seem like it should be terribly difficult to solve, but even after trudging through many a page of Google I have yet to find a solution.

[The problem]

Here is my python file (test.py), simplified to just show the problem:

f = open("testing.txt", 'w')
f.write("oh hai\n")
f.close()

Of course, when run directly from the command line, this generates the file as you'd expect.

However, when run in a simple .vbs script (WARNING: My vbs skills are lacking. This is probably why I am having a problem. So far I haven't had many issues, apart from hating life from using XP to code when I'm used to using vim)

Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "C:\Python27\python test.py"
WshShell.Run cmd

no output file is generated! At all! It's infuriating, as when I input that exact command ("C:\Python27\python test.py") into the run program from the start menu, it works!

[System info]

At work, so they're on Windows XP. Everything else is pretty standard, or so I'm lead to believe.

EDIT: Changed "C:\Python27\testing.py" to just "testing.py". This was left over from when I was trying to solve it, and thought maybe it was putting the files somewhere outside of the destination folder.

0

2 Answers 2

3

First, your Python script looks suspicious, I doubt the backslashes work in a simple string. At least, in my test, it didn't work, I just replaced them with forward slashes.

Next, you can see the error message by prepending cmd with cmd /k, the run window remains on screen. You can see the .py file isn't found, because it isn't in the current directory. You have to specify an absolute path to this file, perhaps by getting the current path from the script.

[EDIT] I finally got a working code (my VBS is a bit rusty...)

Dim wshShell, fso, loc, cmd

Set fso = CreateObject("Scripting.FileSystemObject")
loc = fso.GetAbsolutePathName(".")
WScript.Echo loc

'~ cmd = "%ComSpec% /k C:\Languages\Python\python.exe " + loc + "\test.py"
cmd = "C:\Languages\Python\python.exe " + loc + "\test.py"
WScript.Echo cmd

Set wshShell = CreateObject("WScript.Shell")
wshShell.Run cmd

You can also check the arguments if a path is provided:

if WScript.Arguments.Count = 0 then
    loc = fso.GetAbsolutePathName(".")
else
    loc = WScript.Arguments(0)
end if

Such script is better run with cscript instead of default wscript.

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

3 Comments

Ah yes, that was a left over from when I was seeing if adding the full listing made it work! It didn't. I'll edit that out. Give me a moment and I'll try your solution :)
Interestingly, I get the error message "can't open file C:\Documents". How do I get it to include the spaces do you think? The folder name is "Documents and Settings". Good ol' MS having spaces in file names.
Solved! It was a combination of your solution and Tomalak's solution. Thank you very much for your help :D
0

Try

f = open("C:\\Python27\\testing.txt", 'w')

instead of your first line.

2 Comments

There is no backslash escaping in VBScript.
Ah! *lol*, forget it. ;-) (I just wonder why it works when called stand-alone, then…)

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.