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

26,890

Solution 1

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"

Solution 2

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.

Share:
26,890
GCSTider
Author by

GCSTider

Updated on July 09, 2022

Comments

  • GCSTider
    GCSTider almost 2 years

    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% –
    
    • createproblem
      createproblem over 10 years
      What operating system? Linux / Windows / Mac? Give us some informations about the files names... example?
    • GCSTider
      GCSTider over 10 years
      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
    • GCSTider
      GCSTider over 10 years
      I've tried: 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%
    • GCSTider
      GCSTider over 10 years
      Forgot the OS. Using a Windows XP desktop
    • GCSTider
      GCSTider over 10 years
      I've got a text file that displays an alarm printer. I would like to set up a batch file 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.
    • Textmode
      Textmode over 10 years