Processing files from the previous day

J

JJ

Hello. I would like to post some code and see if anyone can help me
out with a problem I am having.

There are a bunch of files in the directory, with file names like
accesslog_mmddyyyy.log. What I am trying to do is run a script which
will zip all files where the mmddyyyy part of the file name is NOT
equal to today's date in the same format (ie. 10222003). I have a
method which will extract the current date into the mmddyyyy format,
but I don't know how to extract the date string out of the file name.
Here is what I have so far:

:: gets the current date and converts it to mmddyyyy
for /f "tokens=1,2,3,4,5" %%i in ('date /t') do for /f "delims=/
tokens=1,2,3,4" %%m in ("%%j") do set strDate=%%m%%n%%o

::zip up each file from previous day
For /F "tokens=*" %%F In ('Dir c:\test1 /B /L') do pkzip25 -add
c:\test1\%%F c:\test1\%%F


I have been pulling my hair for most of the day trying to figure it
out, but I am not all that good with Batch language for Windows 2000
and doing something like this is a little complicated.

Any help will be greatly appreciated!!

Jim
 
P

Phil Robyn

JJ said:
Hello. I would like to post some code and see if anyone can help me
out with a problem I am having.

There are a bunch of files in the directory, with file names like
accesslog_mmddyyyy.log. What I am trying to do is run a script which
will zip all files where the mmddyyyy part of the file name is NOT
equal to today's date in the same format (ie. 10222003). I have a
method which will extract the current date into the mmddyyyy format,
but I don't know how to extract the date string out of the file name.
Here is what I have so far:

:: gets the current date and converts it to mmddyyyy
for /f "tokens=1,2,3,4,5" %%i in ('date /t') do for /f "delims=/
tokens=1,2,3,4" %%m in ("%%j") do set strDate=%%m%%n%%o

::zip up each file from previous day
For /F "tokens=*" %%F In ('Dir c:\test1 /B /L') do pkzip25 -add
c:\test1\%%F c:\test1\%%F


I have been pulling my hair for most of the day trying to figure it
out, but I am not all that good with Batch language for Windows 2000
and doing something like this is a little complicated.

Any help will be greatly appreciated!!

Jim

- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
C:\cmd>dir /s /b \junkdir\access*
C:\junkdir\accesslog_10012003.log
C:\junkdir\accesslog_10022003.log
C:\junkdir\accesslog_10102003.log
C:\junkdir\accesslog_10222003.log

C:\cmd>dir /s /b \junkdir\access*.log | find /v "10222003"
C:\junkdir\accesslog_10012003.log
C:\junkdir\accesslog_10022003.log
C:\junkdir\accesslog_10102003.log

C:\cmd>demo\ZipNotTodayFiles
Your pkzip command C:\junkdir\accesslog_10012003.log
Your pkzip command C:\junkdir\accesslog_10022003.log
Your pkzip command C:\junkdir\accesslog_10102003.log

C:\cmd>rlist demo\ZipNotTodayFiles.cmd
=====begin C:\cmd\demo\ZipNotTodayFiles.cmd ====================
1. @echo off
2. for /f "tokens=*" %%a in (
3. 'dir /s /b \junkdir\access*.log ^| find /v "10222003"'
4. ) do echo Your pkzip command %%a
=====end C:\cmd\demo\ZipNotTodayFiles.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -
 
J

JJ

Phil Robyn said:
1. @echo off
2. for /f "tokens=*" %%a in (
3. 'dir /s /b \junkdir\access*.log ^| find /v "10222003"'
4. ) do echo Your pkzip command %%a

Phil, thanks kindly for your prompt post last night. I tried your
code, and it worked perfectly. I substituted "10222003" in your FOR
statement with the variable %strDate%, and all files were processed
except the ones with the file name string matching the value of
strDate (current date).

