Jump to content

Help me to redirect to a file any errors from my batch.


Recommended Posts

Mhz no you are wrong because these if statement are the checks, please read the script before making incorrect statements.

This is to check if the disks has any thing in it

If strDrive.IsReady = True Then

This is to check to see if it a CD or DVD drive

If strDrive.DriveType = 4 Then

This is to check that it the correct path and file

If Fso.FileExists(strDrive.DriveLetter & Cmd1) Then

this will run only if the 3 if statements are true, this would be the CD Or DVD drive letter

strDrive.DriveLetter

Act.Run(strDrive.DriveLetter & Cmd1 & " /qb /norestart"),1,True

The script will only work if that statement is true, this is better then using a send key method as this is not the best method to do this.

Now if I took out the first 2 if statements then it would search for strDrive.DriveLetter & Cmd1 on your floppy drive, harddrive, cd or dvd drive. This builds the completed path to the file,

which the action uses.

Edited by gunsmokingman
Link to comment
Share on other sites


Here's a TEE like method which doesn't require 3rd party programs, it uses VBS!

Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Set Args=WScript.Arguments
LogFile=Args(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile= fso.CreateTextFile(Args(0))

Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
StdOut.WriteLine str
LogFile.WriteLine str
Loop

LogFile.Close

Run your Batch file with a pipe to the filter executed with CSCRIPT and add the logfile name you require as a parameter.

Something like this:

MyBatchFile.cmd |cscript//nologo TFILTER.VBS MyLogFile.txt

The output of the Batch file is displayed line-by-line and simultaneously captured to MyLogFile.txt line-by-line.

If you change the VBS line:

  LogFile.WriteLine str

to

  LogFile.WriteLine "Line " & (StdIn.Line - 1) & ": " & str

this will number all the lines in the log (but not on screen).

Note: You must execute the TFILTER.VBS with CSCRIPT and not WSCRIPT, (STDIN and STDOUT are not available via WSCRIPT).

Link to comment
Share on other sites

Here's a TEE like method which doesn't require 3rd party programs, it uses VBS!

Yep, but it is also possible by using a "pure" .BAT, see my previously posted link:

http://www.robvanderwoude.com/tee.html

TEE.BAT, Version 2.01 for Windows NT 4 / 2000 / XP

@ECHO OFF

:: Check Windows version

IF NOT "%OS%"=="Windows_NT" GOTO Syntax

:: Keep variables local

SETLOCAL

:: Check command line arguments

SET Append=0

IF /I [%1]==[-a] (

SET Append=1

SHIFT

)

IF [%1]==[] GOTO Syntax

IF NOT [%2]==[] GOTO Syntax

:: Test for invalid wildcards

SET Counter=0

FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"

IF %Counter% GTR 1 (

SET Counter=

GOTO Syntax

)

:: A valid filename seems to have been specified

SET File=%1

:: Check if a directory with the specified name exists

DIR /AD %File% >NUL 2>NUL

IF NOT ERRORLEVEL 1 (

SET File=

GOTO Syntax

)

:: Specify /Y switch for Windows 2000 / XP COPY command

SET Y=

VER ¦ FIND "Windows NT" > NUL

IF ERRORLEVEL 1 SET Y=/Y

:: Flush existing file or create new one if -a wasn't specified

IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

:: Actual TEE

FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (

> CON ECHO.%%B

>> %File% ECHO.%%B

)

:: Done

ENDLOCAL

GOTO:EOF

:Count

SET /A Counter += 1

SET File=%1

GOTO:EOF

:Syntax

ECHO.

ECHO Tee.bat, Version 2.10 for Windows NT 4 / 2000 / XP

ECHO Display text on screen and redirect it to a file simultaneously

ECHO.

ECHO Usage: some_command ³ TEE.BAT [ -a ] filename

ECHO.

ECHO Where: "some_command" is the command whose output should be redirected

ECHO "filename" is the file the output should be redirected to

ECHO -a appends the output of the command to the file,

ECHO rather than overwriting the file

ECHO.

ECHO Written by Rob van der Woude

ECHO http://www.robvanderwoude.com

ECHO Modified by Kees Couprie

ECHO http://kees.couprie.org

ECHO and Andrew Cameron

:thumbup

Of course it can be simplified and included in the same "main" batch file....

jaclaz

Link to comment
Share on other sites

Hi again. First of all, thank you all for your ideas. Now I will stop dealing with my unattended CD (3 weeks of full work is enough) and I will restart the improvements at my next format. But I thought as a proper act to give my feedback:

To Gunsmokingman: I was ready to test your solution, but finally I didn't, because I saw a simpler method posted by MHz. I see that you know very well to code in vbs and write more precise scripts (with more checks, better error catching etc), but personally I am in the beginning of the road so I prefer to go step-by-step. I will keep your posts as a reference and maybe I will re-bother you for an explanation.

To MHz: With your help, I finally made possible to have all my programs being executed from the CD. Your code has worked excellent. I understood that the problem was with what the script was considering as the current directory. These 5 lines fixed my .vbs. Thank you.

To Jaclaz: Finally, with this version of tee command, I managed to have it worked. Thanks for the link. I used your syntax and the log file was being created, having inside of it all the screen's output. But another problem came up. The error messages or (more precisely) the stderr stream is not being logged. I see only the standard output. At this moment, I don't remember if the stderr was being showed on the screen, but I think no. Maybe this command filters all the input it takes and gives as the final output (duplicated in the screen and the file) only the stdout stream. But this is not exactly what I need. I want a log file with the full output of my script including any errors. Is it possible to improve the code in the tee.bat so the stderr stream is also being logged?

To Yzowl: I have also tried your solution, but with the same results as above (again, I don't remember if the stderr was being showed on the screen). Any error messages are hidden (the counting of the lines is very cool though). It would be very useful if you added to your .vbs support for the stderr stream (at least in the log file).

Link to comment
Share on other sites

Another few (semi-random) ideas:

http://www.teaser.fr/~amajorel/stderr/

http://www.faqs.org/faqs/msdos-programmer-...section-11.html

http://www.codeproject.com/dialog/quickwin.asp

Wouldn't something similar to this work?:

@Echo OFF
del myboth.txt >nul
SET NEXTCOMMAND=VOL
FOR %%A in (c d e f g h i j k l m n o p q r s t u v x y z) Do (
ECHO %NEXTCOMMAND% %%A: >>MYBOTH.TXT

%NEXTCOMMAND% %%A: >>MYBOTH.TXT 2>&1
)
FOR /F "tokens=1* delims=" %%A in (MYBOTH.TXT) DO ECHO %%A

or this:

@Echo OFF
del myboth.txt >nul
SET NEXTCOMMAND=VOL
FOR %%A in (c d e f g h i j k l m n o p q r s t u v x y z) Do (
ECHO %NEXTCOMMAND% %%A: >MYBOTH.TXT
%NEXTCOMMAND% %%A: >>MYBOTH.TXT 2>&1
type myboth.txt
)

In the above example I am using the VOL command on all letters as if the drive letter exist, the otput is STDOUT, otherwise it is STDERR...

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...