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

michtech
michtech used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007
Commented:
Basically two options...
You can 'redirect' commands, such as your 'echo' lines above:

set reportfile=c:\report.txt

echo This program will delete all temporary files from the Prefetch, Windows\Temp, and the Userprofile\Temp folder. >>%reportfile%

or, for ones that don't really report too much, drop your own 'echo' line above it:

echo Deleting prefetch data... >>%reportfile%
del prefetch\*.* /f /s /q
Top Expert 2007

Commented:
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

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Commented:
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
Hi michtech, just an observation:
What is the intended result of the following line in your batch file?:
temp > c:\batchresults.txt

You have changed directory into the user's:
"C:\Documents and Settings\<UserName>\Local Settings"
folder, and deleted Temporary Internet Files and files in the Temp folder.

 ... by the way, you have left off a closing " in that line:
cd "c:\Documents and Settings\%username%\Local Settings\
It should be:
cd "c:\Documents and Settings\%username%\Local Settings"

and then you have:
temp > c:\batchresults.txt

The command equates, because of the current directory, to:

c:\Documents and Settings\%username%\Local Settings\temp > c:\batchresults.txt

There is no command there to redirect the results of.  Did you mean to have a DIR command there to redirect to file?

Author

Commented:
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

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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.

Author

Commented:
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?

Commented:
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
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
This is exactly why I take the approach mentioned in 26446986, so that EVERYTHING that comes out of the script gets logged.

~bp

Author

Commented:
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

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Author

Commented:
The current file is called temp.bat, and I just double click the file to run it from the desktop on a given machine.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
==> 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

Author

Commented:
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.

Commented:
Oh and Vista is most likely havin user dirs in c:\users or similar.  Use %userprofile% instead of c:\Documents and Settings\%username%

Steve
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial