Link to home
Start Free TrialLog in
Avatar of michtech
michtech

asked on

How to include commands for a batch file to a test file?

I have a batch file that I use to take care of maintenance issues. How do I add to the batch file for it to create a report of all that it accomplished for viewing?
cd %systemdrive%
cd %systemroot%
cls
@echo off
echo This program will delete all temporary files from the Prefetch, Windows\Temp, and the Userprofile\Temp folder. 
echo.
pause
del prefetch\*.* /f /s /q
del temp\*.* /f /s /q
del "%temp%\*.*" /f /s /q
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\
del "Temporary Internet Files\*.*" /f /s /q
del temp\*.* /f /s /q
cls
temp > c:\batchresults.txt
echo.
echo              All tasks completed successfully, 
pause

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course, with all the /q on your deletes, it's set to 'quiet', so not much will be returned...
@echo off
set reportfile=c:\batchresults.txt
cd /d %systemroot% >%reportfile%
cls
echo This program will delete all temporary files from the Prefetch, Windows\Temp, and the Userprofile\Temp folder.  >>%reportfile%
echo.
pause
del prefetch\*.* /f /s /q>>%reportfile%
del temp\*.* /f /s /q>>%reportfile%
del "%temp%\*.*" /f /s /q>>%reportfile%
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\
del "Temporary Internet Files\*.*" /f /s /q>>%reportfile%
del temp\*.* /f /s /q>>%reportfile%
cls
temp >>%reportfile%
echo.
echo              All tasks completed successfully, 
pause

Open in new window

Avatar of Bill Prew
Bill Prew

How are you running your BAT file.  If from the Task Scheduler, then make the command you execute something like this:

c:\temp\yourfile.bat>c:\temp\log.txt 2>&1

If you are running right from a command shell window, then I'd do it this way:

cmd /c c:\temp\yourfile.bat>c:\temp\log.txt 2>&1

Adjust the batch file name, and the location of the log file as needed.  Bot approaches willlog all output to the log file.

~bp
If you want to see your commands aswell as their output in the logs as suggested above just remove the @echo off line too.

If a script gets scheduled and there is possibility of errors then I would build in error checking and check errorlevel after each command that matters.  You can keep track of those and at the end return an error to task scheduler etc.

Can't see more than first couple of lines of your script on mobile to know what it does though to say any more at the mo.

Steve
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of michtech

ASKER

Ok, first I have to appologize. I did not realize in my original question statement I miss typed "text" as test. Sorry! But, it seems you have the idea anyway.

sirbounty - I did as you suggested, although new to batch files I figured out after a couple trial and errors that the ">>%reportfile%" needs to be at the end of each command you want to include. I also understand there are alot of "q"'s in this, but was under the standing that quiest mode just does not confim deletion when wid cards are used. The files being deleted still scroll on the screen and get put into the report.txt.  TY

billprew - I am executing this from the current users desktop, there are other reasons that I have to be at the desktop and figured I could have this batch file do a few things for me.

dragon-it - Appreciate the additional thought on @echo there or not there. I don't know anything inregards to errors or error checking that may apply to deleting temporary files. Do you?

BillDL - I deleted the "temp > c:\batchresults.txt" as was left over attemps of creating a report. I also fixed my other mistake of no closing, TY.

I have another additional question, if I want a date stamp recorded for each report, as they are getting appended. Where in the batch, and what would be put in?


cd %systemdrive%
cd %systemroot%
cls
set reportfile=c:\report.txt
echo This program will delete all temporary files from the Prefetch, Windows\Temp, Userprofile\Temp folder, 

Temporary Internet Files, and the Internet Cookies. >>%reportfile%
echo.
pause
del prefetch\*.* /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
del "%temp%\*.*" /f /s /q >>%reportfile%
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\" >>%reportfile%
del "Temporary Internet Files\*.*" /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
cls
echo.
echo              All tasks completed successfully, >>%reportfile%
pause

Open in new window

As far as the date stamp goes, I would add a line like below at the very top, right after you set the reportfile variable:

echo Batch file [%0] started at: %DATE  %TIME%>>%reportfile%

~bp
Hi michtech, and thank you.

If you just add the following commands right at the start of the batch file and right at the end, it will separate the process logs by date and time with nice line spaces:

echo ================================>>C:\Report.txt
echo Batch file started %DATE% - %TIME%>>C:\Report.txt
echo.>>C:\Report.txt

The rest of the batch file here.

echo.>>C:\Report.txt
echo Batch file finished %DATE% - %TIME%>>C:\Report.txt
echo ================================>>C:\Report.txt
echo.>>C:\Report.txt

If you place the line:
set reportfile=c:\report.txt
before you echo the first Time and Date to the report, then you can substitute C:\Report.txt with %reportfile%

You asked/stated:
"I don't know anything in regards to errors or error checking that may apply to deleting temporary files."
There can be issues with such things as log files that are currently being used by AntiVirus applications and similar.  Removal of the @echo off may help you to see any situations where there are problems, but for some odd reason the error output isn't redirected to the log.  While testing it is also a good idea to add a PAUSE as your last line, because batch files in Windows 2000 and later tend to finish and close the CMD window.  You may wish to leave a Pause at the end to hold the screen open and see what happened until you have nailed down any problem areas.  Alternatively launch the batch file from a new CMD window by typing the batch file name and the window should remain open.
Hi Bill.  I was reading and typing while you were posting.  Good idea to include the name of the batch file in the log file though.
Thank you again, I added the text BillDL suggested, and left the pause in the string. I do see some text talking about access denied.......and it does not output to the report and scrolls off the screen to fast for me to read it. Any suggestions?
As it stands any lines with >> on will not redirect any stderror output which may or not be giving you clues.  Run each line manually and see what is causing the error, possibly just trying to remove temp files will be trying to remove a few current ones in use too.

