How to Archive Log Files and Free Up Hard Drive Space with 7zip and Batch File
By: Date: May 6, 2023 Categories: Windows Tags: , , , , ,

If your like me you always been looking for a great archive script that can take log files and archive them off to another drive. The idea is to free up hard drive space by compressing the files, storing them on another drive, and them removing the original log files safely.  Using 7zip and the below batch file you can easily achieve this.


This script is designed to compress and delete log files older than a specified number of days in a given directory on a Windows system using 7-Zip. Here’s a detailed explanation of what the script does:

  1. Set variables: The script sets several variables at the beginning, such as the log directory path, output directory path, the number of days old for files to be processed, the compression rate, the compression algorithm, the path to the 7-Zip executable, the log file path, and the file list path.
  2. Get the current date: The script extracts the current date in the YYYY-MM-DD format.
  3. Write the header to the log file: The script writes the start time and the compression settings to the log file.
  4. Create a list of log files older than the specified days: The script uses the forfiles command to search for log files in the specified log directory that are older than the specified number of days. It saves the list of file paths to a temporary text file.
  5. Check if the file list is empty: The script checks if any files meet the criteria. If no files are found, it logs a message stating that no files were found, skips the compression and deletion steps, and moves to the end of the script.
  6. Compress the files: If there are files meeting the criteria, the script compresses all the files in the list using the specified compression rate and algorithm with 7-Zip. The output is a single compressed archive with the current date in its name.
  7. Delete the original files: If the compression is successful, the script iterates through the list of files and deletes them one by one. It also logs the deletion of each file in the log file.
  8. Delete the temporary file list: The script deletes the temporary file list that was created earlier.
  9. Write the footer to the log file: The script writes the finish time to the log file, indicating that the script has completed its execution.

The script is designed to be run as a batch file on a Windows system, and it compresses and deletes log files in a specified directory that are older than a specified number of days using 7-Zip. The script logs its actions in a separate log file in the output directory.

@echo off
setlocal

:: Set variables
set log_directory=C:\logs
set output_directory=D:\logs
set days_old=30
set compression_rate=9
set compression_algorithm=LZMA2
set _7zip_path="C:\Program Files\7-Zip\7z.exe"
set log_file=%output_directory%\compression_deletion_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt
set file_list=%output_directory%\file_list.txt

:: Get the current date in YYYY-MM-DD format
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)

:: Write header to log file
echo [%date% %time%] Script started >> %log_file%
echo Compressing log files older than %days_old% days with compression rate %compression_rate% and %compression_algorithm% algorithm >> %log_file%

:: Create a list of log files older than specified days
echo Creating file list...
forfiles /p %log_directory% /s /m *.log /d -%days_old% /c "cmd /c echo @path" >> %file_list%

:: Check if the file list is empty
if %errorlevel%==1 (
    echo No files found that meet the criteria >> %log_file%
    goto :end_script
)

:: Compress all log files in the list using specified compression rate and algorithm
echo Compressing files...
%_7zip_path% a -m0=%compression_algorithm% -mx%compression_rate% %output_directory%\logs_%mydate%.7z @"%file_list%"

:: If compression is successful, delete log files in the list
if %errorlevel%==0 (
    for /f "delims=" %%i in (%file_list%) do (
        echo Deleting %%i
        del /f /q "%%i"
        echo [%date% %time%] Deleted %%i >> %log_file%
    )
) else (
    echo [%date% %time%] Failed to compress files >> %log_file%
)

:: Delete the file list
del /f /q %file_list%

:end_script
:: Write footer to log file
echo [%date% %time%] Script finished >> %log_file%

endlocal

To set up a scheduled task to run the script daily, follow these steps:

  1. Open Task Scheduler: Press the Windows key + R to open the Run dialog box, then type “taskschd.msc” and press Enter.
  2. Create a new task: In the Task Scheduler, click on the “Create Task” option in the Actions panel on the right.
  3. Enter the task details: In the “General” tab of the “Create Task” dialog box, enter a name and description for the task. Select the “Run whether the user is logged on or not” option, and select the option to run with the highest privileges. Set the operating system to Windows 10 and above, and select the architecture that matches your system.
  4. Set the trigger: In the “Triggers” tab, click the “New” button to create a new trigger. Select “Daily” and set the time you want the task to run. Set the task to recur every day.
  5. Set the action: In the “Actions” tab, click the “New” button to create a new action. Select “Start a program” as the action type, and enter the full path to the script in the “Program/script” field. You can also enter any optional arguments or working directory.
  6. Set the conditions: In the “Conditions” tab, you can set conditions such as whether to run the task only when the computer is idle, or whether to wake up the computer to run the task.
  7. Set the settings: In the “Settings” tab, you can set various settings such as whether to stop the task if it runs longer than a certain time, whether to restart the task if it fails, and whether to delete the task after a certain period of time.
  8. Save the task: Click the “OK” button to save the task.

After setting up the scheduled task, the script will run daily at the specified time, compressing and deleting the log files in the specified directory that are older than the specified number of days.

Leave a Reply

Your email address will not be published. Required fields are marked *