Announcement

Collapse
No announcement yet.

VBS - Trying to redirect output to text file

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

  • VBS - Trying to redirect output to text file

    Ok, so I'm a VB newb and that's probably what's causing this issue, but I can't seem to get this subroutine to run properly.

    What I'm wanting the subroutine to do is set the working directory to L:\ (a mapped drive from the previous subroutine), call the c:\scripts\zip.exe app with the hardcoded arguments (and a variable from a defined function), and redirect the output for zip.exe to append on a text file stored on C:\scripts\

    Here's the subroutine as I have it now:
    Code:
    Sub Compress_Files()
    	strDirectory = "L:"
    'Create the File System Object
                Set objShell = CreateObject("wscript.shell")
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set Ofolder = objFSO.GetFolder(strDirectory)
    
    			objshell.CurrentDirectory = strDirectory
    
                objShell.run "%comspec%|c:\scripts\zip.exe -m " & Datestamp & ".zip *.hl7 >> ""c:\scripts\zip.log""",1,TRUE
    end sub
    I've been searching for the answer for the last day and a half, and I think I must be searching for the wrong terms (or it's so basic no one asks)...but what I've tried so far:

    Code:
     objShell.run "%comspec%|c:\scripts\zip.exe -m " & Datestamp & ".zip *.hl7 >> ""c:\scripts\zip.log""",1,TRUE
    Returns "The system cannot find the file specified" error (jpg attached as WSH_error)
    Code:
    objShell.run "%comspec% | c:\scripts\zip.exe -m " & Datestamp & ".zip *.hl7 >> ""c:\scripts\zip.log""",1,TRUE
    Opens the command window, but does not run the piped command and halts the script until the command window is closed
    Code:
    objShell.run "%comspec% /c|c:\scripts\zip.exe -m " & Datestamp & ".zip *.hl7 >> ""c:\scripts\zip.log""",1,TRUE
    Flashes up the command window very briefly (if you blink it's gone) and proceeds with processing the script, does not run the piped command..../k does the same but the command windows remains open

    Having single, double, or no quotes around the log file argument has no effect that I can tell, nor does the Chr(34) string. Changing the defined variable to a constant (like test.log) has no effect either. This code:
    Code:
    objShell.run "c:\scripts\zip.exe -m " & Datestamp & ".zip *.hl7 >> c:\scripts\zip.log",1,TRUE
    runs fine, other than of course it doesn't generate the log file.

    Any help that you guys can provide, or even just a pointer in the right direction, is most appreciated!
    Attached Files
    Last edited by CypherDragon; 29th February 2008, 20:03. Reason: Closing thread

  • #2
    Re: VBS - Trying to redirect output to text file

    Can you post the full script.

    Makes it easier to see what line 51 is.

    Comment


    • #3
      Re: VBS - Trying to redirect output to text file

      check the command line this way:
      Code:
      dt = Now() : dt = Year(dt)*1e4 + Month(dt)*1e2 + Day(dt) _
        & "-" & right(1e6 + Hour(dt)*1e4 + Minute(dt)*1e2 + Second(dt),6)
      
      Datestamp = dt
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ' Sub Compress_Files()
      
      strCommand = "%comspec% /c ""c:\scripts\zip.exe"" " _
                   & "-m """ & Datestamp & ".zip"" *.hl7 " _
                   & ">>""c:\scripts\zip.log"" 2>&1"
      
      wscript.echo strCommand
       ' objShell.run strCommand, 1, TRUE
      By creating "strCommand" and using "wscript.echo", you can check on the command-line, before running it.
      And then copy the text that is showed with wscript.echo to a commandprompt and run it this way, to see if it works.

      \Rems

      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


      • #4
        Re: VBS - Trying to redirect output to text file

        Rems is right, you need to make sure what you are sending to the cmd line actually works on the command line. Most times I'll use an actual cmd prompt to get the command to work then copy/paste it to the code and tweak it so it's script compliant.

        Code:
        strCommand = "c:\scripts\zip.exe -m " & Datestamp & _
                                ".zip *.hl7 >> c:\scripts\zip.log 2>&1"
        Jake G

        Former Microsoft MVP - IIS ('02-'06)

        Comment


        • #5
          Re: VBS - Trying to redirect output to text file

          As it turns out, the wscript.shell Run method works pretty much the same as the Run dialog box.
          Some command lines however will work only from the command prompt (and will not work if you use the Run dialog box). In that case you must use cmd /c to open a command prompt then run the command. This way the command can run also from the Run dialog box, and can run when using the Run method.
          http://www.microsoft.com/technet/scr.../sept0905.mspx

          When you want to redirect output of the command, you must always use the command prompt. Therefore you should use %comspec% /c here!
          (using the system variable %comspec% instead of cmd.exe will ensure to use always the right command for the operatingsystem.)

          The strCommand should be:
          Code:
          strCommand = "%comspec% /c ""c:\scripts\zip.exe"" " _
                       & "-m """ & Datestamp & ".zip"" *.hl7 " _
                       & ">>""c:\scripts\zip.log"" 2>&1"
          copy the echo output to the Run dialogbox for testing.

          There is no output from executing %comspec% /c , no results of it need to be piped to your command string. Therefore you should not use "|" between the commands. Using the /c switch it all what is needed to create a prompt for you command to run on.

          \Rems

          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


          • #6
            Re: VBS - Trying to redirect output to text file

            You guys are awesome. I ended up with this:
            Code:
            strCommand = "%comspec% /c c:\scripts\zip.exe " _
               & "-m " & Datestamp & ".zip *.hl7 >> c:\scripts\zip-" & Timestamp & ".log 2>&1"
            and it works perfectly! Thanks for the help!

            Comment

            Working...
            X