Link to home
Start Free TrialLog in
Avatar of Rizuan
RizuanFlag for Malaysia

asked on

rename file based on timestamp

I have these commands in my windows batch script.
ren PCAF_RAS_WIN.sds PCAF_RAS_WIN_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%.sds
ren win_ras.ssm win_ras_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%.ssm

The issue is the command not able to rename the file with the time stamp if the current hour is one digit example from 1 -9 hr.

Appreciate your advice.
Avatar of BillDL
BillDL
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Rizuan

Could you please run these commands:
echo %DATE%
echo %TIME%
and post the results.

I'm in the UK and changed my time to Malaysia Time Zone (GMT+8) but when I echo the %date% it still just uses the DD/MM/YYYY format.  Judging from your variables when you echo the %date% it shows with day name in front of the date like "Sun DD/MM/YYYY"

I presume that for the %time% you are getting a leading blank space when the hour is 1 to 9.  So, it is 01:03 here at the moment and for %time% I get
" 1:03:50.76"

If that is the case, then try this IF statement that should test whether the first character of %time% is a blank space, and will use the 2nd character with a leading zero added.

@echo off
echo The full Time is: "%time%"
if "%time:~0,1%"==" " (
    set T=0%time:~1,1%-%time:~3,2%
) else (
    set T=%time:~0,2%-%time:~3,2%
)
echo The modified time as HMM is: "%T%"
echo.
echo ren "filename.sds" "filename_%date:~10,4%%date:~4,2%%date:~7,2%_%T%"
pause
If you can start the routine by setting a variable to the value of %time% with a preceding zero, and then modify that variable to take the last 11 characters. Then use the new environment variable in the following code instead of %time%.

For example:

set nt=0%time%
set nt=%nt:~-11%
ren PCAF_RAS_WIN.sds PCAF_RAS_WIN_%date:~10,4%%date:~4,2%%date:~7,2%_%nt:~0,2%%nt:~3,2%.sds
ren win_ras.ssm win_ras_%date:~10,4%%date:~4,2%%date:~7,2%_%nt:~0,2%%nt:~3,2%.ssm

Open in new window

Try this:

if "%time:~0,1%"==" " (set hour=0%hour:~1,1%) else (set hour=%hour:~0,2%)

ren PCAF_RAS_WIN.sds PCAF_RAS_WIN_%date:~10,4%%date:~4,2%%date:~7,2%_%hour%%time:~3,2%.sds

ren win_ras.ssm win_ras_%date:~10,4%%date:~4,2%%date:~7,2%_%hour%%time:~3,2%.ssm
Oops! I meant:

Try this:

if "%time:~0,1%"==" " (set hour=0%time:~1,1%) else (set hour=%time:~0,2%)

ren PCAF_RAS_WIN.sds PCAF_RAS_WIN_%date:~10,4%%date:~4,2%%date:~7,2%_%hour%%time:~3,2%.sds

ren win_ras.ssm win_ras_%date:~10,4%%date:~4,2%%date:~7,2%_%hour%%time:~3,2%.ssm
SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, good point about creating a time stamp that will be the same for both files being renamed, Bill.  I hadn't considered that aspect.  A better approach altogether and much neater.
@Rizuan:
What date format are you looking fore?
Avatar of Rizuan

ASKER

Thanks for the solutions. I will let you guys know the results once i test them tomorrow morning.

ReneGe,
The format that i require as below:
PCAF_RAS_WIN_yyyymmdd_hhmm.sds. Example: PCAF_RAS_WIN_20111202_1500.sds
win_ras_yyyymmdd_hhmm.sds. Example: win_ras_20111202_1500.ssm
Here another easy to read method.
I learned this from BillDL, billprew, Paul, DragonIT, and the Qs.
With WMIC, it will work whatever your system date and time formats are.
Also, you can easily and cleanly add files to be renamed.

Cheers
@ECHO OFF

CALL :GetDateTime
CALL :RenameFile "PCAF_RAS_WIN.sds"
CALL :RenameFile "win_ras.ssm"

PAUSE
EXIT

:RenameFile
REN "%~f1" %~n1_%yy%%mm%%dd%_%hh%%min%%~x1"
EXIT /b

:GetDateTime
FOR /F "tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Year^,Month^,Day^,Hour^,Minute^,Second ^| FINDSTR /R [0123456789]') do (
	IF %%A GEQ 10 (set dd=%%A) ELSE (set dd=0%%A)
	IF %%B GEQ 10 (set hh=%%B) ELSE (set hh=0%%B)
	IF %%C GEQ 10 (set min=%%C) ELSE (set min=0%%C)
	IF %%D GEQ 10 (set mm=%%D) ELSE (set mm=0%%D)
	IF %%E GEQ 10 (set ss=%%E) ELSE (set ss=0%%E)
	IF %%F GEQ 10 (set yy=%%F) ELSE (set yy=0%%F)
)

EXIT /b

Open in new window

Avatar of Bill Prew
Bill Prew

@ReneGe

Doesn't Year return a 4 digit integer, which would always be larger than 10?

~bp
@Billprew:
Absolutely. I just copy past the command line from %%A to %%F, so blame it to my sunday lazy fingers ;)

Thanks Bill !!

@Rizuan:
You can leave my version as is. Or just change line 21 to:
set yy=%%F