Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
how to create a batch file which suffixes yesterdays date to the file name
example filename_12-may-09.txtthx in advance

@echo off & cls
::*****ALWAYS BACKUP YOUR FILES*****
::*NB* - THIS CODE DEPENDS ON YOUR DATE OUTPUT.
:: MINE IS: 'DD/MM/YYYY'::PLEASE NOTE, THIS IS UNTESTED. I AM TIRED.
::TEST IT, IF YOU DON'T LIKE IT, DON'T USE IT.
::IF YOU CAN IMPROVE IT OR USE A BIT OF IT FOR
:: SOMETHING ELSE THEN GOOD FOR YOU!
::---------------------------------------::GET DATE INTO dd/mm/yy
set dd=%date:~0,2%
set mm=%date:~3,2%
set yy=%date:~8,2%::MAKE DATE "YESTERDAY'S DATE"
if %dd% == 01 (
goto :CARRYMONTH
) else (
set /a dd -=1
goto :RENAMEFILES
):CARRYMONTH
if %mm% == 01 (
goto :CARRYYEAR
) else (
if %mm% == 01 set dd=31
if %mm% == 02 set dd=28
if %mm% == 03 set dd=31
if %mm% == 04 set dd=30
if %mm% == 05 set dd=31
if %mm% == 06 set dd=30
if %mm% == 07 set dd=31
if %mm% == 08 set dd=31
if %mm% == 09 set dd=30
if %mm% == 10 set dd=31
if %mm% == 11 set dd=30
if %mm% == 12 set dd=31
set /a mm -=1
goto :RENAMEFILES
):CARRYYEAR
set dd=31
set mm=12
set /a yy -=1:RENAMEFILES
REM LINES STARTING WITH 'REM' ARE FOR TESTING.
REM REMOVE THE 'REM' FROM THE TWO LINES BELOW TO
REM SEE THE TEST.
REMecho %dd% - %mm% - %yy%
REMpause>nulrename "C:\DSC_0030.JPG" *_%dd%-%mm%-%yy%.*

Lee123abc - I have a small problem with the script. If I set the system date to 01-03-2009 (dd-mm-yyyy) the date returned should be 28-2-09 there being just 28 days in Feb 09 but the script returns 31-2-09, also, if the system date is set to 01-03-2008 the script again returns 31-2-08 which should be 29-2-08, 2008 being a leap year. Similarly 01-07-2009 returns 31-6-09.
It seems there's no provision for dealing with a month which has a variable number of days (February) or less than 31 days.
Edit: The commands set /a dd -=1 set /a mm -=1 and set /a yy -=1 must fail if the relevant variable is set to 08 or 09 which are invalid in Octal.

