Computing.Net > Forums > Programming > batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file

Reply to Message Icon

Name: veer
Date: May 13, 2009 at 06:28:36 Pacific
OS: Windows xp
Subcategory: Batch
Comment:

how to create a batch file which suffixes yesterdays date to the file name
example filename_12-may-09.txt

thx in advance



Sponsored Link
Ads by Google

Response Number 1
Name: lee123abc
Date: May 13, 2009 at 14:23:49 Pacific
Reply:

@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>nul

rename "C:\DSC_0030.JPG" *_%dd%-%mm%-%yy%.*


0

Response Number 2
Name: lee123abc
Date: May 18, 2009 at 14:17:04 Pacific
Reply:

No problem mate. It was my pleasure. Glad to know that it worked fine for ya!


0

Response Number 3
Name: Wahine
Date: May 18, 2009 at 15:47:33 Pacific
Reply:

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.


0

Response Number 4
Name: Judago
Date: May 19, 2009 at 01:03:10 Pacific
Reply:

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


0

Response Number 5
Name: lee123abc
Date: May 19, 2009 at 05:02:21 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: May 19, 2009 at 06:34:55 Pacific
Reply:

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=newname

on the command prompt

c:\test> cscript /nologo myscript.vbs


0

Response Number 7
Name: Wahine
Date: May 19, 2009 at 16:10:31 Pacific
Reply:

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.


0

Response Number 8
Name: ghostdog
Date: May 19, 2009 at 17:47:17 Pacific
Reply:

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.


0

Response Number 9
Name: Mechanix2Go
Date: May 20, 2009 at 02:36:01 Pacific
Reply:

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


0

Response Number 10
Name: ghostdog
Date: May 20, 2009 at 05:10:58 Pacific
Reply:

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.


0

Response Number 11
Name: Mechanix2Go
Date: May 20, 2009 at 05:57:16 Pacific
Reply:

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


0

Response Number 12
Name: ghostdog
Date: May 20, 2009 at 06:07:11 Pacific
Reply:

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.


0

Response Number 13
Name: Mechanix2Go
Date: May 20, 2009 at 06:20:28 Pacific
Reply:

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


0

Response Number 14
Name: ghostdog
Date: May 20, 2009 at 06:32:20 Pacific
Reply:

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 :)


0

Response Number 15
Name: Mechanix2Go
Date: May 20, 2009 at 08:46:28 Pacific
Reply:

Hi ghostdog,

Thanks for that.

I meant compile python code to exe.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 16
Name: ghostdog
Date: May 20, 2009 at 17:06:54 Pacific
Reply:

yes, there's py2exe.


0

Response Number 17
Name: Mechanix2Go
Date: May 21, 2009 at 01:00:18 Pacific
Reply:

Got it. Thanks.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 18
Name: Taurus666
Date: May 28, 2009 at 22:25:36 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file

Batch file to count a specific character in a www.computing.net/answers/programming/batch-file-to-count-a-specific-character-in-a/19751.html

FTP batch file www.computing.net/answers/programming/ftp-batch-file/9027.html

batch file www.computing.net/answers/programming/batch-file/13936.html