Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Batch-File to Rename a File to Include the Date?

I've got a text file that displays an alarm printer. I would like to set up a batch file, under Windows XP, to change the default name of the alarm printer to include the date, which would make searching for errors much easier. The alarm printer is captured to a text file.

I've been able to change the name, but every time I try to set the name to the date either nothing happens or the name is changed to the code instead.

So far I've tried

for /f "tokens=1-5 delims=/ "%d in (%date%) do rename "C:\TPM 4 Alarm Printer\test.txt" %%e-%%f-%%g.txt

and

for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%k
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%j
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%j
set t=%t%_ if "%t:~3,1%"=="_"
set t=0%t% set t=%t:~0,4%
set "C:\Users\e727896\Desktop\test.txt=%d%%t%"
echo %C:\Users\e727896\Desktop\test.txt% –
like image 977
GCSTider Avatar asked Sep 19 '13 13:09

GCSTider


People also ask

How do I batch rename a filename?

To batch rename files, just select all the files you want to rename, press F2 (alternatively, right-click and select rename), then enter the name you want on the first file. Press Enter to change the names for all other selected files.


1 Answers

This will provide a more reliable timestamp if you are using XP Pro.

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

ren "C:\TPM 4 Alarm Printer\test.txt" "Alarm - %fullstamp%.txt"
like image 173
foxidrive Avatar answered Nov 04 '22 05:11

foxidrive