1.

Solve : bat file to rename files with julian date?

Answer»

First of all, I am a new member, so bear with me if I make some mistakes in protocol.

I am running XP pro 5.1 SP2.

I think I have a relatively unique situation as far as renaming files in a batch file. I have searched this site as well as others, and have not found a solution.

The situation is that there will be one file in the directory where my bat file will be, and all I know is that it begins with a small L with a txt extension; ie l*.txt. What I need to do is rename it to TPC_PMG_YYYYDDD.TXT. Where YYYY is the current year and DDD is the current Julian date. (And TPC_PMG_ is a constant).

Any help on this would be greatly appreciated.Batch code does not do date arithmetic. I did find a long convoluted function written in VBScript, but you could simplify the code by building a loop starting with Jan 1 and consecutively adding 1 to the date result until you reach the correct Gregorian date. The number of times through the loop would be the Julian day.



Even with all the date functions in VBScript, the Julian calendar seems to have slipped through the cracks.

Edit: Or you could use the DateDiff function between the current date and Jan 1.Hi Arcdude - WELCOME to CH.

Below is a crude code which will hopefully suit your purpose. It works for me but has not been fully tested, leave that to you.

If there is more than one l*.txt file in the folder the script will fail with the error MESSAGE "A duplicate file exists, or the file cannot be found."

Code: [Select]@echo off
cls

:: Extract current day, month and year
SET day=%date:~7,2%
set month=%date:~4,2%
set year=%date:~-4%

:: .....................................................

:: Set day number at end of each month Jan thru' Nov
set Jan=31
set/a Feb=%Jan%+31
set/a Mar=%Feb%+28

:: =====================================================

:: Set day number at end of Mar if leap year (years to 2020 only)

if %year% equ 2008 set/a Mar=%Feb%+29
if %year% equ 2012 set/a Mar=%Feb%+29
if %year% equ 2016 set/a Mar=%Feb%+29
if %year% equ 2020 set/a Mar=%Feb%+29

:: =====================================================

set/a Apl=%Mar%+30
set/a May=%Apl%+31
set/a Jun=%May%+30
set/a Jul=%Jun%+31
set/a Aug=%Jul%+31
set/a Sep=%Aug%+30
set/a Oct=%Sep%+31
set/a Nov=%Oct%+30

:: .....................................................

:: Set day number
if %month% equ 1 set daynumber=%day%
if %month% equ 2 set/a daynumber=%Jan%+%day%
if %month% equ 3 set/a daynumber=%Feb%+%day%
if %month% equ 4 set/a daynumber=%Mar%+%day%
if %month% equ 5 set/a daynumber=%Apl%+%day%
if %month% equ 6 set/a daynumber=%May%+%day%
if %month% equ 7 set/a daynumber=%Jun%+%day%
if %month% equ 8 set/a daynumber=%Jul%+%day%
if %month% equ 9 set/a daynumber=%Aug%+%day%
if %month% equ 10 set/a daynumber=%Sep%+%day%
if %month% equ 11 set/a daynumber=%Oct%+%day%
if %month% equ 12 set/a daynumber=%Nov%+%day%

:: ......................................................

:: CONVERT day number to three digits if <100
if %daynumber% lss 100 set daynumber=0%daynumber%

:: ......................................................

Rename file
ren l*.txt TPC_PMG_%year%%daynumber%.txt

:: ......................................................




Good luck

Edit: Error...the line to convert the day number to 3 digits does not do so if the date is <10. Amendment to follows asap.

2nd Edit: Amended code posted.

Thanks. I will try this. Multiple l*.txt files will not be a problem.



Discussion

No Comment Found