Announcement

Collapse
No announcement yet.

Can Run run a str? (VBS)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can Run run a str? (VBS)

    Code:
    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    WshShell.Run strTest
    
    strTest = "C:\ dir"
    is it possible to do anything like this? If so what am I doing wrong?
    This is my signature.

  • #2
    Re: Can Run run a str? (VBS)

    I think I have an answer;
    Code:
    strTest = "cmd /K C: && dir "
    strTest2 = " && dir "
    
    Dim WshShell
    
    Set WshShell = WScript.CreateObject ("WSCript.shell")
    
    WshShell.run strTest & strTest2
    This is my signature.

    Comment


    • #3
      Re: Can Run run a str? (VBS)

      I think I have an answer;...[code]...
      Yes, that is the correct syntax
      (well, - if you want to do the DIR command twice ).




      Try also this,
      building the strTest and strTest1 strings to just one variable, eg.:

      strCommand = "C:"
      strCommand = "%comspec% /c " & strCommand & "&CD\&"
      strCommand = strCommand & "Dir"
      strCommand = strCommand & " >""%temp%\DirList"""
      (double quotes around the path\file)

      If you also use the last line from the lines above, the output will in that case be written to a textfile, - this now makes it possible to run the Dosbox 'hidden' if you like. The textfile can be opend by WordPad, NotePad or translated to a xls or html file.

      I use the windows environment variable %comspec% instead of directly using cmd.exe. That is because older versions of windows use command.com instead of cmd.exe. Using the sytemvariable will assure the right command is used for the computer.

      Here is an example how not to show the results in a DosBox, but to write it to Internet Explorer:
      Code:
      '-------------------------------------------------------------------
      'wsh Run method: http://msdn2.microsoft.com/en-us/library/d5fk67ky.aspx
      '-------------------------------------------------------------------
      Dim WshShell, objFileSystem
      
      Set WshShell = WScript.CreateObject ("WSCript.shell")
      Set objFileSystem = CreateObject("Scripting.fileSystemObject")
      
      WshShell.popup "Wait a few seconds",1
      
      
      
      ' ----------------
      '  This command...
      ' ----------------
      
      sTitle = "DirList"
      strCommand = "C:"
      strCommand = "%comspec% /c " & strCommand & "&CD\&"
      strCommand = strCommand & "Dir /A:-D /s /b"
      strCommand = strCommand &" >""%temp%\" & sTitle & """"
      
      Error = WshShell.run(strCommand, 0, true)  '<-- where 0 = hidden, true = wait untill command is finished.
      If Error = 0 then
        strTempFile = WshShell.ExpandEnvironmentStrings("%temp%\"& sTitle)
        'Read strTempFile in an array:
        With objFileSystem.OpenTextFile(strTempFile, 1) 
          sResults = .ReadAll 
          .Close
        End With
        'Open with Intenet Explorer:
        CustomBox sResults, sTitle
      End If
      
      
      ' ----------------
      '  OR this...
      ' ----------------
      
      sTitle = "DirTree"
      strCommand = "C:"
      strCommand = "%comspec% /c " & strCommand & "&CD\&"
      strCommand = strCommand & "TREE . /F /A"
      strCommand = strCommand &" >""%temp%\" & sTitle & """"
      
      Error = WshShell.run(strCommand, 0, true)  '<-- where 0 = hidden, true = wait untill command is finished.
      If Error = 0 then
        strTempFile = WshShell.ExpandEnvironmentStrings("%temp%\"& sTitle)
        'Read strTempFile in an array:
        With objFileSystem.OpenTextFile(strTempFile, 1) 
          sResults = .ReadAll 
          .Close
        End With
        'Open with Internet Explorer:
        CustomBox sResults, sTitle
      End If
      
      
      Set WshShell = Nothing
      Set objFileSystem = Nothing
      Wscript.quit
      
      
      
      'open InternetExplorer and create a page:
      Function CustomBox(sResults, sTitle)
        '----------------------------------
         Set colItems = GetObject( _
            "Winmgmts:\\.\root\cimv2").ExecQuery _
            ("Select ScreenWidth,ScreenHeight From " _
            & "Win32_DesktopMonitor", , 48)
         For Each objItem In colItems
           sWidth= objItem.ScreenWidth
           sHeight = objItem.ScreenHeight
           If sWidth > sHorizontal _
             then intHorizontal = sWidth
           If sHeight > sVertical _
             then intVertical = sHeight
         Next
        '----------------------------------
      
        'Convert input string to a valid HTML string
         sResults = Replace(sResults,"&","&",1,-1,vbTextCompare)
         sResults = Replace(sResults,Chr(34),"""",1,-1,vbTextCompare)
         sResults = Replace(sResults,">",">",1,-1,vbTextCompare)
         sResults = Replace(sResults,"<","<",1,-1,vbTextCompare)
         sResults = Replace(sResults, vbNewLine, "<br>")
      
        set oIE = CreateObject("InternetExplorer.Application")
        'http://msdn2.microsoft.com/en-us/library/aa768398.aspx
        With oIE
         .RegisterAsDropTarget = False
         .Resizable = True : .FullScreen = False
         .AddressBar = False
         .MenuBar = False
         .ToolBar = False
         .StatusBar = False
         .width = 600 : .height = 800
         .Left = (intHorizontal - .width) / 2
         .Top = (intVertical - .height) / 2
         .Navigate "about:blank"
         Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
         .document.open
         .document.write _
           "<html><head><" & "script>bboxwait=true;</" _
           & "script><title>" & sTitle & "</title></head>"_
           & "<body bgColor=Silver" _
           & " style='border-Style:outset;border-" _
           & "Width:3px'>" _
           & "<pre>"& sResults &"</pre>"
         .document.close
         
         'set window focus
         .Visible = True
         Do While (.Busy) : Wscript.Sleep 100 : Loop
         WScript.CreateObject("WSCript.shell").AppActivate _
         ("http:// - " & sTitle & " -")
        End With '---(oIE)---
      End Function

      In addition,
      If you have WSH 5.6 installed on the computer (default on Windows XP SP2), you can also use the WshExec object's exec method to run an executable. You can use the exact same strCommand string as in the previous script, but with this method you can also do something like this;
      Code:
      '-------------------------------------------------------------------
      'wsh Exec Method: http://msdn2.microsoft.com/en-us/library/ateytk4a.aspx
      '-------------------------------------------------------------------
      
      sTitle = "DirList"
      
      strCommand ="C:"
      strCommand = "%comspec% /k "& strCommand & "&CD\"
      
      strRun = "Dir /A:-D /s /b"
      
      Dim WshShell
      Set WshShell = WScript.CreateObject ("WSCript.shell")
      
      Set oExec = WshShell.Exec(strCommand)
      oExec.stdIn.WriteLine strRun
      oExec.stdIn.WriteLine "<hr>"
      oExec.stdIn.WriteLine "exit"
      'oExec.stdIn.close
      
      'popup
       WshShell.sendkeys("^") '<= set focus ready for popup
       WshShell.popup "Wait a few seconds",1, sTitle
       
       sResults = oExec.stdOut.ReadAll
       sResults = Replace(sResults,strRun, strRun & "<hr>")
        
      CustomBox sResults, sTitle
        
      Set WshShell = Nothing
      wscript.quit
      
      
      
      'open InternetExplorer and create a page:
      Function CustomBox(sResults, sTitle)
        '----------------------------------
         Set colItems = GetObject( _
            "Winmgmts:\\.\root\cimv2").ExecQuery _
            ("Select ScreenWidth,ScreenHeight From " _
            & "Win32_DesktopMonitor", , 48)
         For Each objItem In colItems
           sWidth= objItem.ScreenWidth
           sHeight = objItem.ScreenHeight
           If sWidth > sHorizontal _
             then intHorizontal = sWidth
           If sHeight > sVertical _
             then intVertical = sHeight
         Next
        '----------------------------------
      
        'Convert input string to a valid HTML string
         sResults = Replace(sResults,"&","&",1,-1,vbTextCompare)
         sResults = Replace(sResults,Chr(34),"""",1,-1,vbTextCompare)
         sResults = Replace(sResults,">",">",1,-1,vbTextCompare)
         sResults = Replace(sResults,"<","<",1,-1,vbTextCompare)
         sResults = Replace(sResults, vbNewLine, "<br>")
      
        set oIE = CreateObject("InternetExplorer.Application")
        'http://msdn2.microsoft.com/en-us/library/aa768398.aspx
        With oIE
         .RegisterAsDropTarget = False
         .Resizable = True : .FullScreen = False
         .AddressBar = False
         .MenuBar = False
         .ToolBar = False
         .StatusBar = False
         .width = 600 : .height = 800
         .Left = (intHorizontal - .width) / 2
         .Top = (intVertical - .height) / 2
         .Navigate "about:blank"
         Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
         .document.open
         .document.write _
           "<html><head><" & "script>bboxwait=true;</" _
           & "script><title>" & sTitle & "</title></head>"_
           & "<body bgColor=Silver" _
           & " style='border-Style:outset;border-" _
           & "Width:3px'>" _
           & "<pre>"& sResults &"</pre>"
         .document.close
         
         'set window focus
         .Visible = True
         Do While (.Busy) : Wscript.Sleep 100 : Loop
         WScript.CreateObject("WSCript.shell").AppActivate _
         ("http:// - " & sTitle & " -")
        End With '---(oIE)---
      End Function
      With the exec method it is not longer nessesaray to write to a temp file, when you want to script the output. This can be very handy when you planned to script applications (like "nslookup.exe" or..)

      note*, with the exec method it is not possible to 'hide' the DOSbox.
      (Im still looking how I can Minimize (keys: Alt + space + N ) a dos-window by script (' sendkeys "%( N)" ' works fine this way for windows applications but the functionkeys here don't seem to have effect on a dosbox)


      \RemS


      - EDIT -
      *)
      Found a way to minimize (or even hide) the WshShell.Exec command box.
      download "CMDOW.exe":
      http://www.commandline.co.uk/cmdow/
      Then...
      Edit from the last script this line;
      Set oExec = WshShell.Exec(strCommand & "&&(""<path>\cmdow.exe"" @ /HID)")
      Last edited by Rems; 3rd January 2009, 17:58.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X