How to run a file using VisualBasicScript (.vbs)

后端 未结 6 1549
你的背包
你的背包 2020-12-10 06:57

How can I run a file with VisualBasicScript (.vbs)?

The file is \'file.bat\' and it\'s located in the same dir as the .vbs.

相关标签:
6条回答
  • 2020-12-10 07:13

    yes i want to run it.

    Then try this:

    CreateObject("WScript.Shell").Run "file.bat"
    
    0 讨论(0)
  • 2020-12-10 07:20

    Jamb Code:

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

    VBS Code:

    set runFile (".\file.bat")
    mode console
    msgbox (runFile)
    
    0 讨论(0)
  • 2020-12-10 07:21

    Even simplier This code works for all OS, I tried it in Windows 10 and it works so well: Try it by yourself :)

    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
    
    0 讨论(0)
  • 2020-12-10 07:32

    See many examples on technet Script Center Script Repository.

    A simple example is 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
    
    0 讨论(0)
  • 2020-12-10 07:36

    Use the FileSystemObject

    Usage to open file:

    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(".\File.bat", ForReading)
    
    0 讨论(0)
  • 2020-12-10 07:37
    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
    
    0 讨论(0)
提交回复
热议问题