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.
RizuanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BillDLCommented:
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
0
jgyoderCommented:
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

0
Paul TomasiCommented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Paul TomasiCommented:
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
0
Paul TomasiCommented:
Better off doing it this way:


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

set timestamp=%date:~10,4%%date:~4,2%%date:~7,2%_0%hour%%time:~3,2%

ren PCAF_RAS_WIN.sds PCAF_RAS_WIN_%timestamp%.sds
ren win_ras.ssm win_ras_%timestamp%.ssm
0
Bill PrewCommented:
Another simple approach, and fairly clean code.  Setting a variable to the time once also makes sure both files are stamped with the same time stamp.  Also, enclose the new filename in double quotes, this way if there are spaces in the date time stamp (like you had) it will still do the rename, just leaving the spaces in place.  That's typically better than an error until you can fix it.

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

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BillDLCommented:
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.
0
ReneGeCommented:
@Rizuan:
What date format are you looking fore?
0
RizuanAuthor Commented:
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
0
ReneGeCommented:
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

0
Bill PrewCommented:
@ReneGe

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

~bp
0
ReneGeCommented:
@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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.