AutoHotkey 语言 批量删除指定日期和大小文件的方法

AutoHotkey阿木 发布于 2025-06-12 18 次阅读


AutoHotkey Language: Batch File Deletion Based on Date and Size

AutoHotkey is a powerful scripting language for automating tasks on Windows systems. It allows users to create scripts that can automate repetitive tasks, such as file operations, system configuration, and more. In this article, we will explore how to create an AutoHotkey script that can batch delete files based on their creation date and size.

Introduction to AutoHotkey

AutoHotkey is a scripting language for automating the Windows GUI and general scripting. It allows users to create scripts that can simulate keyboard and mouse events, manipulate windows, and interact with the system. AutoHotkey scripts are written in a simple, easy-to-understand syntax and can be executed directly on Windows systems without the need for additional software.

The Problem: Batch File Deletion Based on Date and Size

Imagine you have a large collection of files on your computer, and you want to clean up by deleting files that were created before a certain date or are larger than a specified size. Manually deleting these files can be time-consuming and error-prone. This is where an AutoHotkey script can help automate the process.

Solution: AutoHotkey Script for Batch File Deletion

To create an AutoHotkey script that deletes files based on their creation date and size, follow these steps:

1. Open Notepad or any text editor.
2. Copy and paste the following script into the text editor:

ahk
NoEnv ; Recommended for performance and compatibility with future AutoHotkey versions
Warn ; Enable warnings to assist with detecting common errors
MaxThreadsPerHotkey 2

SetWorkingDir, %A_ScriptDir% ; Sets the script's working directory to the directory where the script is located

; Define the target directory
targetDirectory := "C:pathtoyourdirectory"

; Define the date and size criteria
deleteBeforeDate := "2023-01-01" ; Change this to the date you want to delete files before
maxFileSize := 1024 1024 10 ; 10 MB, change this to the maximum file size you want to delete

; Function to check if a file meets the criteria
CheckFileCriteria(file) {
fileStat := FileGetAttrib, attribs, %file%
fileTime := FileGetTime, creationTime, %file%, C
fileSize := FileGetSize, %file%

; Check if the file meets the date and size criteria
if (fileTime maxFileSize) {
return true
}
return false
}

; Function to delete files that meet the criteria
DeleteFiles() {
Loop, %targetDirectory%., 2 ; Loop through all files in the target directory
{
if (A_LoopFileAttrib != "D") { ; Skip directories
if (CheckFileCriteria(A_LoopFileLongPath)) {
FileDelete, %A_LoopFileLongPath%
MsgBox, Deleted: %A_LoopFileLongPath%
}
}
}
}

; Run the deletion process
DeleteFiles()

3. Replace `C:pathtoyourdirectory` with the actual path to the directory containing the files you want to delete.
4. Modify the `deleteBeforeDate` variable to the date you want to delete files before.
5. Adjust the `maxFileSize` variable to the maximum file size you want to delete (in bytes).
6. Save the script with a `.ahk` extension, for example, `deleteOldFiles.ahk`.

Explanation of the Script

The script consists of several functions:

- `NoEnv` and `Warn` are directives that optimize the script's performance and enable warnings for common errors.
- `SetWorkingDir` sets the script's working directory to the location of the script itself.
- `targetDirectory` is the path to the directory containing the files to be deleted.
- `deleteBeforeDate` is the date before which files will be deleted.
- `maxFileSize` is the maximum file size of the files to be deleted.
- `CheckFileCriteria` is a function that checks if a file meets the specified date and size criteria.
- `DeleteFiles` is a function that loops through all files in the target directory, checks each file against the criteria, and deletes the files that meet the criteria.
- `MsgBox` is used to display a message box with the name of each deleted file.

Running the Script

To run the script, double-click the `.ahk` file you created. The script will start deleting files that meet the specified criteria. You will see a message box for each file that is deleted.

Conclusion

This AutoHotkey script provides a simple and efficient way to batch delete files based on their creation date and size. By automating this process, you can save time and reduce the risk of errors when cleaning up your files. Remember to always backup your files before running such scripts to prevent accidental data loss.