Help renaming a .txt file in a .BAT file


  1. Posts : 19,383
    Windows 10 Pro x64 ; Xubuntu x64
       #1

    Help renaming a .txt file in a .BAT file


    G'day,

    I wonder if someone could point me in the right direction?

    I have a .BAT file that uses robocopy to backup my data. Part of the robocopy saves the reuslts into a log file called backup_log.txt

    At the end of the .BAT file, once robocopy has been completed and the .txt file has been created, I would like to rename it such that the name of the .TXT file references the data and time.

    In the CMD window I see I have access to the date /T command, but for the life of me I cannot figure out how to concatenate these commands with the .txt file. Its easy using the Unix move command..........but I'm stumped in CMD.

    My goal is to rename backup_log.txt to Sat 10/12/2011_backup_log.txt.

    Can anyone make some suggestions?

    Thanks,
    Golden
      My Computer


  2. Posts : 845
    Windows 7 - Vista
       #2

    Hi -

    Try this batch code -
    Code:
    
    @echo off
    set x=_backup_log.txt
    For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set d1=%%a_%%b-%%c-%%d) 
    set x2=%d1%%x%
    ren backup_log.txt %x2%
    
    If backup_log.txt is still in-use (you get an "access denied" error), use copy in place of rename -

    Code:
    
    @echo off
    set x=_backup_log.txt
    For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set d1=%%a_%%b-%%c-%%d) 
    set x2=%d1%%x%
    copy backup_log.txt %x2%
    
    End result = backup_log.txt is renamed/copied to Sat_12-10-2011_backup_log.txt - if run today.

    Regards. . .

    jcgriff2

    `
      My Computer


  3. Posts : 19,383
    Windows 10 Pro x64 ; Xubuntu x64
    Thread Starter
       #3

    Excellent - that did the trick. Thanks mate.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:20.
Find Us