There is one more thing I want to do. I want to try writing a FOR
loop statement which will process all the Zipped files and delete the
ones which are older than 2 months. I was thinking that this could be
done by a simple arithmetic which compares the mm value of the file
name, to the mm value of the current month or the mm value of strDate
.. If mm of strDate - (minus) mm of file date >= 3 then delete the zip
file. I am subtracting 3 just to ensure it is a full 2 months old...
for example, compare access-log08312003.log.zip with
access-log10222003.log.zip. If we subtract 10-8 we get 2, but the
august log is still not a full 2 months (ie. 60days) old.

If you know a better way to do this I am all ears. Maybe it would be
easier to just check the real file creation date of the zip file with
the current date, and if it is older than 60 days, then delete. It
doesn't really matter, what's easiest is most important.

Thanks again, I really appreciate your help.

Jim
 
P

Phil Robyn

JJ said:
Phil, thanks kindly for your prompt post last night. I tried your
code, and it worked perfectly. I substituted "10222003" in your FOR
statement with the variable %strDate%, and all files were processed
except the ones with the file name string matching the value of
strDate (current date).

There is one more thing I want to do. I want to try writing a FOR
loop statement which will process all the Zipped files and delete the
ones which are older than 2 months. I was thinking that this could be
done by a simple arithmetic which compares the mm value of the file
name, to the mm value of the current month or the mm value of strDate
. If mm of strDate - (minus) mm of file date >= 3 then delete the zip
file. I am subtracting 3 just to ensure it is a full 2 months old...
for example, compare access-log08312003.log.zip with
access-log10222003.log.zip. If we subtract 10-8 we get 2, but the
august log is still not a full 2 months (ie. 60days) old.

If you know a better way to do this I am all ears. Maybe it would be
easier to just check the real file creation date of the zip file with
the current date, and if it is older than 60 days, then delete. It
doesn't really matter, what's easiest is most important.

Thanks again, I really appreciate your help.

Jim

You can download FORFILES.EXE for free from Microsoft at

ftp://ftp.microsoft.com/ResKit/y2kfix/x86/

Just use FORFILES to find and delete the files that are more than 60
days old.
 
O

Ole

I adapted a batch i did for somebody else looking to do a similar thing.
Syntax:

====================================================
SUBMONTH: Subtracts user defined interger from the month.
Leap years are not accounted for -- no complaints!

Syntax: SUBMONTH ## [c^|a^|e] [separator]

## = Maximum integer of 27 to subtract from date
separator = any symbol accepted by batch files to separate the output
c = Continental output, YYYYMMDD (largest to smallest)
a = American output, MMDDYYYY (err..American)
e = English output, DDMMYYYY (smallest to largest)
====================================================

no error checking on the ## so if you put in:
submonth bob c /
it'll just return today's date.

unfortunatly i don't have a solution as to how you could use the output of
this to actually delete files and you might also just want a simplified
version to just subtract 2 months. Here it is:

@echo off

:: _______________________Input_Verification_________________

if "%1"=="" goto usage
if "%1"=="?" goto usage
if "%1"=="/?" goto usage

:: attempts to add 1 to the users input to test if user enters a number
set /a test=1% + 1
if errorlevel 1 (
echo:
echo Only numbers are valid input
goto end)

:: check to see if number is in under 12.



if "%2"=="a" goto noerrors
if "%2"=="e" goto noerrors
if "%2"=="c" goto noerrors
if "%2"=="A" goto noerrors
if "%2"=="E" goto noerrors
if "%2"=="C" goto noerrors

echo:
echo Please specify regonial setting.
goto end

:: ________________________Usage_Display____________________

:usage
echo:
echo SUBMONTH: Subtracts user defined interger from the month.
echo Leap years are not accounted for -- no complaints!
echo:
echo Syntax: SUBMONTH ## [c^|a^|e] [separator]
echo:
echo ## = Maximum integer of 27 to subtract from date
echo separator = any symbol accepted by batch files to separate the
output
echo c = Continental output, YYYYMMDD (largest to smallest)
echo a = American output, MMDDYYYY (err..American)
echo e = English output, DDMMYYYY (smallest to largest)
goto end

