1.

Solve : Finding and formatting the time of a file creation?

Answer»

Hi All.

I am wanting to write a batch file to rename a file with a date and time in the filename.

I have managed to write one that compiles the date into YYYYMMDD and the time into hhmm but I cant for the life of me make it get the seconds as well.

Below is my code for the part I have working. It produces hours and mins but the secs variable is of the form "a.m." rather than a number. I know this is because of the /t in this line "FOR /F "TOKENS=*" %%A IN ('TIME /t') DO SET TIME=%%A"

I know that I should be able to change the time /t to just time but then the code hangs for some reason, Im thinking its SOMETHING to do with the tokens bit or the delims bit.

Any advice would be greatly appreciated ESPECIALLY if it retrieves the seconds but not the decimal part of the time returned when the time command is used.

Cheers

Chris

==========================
for /f "tokens=1-8 delims=.:/- " %%d in ('date /t') do set date=%%g%%f%%e

FOR /F "TOKENS=*" %%A IN ('TIME /t') DO SET TIME=%%A
:: Delims is a TAB followed by a space
FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v itime') DO SET iTime=%%B
FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v stime') DO SET sTime=%%B
FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO (
SET HOUR=%%A
SET MINS=%%B
SET SECS=%%C
)
=======================================

I'm almost embarassed to post this, figuring I must have missed something. As far as I know, the registry keeps neither the date or the time; only settings on how they and other types of data are formatted.

This snippet will extract the date and time:

Code: [Select]set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%

You can then use the %dt% and %tm% variables to rename your file:

ren fileA.xxx fileA-%dt%-%tm%.xxx

The above is just an example; you can format the renamed file label as you wish.thats AWESOME THANKS very muchly....

The registry bit was to derermine international settings and the delimiter of the time which became obsolete as I was hardcoding the delimiters...sigh

Thanks again for the snippet.

Cheers

Chris



Discussion

No Comment Found