2009-06-05 21 views
7

¿Cómo puedo ejecutar un archivo con VisualBasicScript (.vbs)?Cómo ejecutar un archivo con VisualBasicScript (.vbs)

El archivo es 'file.bat' y está ubicado en el mismo directorio que el .vbs.

+0

@YourComputerHelpZ - usted es un montón de obtener grandes respuestas. Tal vez deberías explicar cuál es tu objetivo. Abra el archivo 'file.bat' y luego haga qué? ¿Muestra su contenido en un cuadro de mensaje, edita un texto, ejecuta el archivo por lotes, etc.? – ichiban

+2

Al decir "abrir", ¿quiere decir "iniciar"/"ejecutar"? – Helen

+0

sí quiero ejecutarlo. –

Respuesta

18

sí que quiero ejecutarlo.

A continuación, intente esto:

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

Buen espectáculo Helen + el 1 – cmsjr

+0

Excelente respuesta. Simple y al grano. +1 – ichiban

+0

¡Muchas gracias! –

0

Vea muchos ejemplos en technet Script Center Script Repository.

Un ejemplo sencillo es 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 .. realmente no puedo encontrar fácilmente lo que estoy buscando. –

+0

necesito abrir un lote –

+0

La primera respuesta es la mejor – RookieTEC9

0

Use el uso FileSystemObject

abrir el archivo:

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

Sé que es tonto, pero ¿cómo usarlo ...? –

+0

ver arriba, que lo obtendrá como un archivo como abrir un archivo, en este caso se está abriendo para su lectura, también puede especificarlo para escribir o anexar. – cmsjr

+0

Nota ForWriting sobrescribirá cualquier contenido actual. – 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 ... solo quiero abrirlo, nada más. –

-3

Código jamba:

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

Código VBS:

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

¡Esperamos que este comentario lo haya ayudado! – Microsoft

+0

No estoy seguro de que esto responda la pregunta –

Cuestiones relacionadas