7 Stimmen

So führen Sie eine Datei mit VisualBasicScript (.vbs) aus

Wie kann ich eine Datei mit VisualBasicScript (.vbs) ausführen?

Die Datei heißt 'file.bat' und befindet sich im selben Verzeichnis wie die .vbs.

0 Stimmen

@YourComputerHelpZ - Sie erhalten eine Menge guter Antworten. Vielleicht sollten Sie erklären, was Ihr Ziel ist. Die 'file.bat' öffnen und was dann tun? Den Inhalt in einem Nachrichtenfeld anzeigen, einen Text bearbeiten, die Batch-Datei ausführen usw.?

2 Stimmen

Wenn Sie "öffnen" sagen, meinen Sie dann "starten"/"ausführen"?

20voto

Helen Punkte 70190

Ja, ich möchte es durchführen.

Dann versuchen Sie dies:

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

0voto

gimel Punkte 78080

Viele Beispiele finden Sie auf Technet Skript Center Skript-Repository .

Ein einfaches Beispiel ist Computer mit einer Textdatei auswählen und anpingen :

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 Stimmen

Hmmm ich kann nicht wirklich leicht finden, wonach ich suche.

0 Stimmen

Die erste Antwort ist die beste

0voto

cmsjr Punkte 52971

Verwenden Sie die FileSystemObject

Verwendung zum Öffnen der Datei:

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

0 Stimmen

Siehe oben, damit erhalten Sie eine Datei, die zum Lesen geöffnet wird. Sie können auch angeben, dass sie zum Schreiben oder Anhängen geöffnet wird.

0 Stimmen

Hinweis ForWriting überschreibt den aktuellen Inhalt.

0 Stimmen

Mein Fehler, Sie müssen die Konstante ForReading lokal definieren, oben bearbeitet. Bitte klarstellen, "Ich möchte .bat"

0voto

ChickenMilkBomb Punkte 957
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

1 Stimmen

Uhmmmm... ich will es nur öffnen, sonst nichts.

0voto

Alex Casal Punkte 1

Noch einfacher Dieser Code funktioniert für alle Betriebssysteme, ich habe ihn in Windows 10 ausprobiert und er funktioniert sehr gut: Probieren Sie es selbst aus :)

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

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X