dickwanker
asked on
rename a file as yymmddhhss.txt
I need an NT batch job to read a file "input.txt" and rename it to yymmddhhss.txt based on the time it was created?
yyyymmddhhss.txt you mean ... and it can't be done with batch only. Going to have to write some code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
b9 - "can't be done" I like a challenge
Create a batch file with the following contents
REM all on one line
for /f "skip=5 tokens=1-5 delims=/: " %%i in ('dir /tc "%1"') do ren "%1" %%k%%j%%i%%l%%m.txt
REM end of batch
call it with the filename you want to rename. It does use UK date format you may have to nationalize that. I could add path handling if you require it?
Create a batch file with the following contents
REM all on one line
for /f "skip=5 tokens=1-5 delims=/: " %%i in ('dir /tc "%1"') do ren "%1" %%k%%j%%i%%l%%m.txt
REM end of batch
call it with the filename you want to rename. It does use UK date format you may have to nationalize that. I could add path handling if you require it?
oops, missed a bit:
for /f "skip=5 tokens=1-5 delims=/: " %%i in ('dir /tc "%1"') do ren "%1" %%k%%j%%i%%l%%m.txt &goto end
:end
for /f "skip=5 tokens=1-5 delims=/: " %%i in ('dir /tc "%1"') do ren "%1" %%k%%j%%i%%l%%m.txt &goto end
:end
ASKER
cbo's answer worked fine. However, carmines answer using just a 4 line .cmd file was simpler and easier. Thanks everyone for the help.
carmine.... that's not y2k compliant....
b9
Ok, valid until 2080?
@ECHO OFF
FOR /F "skip=5 tokens=1-5 delims=/: " %%i IN ('dir /tc "%1"') DO (CALL :renit %1 %%k %%j %%i %%l %%m)&GOTO end
:renit
IF %2 LSS 80 (SET yr=2000) ELSE (SET yr=1900)
SET /A yr=yr+%2
REN "%1" %yr%%3%4%5%6.txt
GOTO end
:end
SET yr=
Ok, valid until 2080?
@ECHO OFF
FOR /F "skip=5 tokens=1-5 delims=/: " %%i IN ('dir /tc "%1"') DO (CALL :renit %1 %%k %%j %%i %%l %%m)&GOTO end
:renit
IF %2 LSS 80 (SET yr=2000) ELSE (SET yr=1900)
SET /A yr=yr+%2
REN "%1" %yr%%3%4%5%6.txt
GOTO end
:end
SET yr=
ASKER
Carmine...
You rule!
dickwanker
You rule!
dickwanker
This worked great! Thanks. A bit of an explaination of the way "FOR" works would be nice, I had to play around with it for a while to get it to do what I wanted, but it helped loads.