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