views:

4649

answers:

4

Hello,

i want to open a file with VisualBasicScript (.vbs)

How could i do this?

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

It should RUN it!

A: 

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
gimel
hmmm.. can't really find easily what i'm looking for.
YourComputerHelpZ
i need to open a batch
YourComputerHelpZ
A: 

Use the FileSystemObject

Usage to open file:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(".\File.bat", ForReading)
cmsjr
i know it's silly, but how to use it...?
YourComputerHelpZ
see above, that will get you as file as opening a file, In this case it is being opened for reading, you can also specify for writing or appending.
cmsjr
Note ForWriting will overwrite any current contents.
cmsjr
it's not working... It gives an error on the 2nd line
YourComputerHelpZ
btw i would like .bat
YourComputerHelpZ
My bad, you need to locally define the const ForReading, above edited. Please clarify, "I would like .bat"
cmsjr
It appears to accept a relative path no problem.
cmsjr
it doesnt work it doesnt open it up.
YourComputerHelpZ
Does it give you an exception? What exactly do you want to do with the file when you open it? Given that it is a bat file, do you actually want to "run" the batch file?
cmsjr
A: 
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 filObj.attributes and 2 then
      outMsg= outMsg & "Hidden "
     end if
     if filObj.attributes and 4 then
      outMsg= outMsg & "System "
     end if
     if filObj.attributes and 8 then
      outMsg= outMsg & "Volume "
     end if
     if filObj.attributes and 16 then
      outMsg= outMsg & "Directory "
     end if
     if filObj.attributes and 32 then
      outMsg= outMsg & "Archive "
     end if
     if filObj.attributes and 1024 then
      outMsg= outMsg & "Link "
     end if
     if filObj.attributes and 2048 then
      outMsg= outMsg & "Compressed "
     end if
    end if
    set fieObj = nothing
    set fs = nothing
    getFileInfo = outMsg
end function
NickAtuShip
uhmmmm... i just want to open it, nothing else.
YourComputerHelpZ
+4  A: 

yes i want to run it.

Then try this:

CreateObject("WScript.Shell").Run "file.bat"
Helen
Good show Helen +the 1
cmsjr
Excellent answer. Simple and to the point. +1
ichiban
Thanks you very much!
YourComputerHelpZ