1

我使用 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)
4

5 回答 5

3

我通过使用 shell.run """C:\Program Files\WinSCP\Build.bat""" 让它在我的机器上工作 原因:Program Files 有一个空格,所以它必须用引号引起来 - 因此你需要 3两端的引号。

于 2013-09-27T08:21:26.257 回答
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

我希望这可以帮助你!

于 2013-10-15T18:38:42.057 回答
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带有参数的文件(不存在)Files\WinSCP\\Build.bat

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

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

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

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

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

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

并使用这样的功能:

shell.Run qq("C:\Program Files\WinSCP\Build.bat")
于 2013-09-27T08:56:40.810 回答
0

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

于 2013-09-26T12:53:09.397 回答
0

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

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

生产:

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

如果在所有情况下更正此错误后仍然无效,/C请以这种方式在批处理文件的执行中插入参数:

shell.run "/C C:\Program Files\WinSCP\" & "Build.bat"
于 2013-09-26T13:10:04.720 回答