You can instead wrap the whole script in a redirection, e.g.

@Echo off
(
echo Your script
echo More script
) >> yourlogfile.txt 2>>&1
echo This bit won't be in the log

As to error checking it means things like after each command doing

if errorlevel 1 (
 echo Error %errorlevel% with xyz command
 set error=%error% XYZ
)


then at the end maybe:

if not [%error%]==[]  echo There were errors: %error% & pause
This is exactly why I take the approach mentioned in 26446986, so that EVERYTHING that comes out of the script gets logged.

~bp
Ok, bp. I guess I understand. But not sure where to put the string - cmd /c c:\temp\yourfile.bat>c:\temp\log.txt 2>&1
 I have attached the current file for you.
echo ================================>>C:\Report.txt
echo Batch file started %DATE% - %TIME%>>C:\Report.txt
echo.>>C:\Report.txt
cd %systemdrive%
cd %systemroot%
cls
set reportfile=c:\report.txt
echo This program will delete all temporary files from the Prefetch, Windows\Temp, Userprofile\Temp folder, Temporary Internet Files, and the Internet Cookies. >>%reportfile%
echo.
pause
del prefetch\*.* /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
del "%temp%\*.*" /f /s /q >>%reportfile%
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\" >>%reportfile%
del "Temporary Internet Files\*.*" /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
cls
echo.
echo.>>C:\Report.txt
echo Batch file finished %DATE% - %TIME%>>C:\Report.txt
echo ================================>>C:\Report.txt
echo.>>C:\Report.txt
echo              All tasks completed successfully, >>%reportfile%
pause

Open in new window

First, you would remove all the individual logging from your current batch file, as attached.  You haven't specified what the name of that batch file is, so I'll call it C:\TEMp\YOURFILE.BAT for now.

Then, however you currently execute that batch file, you would adjust to capture and send all output to a log file.  So if you currently just type the following command at a prompt:

C:\TEMP\YOURFILE.BAT

You would now want to type:

C:\TEMP\YOURFILE.BAT >C:\TEMP\LOG.TXT 2>&1

If you are running this as a scheduled taks, then the same idea would apply, in the Task Scheduler change the command that you are running to include the redirection to a log file.

You won't be adding anything to your original batch script, just changing the way you initiate it.  Let me know if this still isn't clear.

~bp
echo ================================
echo Batch file started %DATE% - %TIME%
echo.
cd %systemdrive%
cd %systemroot%
cls
set reportfile=c:\report.txt
echo This program will delete all temporary files from the Prefetch, Windows\Temp, Userprofile\Temp folder, Temporary Internet Files, and the Internet Cookies. >>%reportfile%
echo.
pause
del prefetch\*.* /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
del "%temp%\*.*" /f /s /q >>%reportfile%
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\" >>%reportfile%
del "Temporary Internet Files\*.*" /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
cls
echo.
echo.
echo Batch file finished %DATE% - %TIME%
echo ================================
echo.
echo              All tasks completed successfully, >>%reportfile%
pause

Open in new window

The current file is called temp.bat, and I just double click the file to run it from the desktop on a given machine.
==> michtech

==> The current file is called temp.bat, and I just double click the file to run it from the desktop on a given machine.

Understand.  The easiest way to do it would be the following:
- right click on the test.bat icon on the desktop
- select create shortcut
- that will place a new icon on the desktop
- right click the new icon
- select properties
- edit the Target to add the  >C:\REPORT.TXT 2>&1   at the end
- click okay

After that, run it by double clicking the new icon rather than the old icon.

If this works well, then longer term you might want to move the actual BAT file to a folder ather than the Desktop, and update the Properties of the shortcut to reference it's new location.

~bp
Any way to add the string directly to the temp.bat file, I use this file to help deleting temp files on various computers, then delete the file when done? Also, it may be because of permissions but this works great on XP, but not on Vista.
Oh and Vista is most likely havin user dirs in c:\users or similar.  Use %userprofile% instead of c:\Documents and Settings\%username%

Steve
If you want a single file then you would need to get a bit more clever.  The best way I could see to do that would be for the temp.bat file to check if an parms were passed to it.  If not, then it could execute itself again with a parm that would indicate to go to the main logic the second time it runs.  That output could then be captured as I mentioned.  Here's the concept, although I haven't tested this.

~bp
if "%1"=="EXECUTE" goto :MainLogic
%0 EXECUTE >C:\REPORT.TXT 2>&1
exit /b
:MainLogic
echo ================================
echo Batch file started %DATE% - %TIME%
echo.
cd %systemdrive%
cd %systemroot%
cls
echo This program will delete all temporary files from the Prefetch, Windows\Temp, Userprofile\Temp folder, Temporary Internet Files, and the Internet Cookies. >>%reportfile%
echo.
pause
del prefetch\*.* /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
del "%temp%\*.*" /f /s /q >>%reportfile%
cd\\
cd "c:\Documents and Settings\%username%\Local Settings\" >>%reportfile%
del "Temporary Internet Files\*.*" /f /s /q >>%reportfile%
del temp\*.* /f /s /q >>%reportfile%
cls
echo.
echo.
echo Batch file finished %DATE% - %TIME%
echo ================================
echo.
echo              All tasks completed successfully, >>%reportfile%
pause

Open in new window