[Windows] Automatically delete log files that are no longer needed on a regular basis
table of contents
Hello.
I'm Kawa from the System Solutions Department, and the uptime has been about 12 minutes recently
Before you know it, it's spring.
Personally, I love spring the most, and it feels like a fresh start because so many things are new.
If possible, I would like to get rid of the dark history of the past, so this time I will write about Windows log rotation. Starting from spring, start organizing unnecessary files.
Execution environment
Windows 11 Pro
Preparing the batch file
the operation log file of some business application is saved under
C:\Users\testuser\Documents\test This log file is like a perpetual motion machine that is added every day. It's a pain to manually delete them. This time, I will explain a batch file that periodically deletes this after two weeks.
First, create a batch file (.bat format) like the one shown below using a text editor.
"logrotate.bat"
@echo off setlocal set LOGDIR="C:\Users\testuser\Documents\test" forfiles /P "%LOGDIR%" /M *.log /C "cmd /C Del /S @path" /D -14 exit / B 0
Insert this into your Windows task scheduler and you're ready to go.
Program explanation
@echo off setlocal
▶ @echo off prevents the output from being displayed. By using setlocal, the environment variables used in this program will not affect others (localization).
set LOGDIR="C:\Users\testuser\Documents\test"
▶ Assign the target log save destination path to the variable "LOGDIR".
forfiles /P "%LOGDIR%" /M *.log /C "cmd /C Del /S @path" /D -14
▶ It's complicated, so let's break it down into parts.
From the help below,
use /P to refer to the corresponding path,
/M to search for the file (in this case, a file with the extension ".log"),
/C to execute any command, and
/D to find the number of days that have passed Check. This time 14 days = 2 weeks.
forfiles command help FORFILES [/P pathname] [/M search mask] [/S] [/C command] [/D [+ | -] {yyyy/MM/dd | dd}] Description: File (or ) and run the command on that file. This is useful for using batch jobs. Parameter list: /P pathname Indicates the path to start the search. The default folder is the current running directory (.). /M search mask Search for files by search mask. The default search mask is '*'. /S Tells forfiles to also process subdirectories (eg "DIR /S"). /C command Indicates the command to execute for each file. Enclose the command string in double quotes. The default command is "cmd /c echo @file". The following variables can be used in the command string: @file - returns the name of the file. @fname - Returns the file name without extension. @ext - Returns only the file extension. @path - Returns the full path of the file. @relpath - Returns the relative path of the file. @isdir - Returns "TRUE" if the file type is a directory, "FALSE" if it is a file. @fsize - Returns the size of the file in bytes. @fdate - Returns the date the file was last modified. @ftime - Returns the last modification time of the file. If you use special characters on the command line, specify the characters in hexadecimal code in the format 0xHH (for example, 0x09 for tab). "cmd /c" is required before CMD.EXE's internal commands. /D Date Selects files whose last modified date is on or after the specified date (+) or on or before the specified date (-) using the format "yyyy/MM/dd". Or, select files whose last modified date is "dd" days later or "dd" days before the current date. Valid "dd" values are between 0 and 32768. If not specified, "+" is used by default. /? Display help or usage information.
"cmd /C Del /S @path"
▶ At this point, use the deletion command "Del" and /S @path to delete the specified file from all subdirectories and display the deleted file name.
exit /B 0
▶ By adding "/B" to exit, you can terminate the batch, and by specifying exit code "0", you can terminate cmd.exe without returning an error.
Execution result
You can debug by manually running the following from the command prompt.
Place the file appropriately and check the result without the /D option.
testuser> forfiles /P "%LOGDIR%" /M *.log /C "cmd /C Del /S @path" Deleted files - C:\Users\testuser\Documents\test\test.log Deleted files - C :\Users\testuser\Documents\test\test1.log Deleted file - C:\Users\testuser\Documents\test\test2.log
It was successfully deleted.
If you can make it this far, you can take a breather. Let's configure the task scheduler.
Add to task scheduler
Open Task Scheduler using Windows search.
Click [Task Scheduler Library] > [Create Task] from the menu
▶ On the [General] tab, configure the task name and permission settings such as "Run only when logged on". Adjust the security options as you like depending on what you want to do.
▶ Set the execution timing on the [Trigger] tab. This time, it will be held every Sunday at 0:00 from February 9th.
▶ Finally, select the program file you created from the [Operation] tab.
If you set it to Yoshina, you will no longer have to worry about PC capacity, and you will definitely be able to use it from April! Don't look back on the past and do your best, new adults!
~Complete~