Computing Staff
  • 0

How To Rename The Log File With .Bat File

  • 0

Hi, when logging files creation file name is like logfile_20100621.log, I want to change this file name as logfile_20100620.log.Please give me the solution how to rename with previous date using Batch files?

Share

1 Answer

  1. i had this “subdate” routine laying around from another post and recycled it.

    @echo off & setlocal enabledelayedexpansion
    :: filename is from whereever the logfile is derived from: dir, set/p, etc.
    for /f “tokens=1,2 delims=_” %%a in (filename) do set d8=%%~nb & set ff=%%a
    ::—— pseudo-date components creation
    set mo=_%d8:~4,2%
    set day=_%d8:~6,2%
    set yr=%d8:~0,4%
    set mo=%mo:_0=%
    ::—— begin date subtraction
    set /a day=%day:_0=%-1
    if !day! neq 0 goto :ok
    set offs=0
    for /L %%a in (1 1 12) do (
    if %%a equ 8 set /a offs=1
    set /a test=”(%%a+!offs!)%%2″
    set /a nn%%a=30+!test!
    )
    set /a test=”yr%%4″
    set nn2=28
    if !test! equ 0 set /a nn2+=1
    :: foll. just eliminates an IF (test for month=0) by using modulo and offset
    set /a mo=”(%mo%+10)%%12+1″
    if !mo! equ 12 set /a yr-=1
    set day=!nn%mo%!
    ::—— end date subtraction
    :ok
    set newname=!yr!0!mo:~-2!0!day:~-2!
    :: just for debugging: remove “echo” when ready to roll.
    echo ren %filename% %ff%_%newname%.log

    • 0