Как я могу запустить файл с VisualBasicScript (.vbs)?
Файл называется «file.bat» и находится в том же каталоге, что и .vbs.
Как я могу запустить файл с VisualBasicScript (.vbs)?
Файл называется «file.bat» и находится в том же каталоге, что и .vbs.
да, я хочу запустить его.
Тогда попробуйте это:
CreateObject("WScript.Shell").Run "file.bat"
См. множество примеров в technet репозитории скриптов Script Center.
Простой пример: Выбор и проверка связи с компьютерами с помощью текстового Файл:
On Error Resume Next
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)
strComputers = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strComputers, vbCrLf)
Set objShell = CreateObject("WScript.Shell")
For Each strComputer In arrComputers
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer
Set objExecObject = objShell.Exec(strCommand)
strText = objExecObject.StdOut.ReadAll
If Instr(strText, "Reply") > 0 Then
' =====================================================================
' Insert your code here
' =====================================================================
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_OperatingSystem")
For Each objItem In ColItems
Wscript.Echo strComputer & ": " & objItem.Caption
Next
Else
Wscript.Echo strComputer & " could not be reached."
End If
Next
Используйте FileSystemObject.
Использование для открытия файла:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(".\File.bat", ForReading)
Еще проще Этот код работает для всех ОС, я попробовал его в Windows 10, и он работает так хорошо: попробуйте сами :)
Function BrowseForFile()
BrowseForFile = CreateObject("WScript.Shell").Exec( _
"mshta.exe ""about:<input type=file id=f>" & _
"<script>resizeTo(0,0);f.click();new ActiveXObject('Scripting.FileSystemObject')" & _
".GetStandardStream(1).WriteLine(f.value);close();</script>""" _
).StdOut.ReadLine()
End Function
Код косяка:
jamb(run) "%PWD%\File.bat" & display box(small) with $OUTPUT
Код ВБС:
set runFile (".\file.bat")
mode console
msgbox (runFile)