Setting variable values and logging to a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tigers!
Posts: 6
Joined: 01 Jul 2008 21:57
Location: Australia

Setting variable values and logging to a file

#1 Post by Tigers! » 01 Jul 2008 22:13

Gudday all
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
echo Q20=%Q20%
set /a DATA=0
echo DATA=%DATA%
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.
Please help.

DGMakers
Posts: 13
Joined: 30 Jun 2008 04:28

#2 Post by DGMakers » 02 Jul 2008 01:52

i never use for's in batch, THEY NEVER WORK FOR ME !!!

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#3 Post by jeb » 02 Jul 2008 08:34

Hi,

nice problem, but where is the problem? :wink:

I tried this and it works

Code: Select all

REM *** Update log file and delete any tmp files and clean up
@echo off
setlocal
if exist "deletelist.tmp" goto :doIt
goto :eof

:doIt
set /a Q20=0
echo Q20=%Q20%
set /a DATA=0
echo DATA=%DATA%
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


The reason is the line

Code: Select all

if exist "deletelist.tmp" goto :doIt

instead of

Code: Select all

if exist "deletelist.tmp" (


because you get problems at the first ")" in the line

Code: Select all

for /f "usebackq" %%x in (`type deletelist.tmp ^| find /c "Q20_"`) do set Q20=%%x


jeb

DGMakers
Posts: 13
Joined: 30 Jun 2008 04:28

#4 Post by DGMakers » 03 Jul 2008 04:10

you cant use

Code: Select all

if exist "deletelist.tmp" goto :doIt


because :doIt is just setting a label. you need to use:

Code: Select all

if exist "deletelist.tmp" goto doIt

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 03 Jul 2008 23:15

DGMakers
goto :doIt - works fine on XP. What are you using?

DGMakers
Posts: 13
Joined: 30 Jun 2008 04:28

#6 Post by DGMakers » 04 Jul 2008 08:11

XP

so u saying it works if i go goto :doit & echo loading...

well it doesnt

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#7 Post by DosItHelp » 06 Jul 2008 13:39

DGMakers,
I tried the short batch below. As expected it echos "It did it."

Code: Select all

@echo off
goto :doit
exit /b

:doit
echo.It did it.
exit /b


I also tried with "SETLOCAL DISABLEEXTENSIONS" up front with same result.
Not sure, may be I don't understand the issue you're pointing out?
:?

DGMakers
Posts: 13
Joined: 30 Jun 2008 04:28

#8 Post by DGMakers » 08 Jul 2008 00:44

lol, ok your correct but if you may use & anothercommand on the end of the goto... yea as i said before.

Thebetr1
Posts: 12
Joined: 30 Jun 2008 02:50
Location: My computer
Contact:

#9 Post by Thebetr1 » 08 Jul 2008 01:04

its bad programming to use
goto :label

because : is the character that is setting the label

its like going
goto Setlabel hi


Also this site is being glitchy even though ive got it set to show my sig
it doesn't show it ??

Tigers!
Posts: 6
Joined: 01 Jul 2008 21:57
Location: Australia

#10 Post by Tigers! » 08 Jul 2008 17:22

Thank you all for your help thus far.
I have split the code into 2 functions as suggested earlier.
The code is now

Code: Select all

@echo off

REM *** Set location to begin the directory list ***
set filestodeletepath=f:\AU-SAP33_Backup\oracle\Q20\sapbackup 
REM *** The script itself lives here ***
set homepath=f:\AU-SAP33_Backup                                                 

REM *** This gets the full path list of each required directory. File directory_list2.tmp contains the final path directory list
dir %filestodeletepath% /s /o:d | findstr /c:"Directory" > directory_list1.tmp
for /f "tokens=3" %%i in (directory_list1.tmp) do @echo %%i >> directory_list2.tmp
for /f "tokens=1" %%i in (directory_list2.tmp) do call:getfilelist %%i %homepath%

REM *** Return to home directory ***
chdir %homepath%

REM *** Update log file and delete any tmp files and clean up
if exist "deletelist.tmp" call:countfilenumbers

:: === Function ===
:countfilenumbers
rem echo entering function countfilenumbers
set /a Q20=0
echo Q20=%Q20%
set /a DATA=0
echo DATA=%DATA%
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
rem echo leaving function countfile numbers
goto:eof


:: === Function ===
:getfilelist
set currentdir=%1
set home=%2
chdir %currentdir%
if exist "*DATA*" (
   dir /b /a:-a | findstr /c:"DATA" > Anotset.tmp
   for /f "tokens=1" %%h in (Anotset.tmp) do @echo %currentdir%\%%h >> %home%\deletelist.tmp
   del Anotset.tmp
)
if exist "Q20_*" (
   dir /b /a:-a | findstr /c:"Q20_" > Anotset.tmp
   for /f "tokens=1" %%h in (Anotset.tmp) do @echo %currentdir%\%%h >> %home%\deletelist.tmp
   del Anotset.tmp
)
rem echo leaving function getfilelist
goto:eof

The code sets the entry variables ok and prints out the values as required yet is doing it twice now.
Q20=0
DATA=0
Number of Q20* files deleted 53
Number of DATA* files deleted 94
Q20=0
DATA=0
Number of Q20* files deleted 53
Number of DATA* files deleted 94

The thing that bothers me is that I have a goto:eof at the end of each function. Isn't that bound to cause confusion? (I t does with me at least :? )
What are I doing wrong?

PS: Can one add a screenshot to these posts? I had to write out the output rather than just show the screen shot.

Post Reply