top of page

Posts

Automatically Delete Log Files in Windows



This time, I will write about organizing unnecessary files 


Execution Environment

Windows 11 Pro


Preparing the Batch File

Let's assume that the operation log files of some business applications are saved under "C:\Users\testuser\Documents\test". Managing these ever-accumulating log files manually can be tedious. This time, I will explain how to prepare a batch file that automatically deletes these files after two weeks.


First, create a batch file (.bat format) like the one below using a text editor.

@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

By incorporating this into the Windows Task Scheduler, you're all set.


Program Explanation

@echo off
setlocal

▶@echo off suppresses the display of the command results. setlocal ensures that the environment variables used in this program do not affect anything else (localization).

set LOGDIR="C:\Users\testuser\Documents\test"

▶Assigns the path of the log files' location to the variable LOGDIR.

forfiles /P "%LOGDIR%" /M *.log /C "cmd /C Del /S @path" /D -14

▶Might seem complicated, so let's break it down.

From the help below,

/P refers to the path,

/M searches for files (in this case, files with the ".log" extension),

/C executes a specified command,

/D checks for the number of days passed. Here, 14 days equals two weeks

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]


Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.


Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).


    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .


    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".


    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped 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 extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.


                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".


    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.


    /?                  Displays this help message.
"cmd /C Del /S @path"

▶In this part, the delete command "Del" and /S @path will delete the specified file from all subdirectories as well, and the name of the deleted file will be displayed.

exit /B 0

▶You can specify "/B" for exit to terminate the batch, or "0" for exit code to terminate cmd.exe without returning an error.



Execution Results

Manually executing the following command in the command prompt allows for debugging. By placing some files and checking the results without the /D option, you can confirm the deletion process.

testuser> forfiles /P "%LOGDIR%" /M *.log /C "cmd /C Del /S @path"
 
Deleted file - C:\Users\testuser\Documents\test\test.log
Deleted file - C:\Users\testuser\Documents\test\test1.log
Deleted file - C:\Users\testuser\Documents\test\test2.log

Deleted files are properly removed. Once you've reached this point, the next step is to configure the Task Scheduler.


Adding to Task Scheduler

Open Task Scheduler via Windows search.

Click [Create Task] from the [Task Scheduler Library] menu.

▶ In the [General] tab, you can set task names and permissions such as "Run only when logged on". Security options can be adjusted as desired depending on what you want to execute.

▶In the [Triggers] tab, set the execution timing. In this case, we set it to midnight every Sunday starting 2/3.

▶ Finally, select the program file you created from the "Action" tab.


If you successfully set up your PC, you will no longer have to worry about PC capacity.


Thank you for reading.



This blog post is translated from a blog post written by Kawa Ken on our Japanese website Beyond Co..




Comments


bottom of page