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

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


AutoHotkey Language: Batch Delete Files by Date

AutoHotkey is a powerful scripting language for automating tasks on Windows systems. It is often used for creating custom scripts to automate repetitive tasks, such as file management, system configuration, and more. In this article, we will delve into the creation of an AutoHotkey script that can batch delete files based on their creation or modification date.

Introduction to AutoHotkey

Before we dive into the script, let's briefly discuss what AutoHotkey is and how it works. AutoHotkey is a scripting language that allows users to create scripts to automate various tasks. It is particularly useful for automating keyboard and mouse inputs, but it can also be used for more complex tasks, such as file management.

AutoHotkey scripts are written in a simple text editor and can be executed directly on Windows systems. The scripts can be as simple as changing the keyboard layout or as complex as automating entire applications.

The Task: Batch Delete Files by Date

The task at hand is to create an AutoHotkey script that can delete files based on their creation or modification date. This can be useful for cleaning up old files that are no longer needed, or for managing disk space on a system.

Script Overview

The script will perform the following steps:

1. Prompt the user for the directory to search for files.
2. Prompt the user for the date criteria (creation or modification date).
3. Search the specified directory for files that match the date criteria.
4. Confirm with the user before deleting the files.
5. Delete the files if confirmed.

Writing the Script

Below is the AutoHotkey script that accomplishes the task described above:

ahk
; Prompt the user for the directory to search
InputBox, directory, Directory, Please enter the directory to search for files:, , 200, 100
IfError, directory
MsgBox, No directory specified. Exiting script.
ExitApp

; Prompt the user for the date criteria
InputBox, dateType, Date Type, Choose the date type for deletion:`n1 - Creation Date`n2 - Modification Date, , 200, 100
IfError, dateType
MsgBox, No date type specified. Exiting script.
ExitApp

; Set the date type variable
If (dateType = 1)
dateType := "CreationDate"
Else If (dateType = 2)
dateType := "LastWriteTime"
Else
MsgBox, Invalid date type specified. Exiting script.
ExitApp

; Prompt the user for the date
InputBox, date, Date, Please enter the date in YYYY-MM-DD format:, , 200, 100
IfError, date
MsgBox, No date specified. Exiting script.
ExitApp

; Convert the date to a timestamp
dateTimestamp := DateToTimestamp(date)

; Search for files in the directory
Loop, Files, %directory%., 2
{
; Get the file's timestamp
fileTimestamp := FileGetTime(A_LoopFileName, dateType)

; Compare the file's timestamp with the user's input
If (fileTimestamp <= dateTimestamp)
{
; Prompt the user to confirm deletion
MsgBox, Are you sure you want to delete %A_LoopFileName%?
IfMsgBox, No
Continue

; Delete the file
FileDelete, %A_LoopFileName%
MsgBox, %A_LoopFileName% has been deleted.
}
}

MsgBox, The script has finished processing the files.

Explanation of the Script

1. InputBox for Directory: The script starts by prompting the user to enter the directory where the files to be deleted are located.

2. InputBox for Date Type: The user is then asked to select the date type for the deletion criteria—either creation date or modification date.

3. InputBox for Date: The user is prompted to enter the date in the format YYYY-MM-DD.

4. Date Conversion: The entered date is converted to a timestamp for comparison with the file's timestamp.

5. File Search and Deletion: The script loops through all files in the specified directory. For each file, it checks if the file's timestamp matches or is less than the user's input timestamp. If it does, the script prompts the user to confirm the deletion. If confirmed, the file is deleted.

6. Completion Message: After processing all files, a message is displayed to indicate that the script has finished.

Conclusion

This AutoHotkey script provides a simple yet effective way to batch delete files based on their creation or modification date. By automating this task, users can save time and ensure that old files are removed from their systems. The script is a good example of how AutoHotkey can be used for file management tasks, and it can be further customized to suit specific needs.