:: _______________________Processing________________________

:noerrors

:: Day, month and year are obtained from %date%
:: Removes the user specified value from day.
set day=%date:~0,2%
set /a month=%date:~3,2% - %1
set year=%date:~6,4%

:: checks to see if the result of days is a minus
:: number i.e. user input of 7 day of 2
if %month% lss 01 goto bigif
:: if it the result isn't a minus number output can be given
goto display

:bigif
::
set minus=%month:~1,2%

:: set month to last month (-1) because uses want to subtract
:: more days than have pasted in the month, so you need to go
:: back a month.

set /a year=%year% - 01
:: sets the value of the last day in the month
set /a month=12-0%minus%
goto display

:: ________________________Output___________________________


:display
:: different order for the %2 variable. and %3 included as the separator
if "%2"=="a" set daysold=%month%%3%day%%3%year%
if "%2"=="A" set daysold=%month%%3%day%%3%year%
if "%2"=="E" set daysold=%day%%3%month%%3%year%
if "%2"=="e" set daysold=%day%%3%month%%3%year%
if "%2"=="c" set daysold=%year%%3%month%%3%day%
if "%2"=="C" set daysold=%year%%3%month%%3%day%

echo %daysold%

:end
 
P

Phil Robyn

Ole said:
I adapted a batch i did for somebody else looking to do a similar thing.
Syntax:

====================================================
SUBMONTH: Subtracts user defined interger from the month.
Leap years are not accounted for -- no complaints!

Syntax: SUBMONTH ## [c^|a^|e] [separator]

## = Maximum integer of 27 to subtract from date
separator = any symbol accepted by batch files to separate the output
c = Continental output, YYYYMMDD (largest to smallest)
a = American output, MMDDYYYY (err..American)
e = English output, DDMMYYYY (smallest to largest)
====================================================

no error checking on the ## so if you put in:
submonth bob c /
it'll just return today's date.

unfortunatly i don't have a solution as to how you could use the output of
this to actually delete files and you might also just want a simplified
version to just subtract 2 months. Here it is:

@echo off

:: _______________________Input_Verification_________________

if "%1"=="" goto usage
if "%1"=="?" goto usage
if "%1"=="/?" goto usage

:: attempts to add 1 to the users input to test if user enters a number
set /a test=1% + 1
if errorlevel 1 (
echo:
echo Only numbers are valid input
goto end)

:: check to see if number is in under 12.



if "%2"=="a" goto noerrors
if "%2"=="e" goto noerrors
if "%2"=="c" goto noerrors
if "%2"=="A" goto noerrors
if "%2"=="E" goto noerrors
if "%2"=="C" goto noerrors

echo:
echo Please specify regonial setting.
goto end

:: ________________________Usage_Display____________________

:usage
echo:
echo SUBMONTH: Subtracts user defined interger from the month.
echo Leap years are not accounted for -- no complaints!
echo:
echo Syntax: SUBMONTH ## [c^|a^|e] [separator]
echo:
echo ## = Maximum integer of 27 to subtract from date
echo separator = any symbol accepted by batch files to separate the
output
echo c = Continental output, YYYYMMDD (largest to smallest)
echo a = American output, MMDDYYYY (err..American)
echo e = English output, DDMMYYYY (smallest to largest)
goto end

:: _______________________Processing________________________

:noerrors

:: Day, month and year are obtained from %date%
:: Removes the user specified value from day.
set day=%date:~0,2%
set /a month=%date:~3,2% - %1
set year=%date:~6,4%

:: checks to see if the result of days is a minus
:: number i.e. user input of 7 day of 2
if %month% lss 01 goto bigif
:: if it the result isn't a minus number output can be given
goto display

:bigif
::
set minus=%month:~1,2%

:: set month to last month (-1) because uses want to subtract
:: more days than have pasted in the month, so you need to go
:: back a month.

