开发者

Windows 7 VBS generated batch file will not start

开发者 https://www.devze.com 2023-03-20 14:25 出处:网络
I\'m using VBS to build a batch file for installing updates.To make sure it works开发者_如何转开发 the way I expected it to, I built this little test script, however the generated batch file errors ou

I'm using VBS to build a batch file for installing updates. To make sure it works开发者_如何转开发 the way I expected it to, I built this little test script, however the generated batch file errors out on launch with an unrecognized character, no matter what the first character is. VBS script:

option explicit

Dim objFSO, objFile, objWShell

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("Test.bat", 2, True)
Set objWShell = CreateObject("WScript.Shell")

objFile.Write("@echo off" & vbCrLf & "echo " & chr(34) & "this is a test" & chr(34) & vbCrLf & "echo " & chr(34) & "This is only a test" & chr(34) & vbCrLf & "timeout /t 5")
Set objFile = Nothing

objWShell.Run "Test.bat"

wscript.quit

Running the script creates and instantly closes the window the bat runs in, but launching the generated bat from a command line yields that the first command being passed (in this case @echo off, but have tried with others) includes an extra character at the beginning, and only passes that character (which appears as a solid square, presumably one windows doesn't know) and the first character of the first command in the batch file. Thus I get something similar to '▬@ is not a recognized internal or external command, operable program or batch file.'

Selecting "Show all characters" in npp when viewing both the batch file and the generating script shows no extra characters and all line breaks in both are crlf. Any ideas?


Changing the last parameter of CreateTextFile to False makes the file ASCII and it then runs properly. (You're currently saving it as Unicode which doesn't usually play nice with command prompt)

0

精彩评论

暂无评论...
验证码 换一张
取 消