Computing.Net > Forums > Windows XP > manipulating dates in file names

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

manipulating dates in file names

Reply to Message Icon

Name: NOTS3W
Date: June 5, 2006 at 21:59:24 Pacific
OS: XP Home SP 2
CPU/Ram: Pentium 4 1.8 Ghz / 512 M
Product: Dell Dimension 4500
Comment:

I'm looking for a way in a batch file to rename a generic file (i.e. RECORDSWK.TXT) with yesterday's date (i.e. yyyymmddWK.TXT). The files are created each Sunday morning and contain the previous week's records, so that on June 4, 2006, the RECORDSWK.TXT file contains the records for the week ending June 3 and needs to be named 20060603WK.TXT.

On the first day of the month, another file is created that contains all of the previous month's records. On June 1, 2006, the file RECORDSMO.TXT needs to be renamed 200605MO.TXT.

I need to take the current date, decrement by one day, then take only the year, month and day or only the year and month of that date to create the new file name.

Is there a way to do this in a batch file in XP?

Thanks for any help you can provide.



Sponsored Link
Ads by Google

Response Number 1
Name: CyberSlug
Date: June 6, 2006 at 06:13:49 Pacific
Reply:

Not a batch file, but this uses the FREE scripting language AutoIt v3:

;This first AutoIt script http://www.autoitscript.com/autoit3/ could be compiled
; and set to run every Sunday using XP's "Scheduled Tasks" in the Control Panel
;Rename RECORDSWK.TXT as per message 152153

#include <Date.au3>
FileChangeDir("C:\path\where\the\files\are")

$today = @Year & "/" & @Mon & "/" & @Mday
$yesterday = _DateAdd("D", -1, $today)
$yesterday = StringReplace($yesterday, "/", "") ;remove slashes

FileMove("RECORDSWK.TXT", $yesterday & "WK.TXT")
Exit



;This second AutoIt script http://www.autoitscript.com/autoit3/ could be compiled
; and set to run first day of each month using XP's "Scheduled Tasks" in the Control Panel
;Rename RECORDSMO.TXT as per message 152153

#include <Date.au3>
FileChangeDir("C:\path\where\the\files\are")

$today = @Year & "/" & @Mon & "/" & @Mday
$yesterday = _DateAdd("D", -1, $today)

$year = StringLeft($yesterday, 4)
$month = StringMid($yesterday, 6, 2)

FileMove("RECORDSMO.TXT", $year & month & "MO.TXT")
Exit



0

Response Number 2
Name: NOTS3W
Date: June 6, 2006 at 11:50:33 Pacific
Reply:

I appreciate the time you spent working that out. I'm hoping to avoid any third party products, however, for several reasons. For one, it means having the utility available on any machine where my routine is run. For another (quoting from the AutoIt web site): "AutoIt v3 has a completely different syntax to AutoIt2 so old scripts are not compatible." I don't want to be forced to rebuild everything with each new version of AutoIt or Windows.

I've actually inched closer to the solution this morning. I've discovered how to parse out the day, month, year and date from DATE and I now know how to subtract 1 from most of those values. Unfortunately, 06-1=5, not 05.

Thanks for your effort. I'm just not ready to abandon the internal batch possibilities yet.


0

Response Number 3
Name: CyberSlug
Date: June 6, 2006 at 18:28:57 Pacific
Reply:

VBScript would probably work just as well, but AutoIt is easier in my opinion.

Just to clarify:
- AutoIt does not need to be installed on every machine because the script can be compiled into a stand-alone EXE.
- The current syntax is not chaning anytime soon; and it doesn't matter because no one will force you to upgrade. Old scripts will still work. (Some people still actively use v2.)

Whatever you decide, just be careful with RECORDSWK.TXT when yesterday's date falls on the previous month.


0

Response Number 4
Name: Mechanix2Go
Date: June 6, 2006 at 20:20:28 Pacific
Reply:

Hi NOTS,

Since you've gotten as far as the "5 not 05" snag, I guess you're ready to experience the thrill of victory and the agony of defeat.

The programming forum here is shot through with 'date math' issues. Very few end happily.

I'll show you shortly how to work the problem of the leading 0 in a 2 place MM or DD. But before we get there and keeping always in mind the 'version frolics'...

Parsing the DATE is, in itself, a crapshoot. Even ignoring languanges, the DATE layout will vary according to winders version, regional settings, CMD/COMMAND and other variables.

So far the only really promising approaches would seeem to be the 'Julian date', for which see posts by user dtech10 in programming forum; or a brute force lookup table:

if %TODAY%==20060607 set yesterday=20060606
...
ad nauseum

The batch nelow uses SYSTEM [ie BIOS] date, so it's not version/regional dependent. Note, however, that it requires XP/2K or maybe NT4. It will NOT work in DOS.

::== YMD6.bat
:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof
:: DONE


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

M2


0

