Home > database >  Creating a Batch file that produces max 10 log files and deletes the oldest
Creating a Batch file that produces max 10 log files and deletes the oldest

Time:03-24

Hey I have a batch script and I want to log it. I want to produce 10 log files.

x1.log x2.log ... x10.log

there should only be max 10 log files in the folder the x1.log is always the newest log created. that means the log files change name when the script runs.

x1.log becomes x2.log when the script runs and a new x1.log is created and x10.log is deleted because x9.log becomes x10.log

I hope I made it understandable.

Have a great day all

IF EXIST N:\projects\Trainee\work\st\M1_Infrastructure_Basics\log\x1.log (
    ren "N:\projects\Trainee\work\st\M1_Infrastructure_Basics\log\x1.log" "x2.log"
    break
    ren "N:\projects\Trainee\work\st\M1_Infrastructure_Basics\log\x2.log" "x3.log"
    ) else (
    echo "file nicht gefunden"
    )

some code but it isnt the right way to solve it i figured out

CodePudding user response:

Here's one way. Do it all in descending sequence.

@echo off
setlocal enabledelayedexpansion
for /l %%i in (9,-1,1) do (
    set /a num=%%i 1
    if exist x%%i.log move /Y "x%%i.log" "x!num!.log" >nul 2>&1
)
call "           
  • Related