Here's a batch that picks up yesterdays date that should work on
just about any xp machine, so long as the language is
english. You can change the formatting at the third last
line, it should be easy to figure out:@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION FOR /F "skip=4 tokens=3" %%A IN ('REG Query "HKEY_CURRENT_USER\Control Panel\International" /v sDate') DO SET lim=%%A FOR /F "skip=4 tokens=2,*" %%A IN ('REG Query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') DO ( SET sdf=%%B SET now=!date! IF DEFINED lim ( FOR /F %%D IN ("!lim!") DO ( SET sdf=!sdf:%%~D= ! SET now=!date:%%~D= ! ) ) ) FOR %%A IN ("jan=1" "feb=2" "mar=3" "apr=4" "may=5" "jun=6" "jul=7" "aug=8" "sep=9" "oct=10" "nov=11" "dec=12") DO SET now=!now:%%~A! FOR %%A IN (m o n t u e w d h r f i s a) DO SET now=!now:%%A=! FOR %%A IN (%sdf%) DO ( SET tester=%%A IF "!tester:ddd=!"=="!tester!" ( IF NOT "!tester:d=!"=="!tester!" ( SET ndf=!ndf! tday ) ELSE ( IF NOT "!tester:m=!"=="!tester!" ( SET ndf=!ndf! tmonth ) ELSE ( SET ndf=!ndf! tyear ) ) ) ) CALL :Match %now% FOR %%A IN (tyear tmonth tday) DO IF NOT DEFINED %%A ( >&2 ECHO An Error Occured - Check if it is EVEN POSSIBLE to work out what >&2 ECHO the date is from the %%date%% variable^("%date%"^). ENDLOCAL EXIT /B 1 ) IF %tyear% LSS 99 SET tyear=20%tyear% if "!tmonth:~0,1!"=="0" set tmonth=!tmonth:~1! if "!tday:~0,1!"=="0" set tday=!tday:~1! goto yesterday :Match FOR %%A IN (%ndf%) DO ( CALL SET %%A=%%1 SHIFT ) goto :eof :yesterday SET /A tday-=1 IF %tday%==0 ( SET /A tmonth-=1 IF !tmonth!==0 ( SET /A tyear-=1 SET tday=31 SET tmonth=12 ) else ( IF !tmonth!==2 ( set /a leap=!tyear!%%4 IF !leap! GTR 0 (SET tday=28) ELSE SET tday=29 ) else ( FOR %%G IN (1,3,5,7,8,10) DO IF !tMONTH!==%%G SET TDAY=31 FOR %%H IN (4,6,9,11) DO IF !tMONTH!==%%H SET TDAY=30 ) ) ) IF NOT "%tmonth:~0,1%"=="0" IF %tmonth% LSS 10 SET tmonth=0%tmonth% IF NOT "%tday:~0,1%"=="0" IF %tday% LSS 10 SET tday=0%tday% endlocal & set yesterdaysdate=%tday%/%tmonth%/%tyear% echo %yesterdaysdate% PAUSE

