Renaming a file to include dates...

May 24, 2011 at 07:20:47
Specs: Windows 7
I have files that are automatically generated every two weeks. I need to be able to get them renamed to put the date range for that particular file. They are payroll files, and run on a 2 week cycle. The date would need to include the start day of the payroll cycle and the end day. I am planning on running this the day after the end of the payroll cycle, so it would need to be from 15 days ago until yesterday. I am planning on doing this every two weeks, but want to use the system date as a starting point. Any ideas?

See More: Renaming a file to include dates...

Report •

May 25, 2011 at 03:36:48
I've knocked this batch/vb script together to show one method of calculating/manipulating dates. It is designed to change the name of the current payroll file only, if you want to rename older files that's another subject. The script has been run only in Windows XP, leave the testing on Win 7 to you. Note that the date format shown is to suit my system date format (day dd/mm/yyyy) you may need to adjust the script to suit your system date format.

@echo off
setlocal enabledelayedexpansion

set vbs=%temp%\vbs.vbs

set eval=1&call :calc
set yesterday=%newdate%

set eval=15&call :calc
set days-ago=%newdate%

set Yesterday=%yesterday:~-4%%yesterday:~7,2%%yesterday:~4,2%
set days-ago=%days-ago:~-4%%days-ago:~7,2%%days-ago:~4,2%

echo Yesterday=%yesterday%
echo 15-days-ago=%days-ago%

:: renaming commands go here  <<------------------------------e.g.
:: ren "path\filename.ext" "%days-ago%-%yesterday%-filename.ext"

del %vbs%
exit /b

> %vbs% echo WScript.Echo DateAdd("d",-%eval%,Date)
for /f "tokens=* delims=" %%a in ('cscript //nologo %vbs%') do (
    set newdate=%%a)

Good luck.

Please come back & tell us if your problem is resolved.

Report •

May 25, 2011 at 05:31:14
Thanks so much. I'll give it a shot and let you know.

Report •

Related Solutions

Ask Question