2009-06-05 16 views
7

Come posso eseguire un file con VisualBasicScript (.vbs)?Come eseguire un file utilizzando VisualBasicScript (.vbs)

Il file è 'file.bat' e si trova nella stessa dir di .vbs.

+0

@YourComputerHelpZ - sei un sacco di ottenere grandi risposte. Forse dovresti spiegare qual è il tuo obiettivo. Apri il file "file.bat" e poi fai cosa? Mostra il suo contenuto in una finestra di messaggio, modifica del testo, esegui il file batch, ecc.? – ichiban

+2

Dicendo "aperto", intendi "lancia"/"esegui"? – Helen

+0

sì, voglio eseguirlo. –

risposta

18

sì voglio eseguirlo.

Allora provate questo:

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

Buona mostra Helen + 1 – cmsjr

+0

Ottima risposta. Semplice e al punto. +1 – ichiban

+0

Grazie mille! –

0

Vedere molti esempi sulla technet Script Center Script Repository.

Un semplice esempio è 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

hmmm .. non riesco davvero a trovare facilmente quello che sto cercando. –

+0

ho bisogno di aprire un lotto –

+0

La prima risposta è la migliore – RookieTEC9

0

Utilizzare l'Usage FileSystemObject

per aprire il file:

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

so che è sciocco, ma come usarlo ...? –

+0

vedi sopra, che ti porterà come file come l'apertura di un file, In questo caso viene aperto per la lettura, puoi anche specificare per la scrittura o l'accodamento. – cmsjr

+0

Nota ForWriting sovrascrive qualsiasi contenuto corrente. – cmsjr

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 
+0

uhmmmm ... voglio solo aprirlo, nient'altro. –

-3

Codice Stipite:

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

Codice VBS:

set runFile (".\file.bat") 
mode console 
msgbox (runFile) 
+0

Speriamo che questo commento ti abbia aiutato! – Microsoft

+0

Non sono sicuro che questo risponda alla domanda –

Problemi correlati