Setting variable values and logging to a file
I have been trying fro a number of weeks now to log details in a log file of a script I wrote earlier.
The script is scans a set of directories and gathers the files that have the strings "Q20_" or "DATA" in their file names and have their archive attribute not set.
Once the list is gathered the file names along with their relevant path are stored in a directory called deletelist.tmp.
Deletelist.tmp is then used to delete those files.
That part works well. My boss now wants me to log how many Q20 & DATA files are deleted each time the script is run as well as the time the script began to run and the time it finished.
I can get the start time, finish time etc: working well. What is missing is the number of files. I thought it would be easy, just count the number of lines in deletelist.tmp Q20_ or DATA appears and store in the log file. Yet it always has a blank value.
The relevant code is
Code: Select all
REM *** Update log file and delete any tmp files and clean up
if exist "deletelist.tmp" (
set /a Q20=0
set /a DATA=0
echo File deletions started %date% %time% >> sap33_cleanup1.log
for /f "usebackq" %%x in (`type deletelist.tmp ^| find /c "Q20_"`) do set Q20=%%x
echo Number of Q20_* files deleted %Q20%
echo Number of Q20_* files deleted %Q20% >> sap33_cleanup1.log
for /f "usebackq" %%x in (`type deletelist.tmp ^| find /c "DATA"`) do set DATA=%%x
echo Number of *DATA* files deleted %DATA%
echo Number of *DATA* files deleted %DATA% >> sap33_cleanup1.log
echo File deletions finished %date% %time% >> sap33_cleanup1.log
I have put the extra echoes to display progress on the screen. The log file is called sap33_cleanup1.log.
It seems to me that the the variables Q20 & DATA are not been updated with the new values after their respective for commands.
Note that Q20& DATA appear to have blank as the value not 0.
The output of the log file is
Code: Select all
File deletions started Wed 02/07/2008 13:50:25.92
Number of Q20_* files deleted
Number of *DATA* files deleted
File deletions finished Wed 02/07/2008 13:50:25.92
I expect to see a numeric value after each the word deleted but see nothing.
I must be doing something really obscure as I can not see it.