Rename a file with the Time

D

David Terrill

I have figured out how to rename a file with the date using the command
below:

ren test.txt test_%date:~10,4%-%date:~4,2%-%date:~7,2%.txt
Example: test.txt to test_2004-11-04.txt

However I would also like to rename a file with the time. Does anyone have
any suggestions?
 
P

Paul R. Sadowski [MVP]

Hello, David:
On Thu, 4 Nov 2004 20:04:13 -0500: you wrote...

DT>
DT> ren test.txt test_%date:~10,4%-%date:~4,2%-%date:~7,2%.txt
DT> Example: test.txt to test_2004-11-04.txt
DT>

I like to use date and time like this for a result that looks like this
20041104.231400

You could remove the first for loop to use just the time. And you could
remove the %%c to not use seconds in the time.

@echo off & setlocal enableextensions
for /f "tokens=1-2" %%a in ("%DATE%") do for /f "tokens=1-3 delims=-/.," %%d
in ("%%b") do set ThisDate=%%f%%d%%e
for /f "tokens=1-4 delims=-:.," %%a in ("%TIME%") do set ThisTime=%%a%%b%%c
set ThisTime=%ThisTime: =%

echo %ThisDate%.%ThisTime%

Regards, Paul R. Sadowski [MVP].
 
G

guard

I have figured out how to rename a file with the date using the command
below:

ren test.txt test_%date:~10,4%-%date:~4,2%-%date:~7,2%.txt
Example: test.txt to test_2004-11-04.txt

However I would also like to rename a file with the time. Does anyone have
any suggestions?

Use #LogDate and #LogTime from the Advanced NT/2K/XP/K3 Command Library.
It's FREE and performs CONSISTENTLY across all four platforms.

#LogDate is always yyyymmdd
#LogTime is always hhnnss

*******

1. CALL ntlib.cmd /Init /Quiet
2. REN "test.txt" "test_%#LogDate%-%#LogTime%.txt"

*******

For more info about #LogDate and #LogTime, including color-keyed examples,
see

(http://TheSystemGuard.com/MtCmds/GetValue/GetLogDate.htm)
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm)

For cross-platform help on internal commands CALL and REN

(http://TheSystemGuard.com/TheGuardBook/CCS-Int/CALL.htm)
(http://TheSystemGuard.com/TheGuardBook/CCS-Int/REN.htm)

To request your FREE copy of the Advanced Command Library, go to
(http://ntlib.com).

*******

The Command Library is constructed using ONLY builtin
commands common to all four platforms (NT/2K/XP/K3).
There is NO BINARY CODE. This is a single .cmd file,
optimized and compressed with full documentation.

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top