1.

Solve : date in dos bat file?

Answer»

Can someone help me create a dos bat file that will copy files that have todays date and yesterdays date as part of the file name? The dates should NOT be hard coded into the bat file. I will schedule the bat file to run AUTOMATICALLY each night.

Example: If the bat file was run today, 6/8/2005 I would like it to copy the following two files:

Membership_YYYYMMDD2246.bak where YYYYMMDD is yesterdays date 20050607
and
Membership_tlog_YYYYMMDD0816.trn where YYYYMMDD is todays date 20050608

Any help will be greatly appreciated.The technique involved is OS dependent. What OS are you using?

The current OS is NT 4.0sp6a but I will eventually have to move it Windows 2003
This little piece of doggerel will work with any Microsoft OS:

Code: [Select]
@echo off
For /f "tokens=1-7 delims=:/-,. " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
For /f "tokens=2-4 delims=/-,.() skip=1" %%a in ('echo.^|date') do (
set dow=%%i
set mo=%%j
set dd=%%k
set yyyy=%%l
)
)


This will give you today's variables. Batch does not do date calculations. If NT supports the /a SWITCH for the SET statement, you could do some crude processing by subtracting 1 from the DD variable but that opens a whole can of worms on the first of each month. Otherwise you're SOL.

Personally I would write a script which has excellent date handling routines.

Good luck. THANKS for the example and yes the set command does support the '/a' option so if you have any other suggestions using the /a let me know.

You are right, the problem comes into play with the first of each month.

Thanks again for your help.
Normally I would just code:

set /a yesterday=%dd%-1

but dd is not numeric and the SET will fail. PC-DOS (IBM) used to have a C2D FUNCTION that would be most helpful here but I guess it never made it to MS.

The best I can do is recommend you try scripting (very cool).

PS. If you still insist on using batch, check out:

http://www.robvanderwoude.com/datetiment.html

Scroll down to Yesterday.bat to see what's involved.

Good luck.



Discussion

No Comment Found