Response Number 5
Name: NOTS3W
Date: June 6, 2006 at 22:04:29 Pacific
Reply:

Hmmm. This is what I eventually came up with:

for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set todaydow=%%a& set todaymonth=%%b& set todayday=%%c& set todayyear=%%d)

set yesterdayyear=%todayyear%
set /a yesterdaymonth=todaymonth
set /a yesterdayday=todayday-1
if %yesterdayday% lss 10 set yesterdayday=0%yesterdayday%
if %yesterdayday%==00 set /a yesterdaymonth=todaymonth-1
if %yesterdaymonth% lss 10 set yesterdaymonth=0%yesterdaymonth%
if %yesterdaymonth%==00 set /a yesterdayyear=todayyear-1
if %yesterdaymonth%==00 set /A yesterdaymonth=12
if %yesterdayday%==00 if %yesterdaymonth%==02 set yesterdayday=28
if %yesterdayday%==00 if %yesterdaymonth%==04 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==06 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==09 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==11 set yesterdayday=30
if %yesterdayday%==00 set yesterdayday=31

It ain't purty but I think it works.

From there, it's easy to put together yesterday's file name:
%yesterdayyear%%yesterdaymonth%%yesterdayday%.txt

Or last month's file name:
%yesterdayyear%%yesterdaymonth%.txt

I THINK the only problem is that if today is March 1 of a leap year, I'd set yesterday's date to 02/28/yy when it really should be 02/29/yy. For my purposes, the only time that comes into play is if today is also a Sunday. Only then do I care what yesterday's DAY was because I'd want to name last week's file with yesterday's (the end of last week) date. So once every 28 years or so I'd have a file with a date that's one day off of accurate.

Of course, if I checked for leap years, I could fix that, too.

I'm not too concerned about lamguage or regional differences since I know where this will be used, but versions could be a problem. Is there any way to put the system date into an environment variable and parse from there?

Thanks for your help.

Ray


0

Related Posts

See More



Response Number 6
Name: NOTS3W
Date: June 6, 2006 at 22:43:53 Pacific
Reply:

M2,

This fixes the leap year problem. The next time March 1 falls on a Sunday in a leap year is in 2020 (I'll hopefully be retired). But this will now work regardless of the day of the week. The day before the 1st of March in a leap year is now February 29.

for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set todaydow=%%a& set todaymonth=%%b& set todayday=%%c& set todayyear=%%d)

set /a leapyeartest=todayyear/4*4

set yesterdayyear=%todayyear%
set /a yesterdaymonth=todaymonth
set /a yesterdayday=todayday-1
if %yesterdayday% lss 10 set yesterdayday=0%yesterdayday%
if %yesterdayday%==00 set /a yesterdaymonth=todaymonth-1
if %yesterdaymonth% lss 10 set yesterdaymonth=0%yesterdaymonth%
if %yesterdaymonth%==00 set /a yesterdayyear=todayyear-1
if %yesterdaymonth%==00 set /A yesterdaymonth=12
if %yesterdayday%==00 if %yesterdaymonth%==02 if %yesterdayyear%==%leapyeartest% set yesterdayday=29
if %yesterdayday%==00 if %yesterdaymonth%==02 if not %yesterdayyear%==%leapyeartest% set yesterdayday=28
if %yesterdayday%==00 if %yesterdaymonth%==04 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==06 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==09 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==11 set yesterdayday=30
if %yesterdayday%==00 set yesterdayday=31

Ray


0

Response Number 7
Name: Mechanix2Go
Date: June 6, 2006 at 23:39:30 Pacific
Reply:

Hi Ray,

I applaud your effort.

A couple cautions:

[1] Your for line is hard wired for a particular date layout which is bound to cause problems. That's why I put up YMD6.bat

[2] I don't have the energy right now to phony up a matching date layout to match yours, but be aware thst if you try to do a set /a on either 08 or 09 it will fail. Because some idiot at M$ decided that 0x is OCTAL notation; so naturally 08 and 09 are NG. [Who the heck uses OCTAL?]


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

M2


0

Response Number 8
Name: trvlr
Date: June 7, 2006 at 12:07:24 Pacific
Reply:

Don't people (e.g Martians) with eight fingers and toes use octal...; whilst the rest of (.e.g Terrans...) use decimal?


0

Response Number 9
Name: Mechanix2Go
Date: June 7, 2006 at 13:41:46 Pacific
Reply:

Hi trvlr,

Well sheee-it! Forgot about them.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

sata driver??.. problems in installing pr...



Post Locked

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


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: manipulating dates in file names

Batch file with latest date in file name www.computing.net/answers/windows-xp/batch-file-with-latest-date-in-file-name/180855.html

wildcharacters in file name www.computing.net/answers/windows-xp/wildcharacters-in-file-name/126298.html

Using DOS Rename FIles with random file names www.computing.net/answers/windows-xp/using-dos-rename-files-with-random-file-names/178014.html