如何使用VisualBasicScript(.vbs)运行文件

时间:2009-06-05 15:05:24

标签: file vbscript

如何使用VisualBasicScript(.vbs)运行文件?

该文件是'file.bat',它与.vbs位于同一个目录中。

6 个答案:

答案 0 :(得分:18)

  

是的,我想运行它。

然后试试这个:

CreateObject("WScript.Shell").Run "file.bat"

答案 1 :(得分:0)

在technet Script Center Script Repository上查看很多示例。

一个简单的例子是Select and Ping Computers Using a Text File

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

答案 2 :(得分:0)

使用FileSystemObject

用于打开文件:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(".\File.bat", ForReading)

答案 3 :(得分:0)

function getFileInfo(filePath)
    dim fso, fileObj, outMsg
    set fso = createobject("Scripting.FileSystemObject")
    set fileObj = fso.getfile(filePath)
    outMsg = ""
    outMsg = outMsg & " Created: " & fileObj.DateCreated & vbcrlf
    outMsg = outMsg & " Last Accessed: " & fileObj.DateLastAccessed & vbcrlf
    outMsg = outMsg & " Last Modified: " & fileObj.DateLastModified & vbcrlf
    outMsg = outMsg & " File Type: " & fileObj.Type & vbcrlf
    if fileObj.attributes and 0 then
        outMsg = outMsg & " File Attributes: Normal File"
    else
        outMsg = outMsg & " File Attributes: "
        if fileObj.attributes and 1 then
            outMsg = outMsg & "Read Only "
        end if
        if fileObj.attributes and 2 then
            outMsg= outMsg & "Hidden "
        end if
        if fileObj.attributes and 4 then
            outMsg= outMsg & "System "
        end if
        if fileObj.attributes and 8 then
            outMsg= outMsg & "Volume "
        end if
        if fileObj.attributes and 16 then
            outMsg= outMsg & "Directory "
        end if
        if fileObj.attributes and 32 then
            outMsg= outMsg & "Archive "
        end if
        if fileObj.attributes and 1024 then
            outMsg= outMsg & "Link "
        end if
        if fileObj.attributes and 2048 then
            outMsg= outMsg & "Compressed "
        end if
    end if
    set fileObj = nothing
    set fso = nothing
    getFileInfo = outMsg
end function

答案 4 :(得分:0)

甚至更简单 该代码适用于所有操作系统,我在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

答案 5 :(得分:-3)

Jamb Code:

select
    Trf.ID,
    (
        case when Spd.MAX_SPEED <= Cat.SPEED_CAT_1 then Trf.amount_drivers1 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_2 then Trf.amount_drivers2 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_3 then Trf.amount_drivers3 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_4 then Trf.amount_drivers4 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_5 then Trf.amount_drivers5 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_6 then Trf.amount_drivers6 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_7 then Trf.amount_drivers7 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_8 then Trf.amount_drivers8 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_9 then Trf.amount_drivers9 else 0 end
        + case when Spd.MAX_SPEED <= Cat.SPEED_CAT_10 then Trf.amount_drivers10 else 0 end
    ) TO_HARD
from
    TRAFFIC Trf
    join MAX_SPEED Spd Trf.MAX_SPEED_KEY = Spd.ID
    join SPEED_CATEGORIES Cat on Trf.SPEED_CATEGORIE_KEY = Cat.ID

VBS代码:

jamb(run) "%PWD%\File.bat" & display box(small) with $OUTPUT