Tom
Wed Jul 12 07:30:36 CDT 2006
Unfortunately, your first approach to redirecting the output implies a
flaw. Redirection is not supported by the mechanisim that the Run
method invokes. It is only supported by the command processor (best
invoked with the COMSPEC environment variable). Therefore, unless the
string provided by the variable "yourcommand" starts with a reference
to the command processor, the subsequent Run statement will fail.
There is also a potential pitfall in the second approach as well. I
don't think it applies here, but it has often caused heartburn for
those trying to automate a certain console based zip archiving program.
Specifically, both text buffers associated with the StdOut and the
StdErr property can become full, thereby blocking proper execution of
the program invoked by the Exec procedure. Therefore, I caution that
it is a good practice to implement processing of both the StdOut and
StdErr interfaces to the Exec method. Failing to empty the StdErr
buffer has tripped up more than a few hapless scripters. (There is
also the annoyance of the command console window that is instaniated by
the Exec statement, though this can be avoided with a fancy recursive
call. Torgeir Bakken (MVP) showed me that trick a long time ago - see:
http://groups.google.com/group/microsoft.public.scripting.wsh/browse_frm/thread/eb38318877ec3fec)
Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh
joseomjr@gmail.com wrote:
> You could get what is being displayed in the cmd window by adding
> something like:
> >> c:\output.txt to the end of your objshell.run
> objshell.run yourcommands >> c:\output.txt, 1, true
>
> try this in a cmd prompt.
> ipconfig >> c:\output.txt
>
> you'll see the output is sent to that text file directly.
>
> the other way would be to use the exec method and retrieve the
> information. an example from the Microsoft site:
> Set objShell = CreateObject("WScript.Shell")
> Set objWshScriptExec = objShell.Exec("ipconfig /all") 'your command
> would go here
> Set objStdOut = objWshScriptExec.StdOut
>
> While Not objStdOut.AtEndOfStream
> strLine = objStdOut.ReadLine
> If InStr(strLine,"Physical Address") Then
> WScript.Echo strLine
> End If
> Wend
>
> If you ask me, the first method would be the quickest an easiest.
>
> Nico VanHaaster wrote:
> > Guess i should give you all the code. Thanks in advance
> > '//Start FTP-Files.vbs
> > Const COMMAND_FTP = "ftp.exe -i -s:"
> > CONST strHost = "localHose" 'Host Server
> > CONST strUser = "ftpUsername" 'User Name
> > CONST strPass = "ftpUserPass" 'Password
> > CONST strMode = "ascii" 'Mode (ascii/binary)
> >
> > FTP("DIR")
> >
> > Function FTP( strCMD, oFile )
> > Dim objFSO
> > Dim strFile, objTempFldr, objFile, objRegExp
> > Dim objShell, WSX, ReturnCode, Output, strLog, strErrorLog
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set oFileFSO = objFSO.OpenTextFile(oFile,2,True)
> > set objTempFldr = objFSO.GetSpecialFolder( 2 )
> > strFile = objFSO.GetTempName
> > strFile = objTempFldr & "\" & left(strFile,Len(strFile)-4) & ".ftp"
> >
> > if not objFSO.FileExists( strFile ) then objFSO.CreateTextFile(
> > strFile )
> > Set objFile = objFSO.OpenTextFile( strFile, 2, True )
> >
> > objFile.WriteLine( strUser )
> > objFile.WriteLine( strPass )
> > objFile.WriteLine( Mode )
> >
> > objFile.WriteLine( strCMD )
> > objFile.WriteLine( "bye" )
> > objFile.Close()
> >
> >
> > Set objShell = WScript.CreateObject("WScript.Shell")
> >
> > SET WSX = objShell.Run "cmd /c " & COMMAND_FTP & strFile & " " &
> > strHost,1,TRUE
> > strErrorLog = objTempFldr.Path & "ftpErrors.txt"
> > strLog = objTempFldr.Path & "ftpLog.txt"
> >
> > Set objFile = objFSO.OpenTextFile( strErrorLog, 2, True )
> > objFile.Write( ReturnCode.ReadAll() )
> > objFile.Close()
> >
> > Set objFile = objFSO.OpenTextFile( strLog, 2, True )
> > objFile.Write( Output.ReadAll() )
> > objFile.Close()
> >
> > set objFSO = nothing
> >
> > Set objRegExp = New RegExp
> > objRegExp.IgnoreCase = True
> >
> > objRegExp.Pattern = "not connected|invalid command|error"
> > If (objRegExp.Test( Output.ReadAll ) = True ) or (objRegExp.Test(
> > ReturnCode.ReadAll ) ) Then
> > FTP = False
> > Else
> > FTP = True
> > End If
> > Set objRegExp = nothing
> > End Function
> > '//End