set /a year=%year% - 01
:: sets the value of the last day in the month
set /a month=12-0%minus%
goto display

:: ________________________Output___________________________


:display
:: different order for the %2 variable. and %3 included as the separator
if "%2"=="a" set daysold=%month%%3%day%%3%year%
if "%2"=="A" set daysold=%month%%3%day%%3%year%
if "%2"=="E" set daysold=%day%%3%month%%3%year%
if "%2"=="e" set daysold=%day%%3%month%%3%year%
if "%2"=="c" set daysold=%year%%3%month%%3%day%
if "%2"=="C" set daysold=%year%%3%month%%3%day%

echo %daysold%

:end

=====begin C:\cmd\demo\TestDelFilesOlderThan.cmd ====================
01. @echo off
02. ::
03. :: deletes any files matching (filespec) that are
04. :: older than (cutoff), where (cutoff) is specified
05. :: as '-nnnn' (number of days)
06. ::
07. :: example: demo\TestDelFilesOlderThan c:\cmd\test\x*.cmd -100
08. ::
09. setlocal
10. set filespec=%1
11. set cutoff=%2
12. call WSHDate today %cutoff%
13. set cutoff=%WSHDate%
14. for %%a in (%filespec%) do call :main %%~fa %%~ta
15. goto :EOF
16. :main
17. set zfile=%1
18. set zdate=%2
19. for /f "tokens=1-3 delims=/ " %%a in (
20. "%zdate%"
21. ) do set fmm=%%a&set fdd=%%b&set fyy=%%c
22. if "%fyy:~0,1%" equ "0" (
23. set fyy=20%fyy%
24. ) else (
25. set fyy=19%fyy%
26. )
27. set zdate=%fyy%%fmm%%fdd%
28. if %zdate% lss %cutoff% echo %zdate% %zfile%
29. goto :EOF
=====end C:\cmd\demo\TestDelFilesOlderThan.cmd ====================

(In the preceding, we are just 'echoing' the dates and names of the files
that match the criterion in line 28. To actually delete or do something
else to the matching files, you would have to modify line 28 accordingly.)

=====begin C:\cmd\UTIL\WSHDate.cmd ====================
01. @echo off
02. setlocal
03. set date1=%1
04. set qty=%2
05. if /i "%date1%" EQU "TODAY" (
06. set date1=now
07. ) else (
08. set date1="%date1%"
09. )
10. echo>%temp%\%~n0.vbs s=DateAdd("d",%qty%,%date1%)
11. echo>>%temp%\%~n0.vbs WScript.Echo year(s)^&right(100+month(s),2)^&right(100+day(s),2)
12. for /f "tokens=1" %%a in (
13. 'cscript //nologo %temp%\%~n0.vbs'
14. ) do set result=%%a
15. endlocal&set %~n0=%result%
=====end C:\cmd\UTIL\WSHDate.cmd ====================

sample screen output (Win2000):

C:\cmd>demo\testdelfilesolderthan c:\cmd\test\m*.cmd -1000
20000121 c:\cmd\TEST\M0001JAN.CMD
19990804 c:\cmd\TEST\MAILER1.cmd
19990913 c:\cmd\TEST\main1234.cmd
20000310 c:\cmd\TEST\MASSCOPY.CMD
20000705 c:\cmd\TEST\microsed.cmd
20001107 c:\cmd\TEST\mmm2mm.cmd
19990927 c:\cmd\TEST\MODULO.CMD
19990805 c:\cmd\TEST\msgbox0.cmd
19990810 c:\cmd\TEST\msgbox2.cmd
20000404 c:\cmd\TEST\MTDIR01.CMD
20000524 c:\cmd\TEST\MTDIR02.CMD
20000729 c:\cmd\TEST\multcopy.cmd
20000314 c:\cmd\TEST\MULTMSG1.CMD
20010112 c:\cmd\TEST\mycyclictask.cmd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top