Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Finding and formatting the time of a file creation  (Read 2307 times)

0 Members and 1 Guest are viewing this topic.

cveale

  • Guest
Finding and formatting the time of a file creation
« on: January 30, 2007, 03:27:30 PM »
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
)
=======================================


Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Finding and formatting the time of a file crea
« Reply #1 on: January 30, 2007, 05:01:17 PM »
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.
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

cveale

  • Guest
Re: Finding and formatting the time of a file crea
« Reply #2 on: January 30, 2007, 05:22:25 PM »
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