Wahine, you are right... the 08 and 09 octals are just killers!!
Also I meesed it up :(Judago is the boss here!!!!! Nice one mate.
Cheers

why go through such trouble with data calculation with batch when you can do it easily with languages like vbscript
Set objFS = CreateObject("Scripting.FileSystemObject") strFile="file.txt" Set objFile = objFS.GetFile(strFile) d = Split(FormatDateTime(DateAdd("d",-1,Now),1),", ") newname = "file_" & Replace(d(1)," ","-") & "-" & d(2) WScript.Echo newname objFile.Name=newnameon the command prompt
c:\test> cscript /nologo myscript.vbs

There are many ways to get the current date-1 and here's another again using VBS:
@echo off cls set newfile=%temp%\date.vbs echo otherdate = (Date()-1)>%newfile% echo yy = datePart("yyyy", otherdate)>>%newfile% echo mm = datePart("m" , otherdate)>>%newfile% echo dd = datePart("d" , otherdate)>>%newfile% echo wscript.echo yy^&" "^&mm^&" "^&dd>>%newfile% FOR /F "tokens=1-3" %%A in ('cscript //nologo %newfile%') do ( set year=%%A set month=%%B set day=%%C ) del %newfile% if %month% lss 10 set month=0%month% if %day% lss 10 set day=0%day% echo Year-Month-Day = %year%-%month%-%day% :: REN command here...There are a couple of interesting totally batch scripts at this site: http://www.tech-recipes.com/rx/1096... They could easily be modded to suit. Verbosity is obviously not frowned upon!!!
Regards to all.
W.

yes, but your solution creates extra process like creating a temp vbs just to calculate dates. then you got to use a for loop again to get the dates. quite inefficient. One vbs script can do the job already.
>> Verbosity is obviously not frowned upon!!!yes, but too much of it makes your code ugly and hard to maintain, especially if you have to read a batch file with many %'s.

Hi ghostdog,
A bit OT, and really lame, but...
Does python need an interpreter? Or is it 'supported' by windows, like VBS?
=====================================
If at first you don't succeed, you're about average.M2

hi m2
yes, python.exe is the interpreter, much like vbs needs cscript.exe or wscript.exe and yes, you can use Python language in most common platforms, eg window, unix, linux, Mac OS etc. All you need is to download it from the official Python web site. For windows, I recommend ActiveState Python.

I DLd the ActivePython-2.6.2.2-win32-x86.zip
27MB YIKES!
python.exe is about 27KB
the chm is 11MB
Which parts do I really need?
TY
=====================================
If at first you don't succeed, you're about average.M2

trust me, 27MB worth of goodies!. csv modules to parse csv files, tar module to archive your files, gzip module to compress your files, urllib/urllib2 to get webpages, datetime module for date calculation, math module for maths ,tkinter for GUI programming, etc among other useful modules for everyday system administration and general programming.
I used the msi version, so it will go through the windows installer. easy to install.
the chm is the manual, its for your reference everytime you program. very handy.
last but not least, check out the python documentation site as well as the Python Packages index for other useful modules.

Hi ghostdog,
Thanks for all that.
Is python good for very large numbers?
Does the ActivePython have a compiler?
=====================================
If at first you don't succeed, you're about average.M2

I personally have not dealt with large numbers. if you are doing stuffs like matrix and things like that, you can check out NumPy. (search google for link). otherwise, you can go to comp.lang.python newsgroup and ask the experts there...
As for your 2nd qns, i am not sure what you meant. If you are talking about compiling Python on your windows system, you can get the source code , then use a C compiler (search for "compile Python windows" in google).
btw, there's a version of python for DOS as well :)

Hi ghostdog,
Thanks for that.
I meant compile python code to exe.
=====================================
If at first you don't succeed, you're about average.M2

Got it. Thanks.
=====================================
If at first you don't succeed, you're about average.M2

use %1 to determine
drag the rename file cover this batch ico
'===================================
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=/ " %%i in ('date /t') do (
(set sy=%%i) && (set sm=%%j) && (set sd=%%k)
set /a sd=%%k-1
if !sd! lss 1 ( set /a sm=%%j-1 ) else (set days=!sd! & goto :show )
if !sm! lss 1 ( set /a sy=%%i-1 & set sm=12)
)
:start
if not defined sd set sd=1
(set sy=0000%sy%) && (set sm=00%sm%) && (set sd=00%sd%)
(set sy=%sy:~-4%) && (set sm=%sm:~-2%) && (set sd=%sd:~-2%)
cd.
set /a y=1%sy%-10000, m=1%sm%-100, d=1%sd%-100 2>nul
if %y% lss 100 (
if %y% lss 50 (set /a y+=2000) else (set /a y+=1900)
set sy=!y!
)
if %m% lss 13 if %d% lss 32 call :Calc
set days=31
for %%i in (4 6 9 11) do if %m% equ %%i set days=30
set /a leap="^!(y%%4) & ^!(^!(y%%100)) | ^!(y%%400)"
if %m% equ 2 set /a days=28+%leap%
if %m% leq 2 (set /a y-=1& set /a m+=12)
if "%sm%"=="2 " ( set sm=12)
if "%sm%"=="1 " ( set sm=11)
:show
set F=%~x1
rename "%~1" "%sy%-%sm%-%days%%F%"
pause
:Calc
set/a Q=(y-1901)/4
set/a R=y-1901-4*Q
set n=0
for %%i in (0,31,59,90,120,151,181,212,243,273,304,334) do (
set /a n+=1
if %m% equ !n! set z=%%i
)
set /a leap="^!(y%%4) & ^!(^!(y%%100)) | ^!(y%%400)"
if %m% gtr 2 (if %leap% equ 0 (set /a z-=1) else (set /a z+=leap))
set/a n=(140*Q+106*(R+1)+z*10+d*10)/295,H=(140*Q+106*(R+1)+z*10+d*10-295*n)/10
if %h% equ 0 set h=29
goto :eof

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |