How to use batch file to rename file to include date?

I have a text file that displays an emergency printer. I would like to configure the batch file under Windows XP to change the default name for the alarm printer to include the date, which would greatly facilitate the search for errors. The alarm printer is written to a text file.

I managed to change the name, but every time I try to set the name to a date, nothing happens or instead of a name instead of a code.

So far i 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% – 
+6
source share
2 answers

Something like this should work on XP:

 ren "C:\TPM 4 Alarm Printer\test.txt" "%date:~4,2%-%date:~7,2%-%date:~10,4%.txt" 

Just adapt it for your problem.

+6
source

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" 
+6
source

Source: https://habr.com/ru/post/954169/


All Articles