在vbscript中如何运行bat文件

时间:2013-09-26 11:43:25

标签: batch-file vbscript batch-processing

我使用vbscript创建了bat文件。并运行该脚本但批处理文件未运行请帮助我

Const ForReading=1, ForWriting=2, ForAppending=8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oServiceManager = CreateObject("com.sun.star.ServiceManager") 
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop") 
Dim aNoArgs() 
Dim oDoc, myrows,inut, s, shell 
s = 1
Set outFile = objFSO.CreateTextFile("C:\Program Files\WinSCP\" & "\Build.bat", True)
outFile.Close
Set outFile = objFSO.OpenTextFile("C:\Program Files\WinSCP\" & "\Build.bat", ForWriting, True)
outFile.WriteLine chr(34) & "C:\Program Files\WinSCP\winscp.exe" & chr(34) & " /console /script=page.txt"
outFile.Close
Set outFile = objFSO.CreateTextFile("C:\Program Files\WinSCP\" & "\Page.txt", True)
outFile.Close
Path = InputBox ("Enter Your Path:")
inut = "file:///" & Path
Set oDoc = oDesktop.loadComponentFromURL(inut, "_blank", 0, aNoArgs) 
oDoc.CurrentController.Frame.ContainerWindow.setVisible(false) 
set oSheet = oDoc.Sheets.getByName("Sheet1") 
set oCell = oSheet.getCellByPosition( 3, 2 ) 'A2
DomainName = oCell.getString() 
set oCell = oSheet.getCellByPosition( 3, 3 ) 'A2
nFile = oCell.getString() 
nFile = nFile - 1
Set outFile = objFSO.OpenTextFile("C:\Program Files\WinSCP\" & "\Page.txt", ForWriting, True)
outFile.WriteLine "option confirm off"
outFile.WriteLine "open sftp://root@" & DomainName & " -hostkey=" & chr(34) &"ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"& Chr(34)
outFile.WriteLine "option transfer binary"
For i = 0 To nFile
set oCell = oSheet.getCellByPosition( 0, s ) 'A2
nValue = oCell.getString() 
set oCell = oSheet.getCellByPosition( 1, s ) 'A2
nsValue = oCell.getString() 
outFile.WriteLine "put " & nValue & " " & nsValue
s = s+1 
Next
outFile.WriteLine "# Disconnect"
outFile.WriteLine "# close"
outFile.Close
oDoc.Close(true) 
msgbox "Done"
set shell=createobject("wscript.shell")
shell.run "C:\Program Files\WinSCP\" & "\Build.bat"
set shell=nothing

我使用vbscript创建了bat文件。并运行该脚本但批处理文件未运行请帮助我

编辑:

Line: 49 Char: 1 Error: The system cannot find the specified Code: 80070002 Source: (nul)

5 个答案:

答案 0 :(得分:3)

我通过使用在我的机器上工作 shell.run“”“C:\ Program Files \ WinSCP \ Build.bat”“”  原因:程序文件有空格,因此必须用引号括起来 - 因此两端需要3个引号。

答案 1 :(得分:1)

构建路径时应使用BuildPath方法。它避免了使用路径分隔符的麻烦。

>>> Set fso = CreateObject("Scripting.FileSystemObject")
>>> WScript.Echo fso.BuildPath("C:\some\folder", "file.ext")
C:\some\folder\file.ext
>>> WScript.Echo fso.BuildPath("C:\some\folder\", "\file.ext")
C:\some\folder\file.ext

另外,连接像这样的字符串文字是没有意义的:

"C:\Program Files\WinSCP\" & "\Build.bat"

当你在其中没有任何变量时,只需将它设为一个字符串:

"C:\Program Files\WinSCP\Build.bat"

但上述两种情况都不是您获得错误的原因。 Windows可以很好地处理重复的路径分隔符。

找不到您尝试执行的文件,因为该路径包含空格,因此您的代码实际上尝试使用参数{执行文件C:\Program(不存在){ {1}}。

你需要在路径周围放置双引号以防止这种情况,或者使用文字双引号(必须加倍才能在字符串中转义它们):

Files\WinSCP\\Build.bat

或通过将字符串与ASCII字符34连接:

shell.Run """C:\Program Files\WinSCP\Build.bat"""

在函数中放置用于创建双引号字符串的代码通常很有帮助:

shell.Run Chr(34) & "C:\Program Files\WinSCP\Build.bat" & Chr(34)

并使用如下函数:

Function qq(str)
  qq = Chr(34) & str & Chr(34)
End Function

答案 2 :(得分:1)

我最近这样做是为了从vbscript创建一个批处理文件(对于winscp!),这就是我做的。

strProcessToKill = "winscp.exe"

'Sets "shell" to run a shell command
sub shell(cmd)
'Run a command as if you were running from the command line
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub

'Creates Sync.bat, which is called at the end of the script.(This is what passes the      FTP parameters to the WinSCP Program)
SET objFSO = CreateObject("Scripting.FileSystemObject")

'Open write stream
SET outFile = objFSO.CreateTextFile("C:\RDSync\sync.bat", True)

'Write to Batch File
outFile.WriteLine "@echo off"
outFile.WriteLine "cd %programfiles%\winscp\"
outFile.WriteLine "start /b winscp.exe /log=""C:\RDSync\logs\!M-!D-!Y@!T.xml""     /xmlgroups /command ""open ftp://username:password@ftp.foo.com"" ""synchronize local -delete -criteria=size" & + " " + """""" + RDfolder + """""" + " " + "/"
outFile.WriteLine "exit"

'Close Write Stream
outFile.Close

'Allows script to access Running Processes"
SET objWMIService = GETOBJECT("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

'set variable to Winscp Process Instance
SET colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

'Checks to see if an instance of WinSCP.exe is running, and if it is, it closes it.   This will loop to close multiple processes
count = 0
FOR EACH objProcess in colProcess
objProcess.Terminate()
count = count + 1

Next

'Runs the batch file from a silent shell command
shell "C:\RDSync\sync.bat"

在上面的例子中,变量“RDfolder”是c:\ users \ username \ documents \ synced files

我希望这可以帮到你!

答案 3 :(得分:0)

尝试shell.run“C:\ Program Files \ WinSCP \ Build.bat” 根据您的代码,您尝试运行C:\ Program Files \ WinSCP \\ Build.bat 连接时请注意双\\。

答案 4 :(得分:0)

每次在所有情况下命名文件时,都会插入一个额外的反斜杠。例如,这一行:

"C:\Program Files\WinSCP\" & "\Build.bat"

产生

"C:\Program Files\WinSCP\\Build.bat"

如果纠正此错误在所有实例中仍然不起作用,请以这种方式在批处理文件的执行中插入/C参数:

shell.run "/C C:\Program Files\WinSCP\" & "Build.bat"