Link to home
Start Free TrialLog in
Avatar of dickwanker
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?
Avatar of b9
b9

yyyymmddhhss.txt you mean ... and it can't be done with batch only.  Going to have to write some code.
ASKER CERTIFIED SOLUTION
Avatar of cbo120897
cbo120897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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

Avatar of dickwanker

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=

Carmine...

      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.