AutoHotkey 语言 一键批量重命名指定格式文件实战

AutoHotkey阿木 发布于 3 天前 3 次阅读


AutoHotkey Language: A Practical Guide to Batch Rename Files with a Single Keystroke

Introduction

AutoHotkey (AHK) is a powerful scripting language for automating Windows tasks. It allows users to create scripts that can automate repetitive tasks, such as renaming files, without the need for complex programming knowledge. In this article, we will delve into the creation of a practical AutoHotkey script that enables users to rename files with a single keystroke. This guide will cover the basics of AutoHotkey, the creation of the script, and how to use it to rename files in a specified format.

Understanding AutoHotkey

Before we dive into the script, let's briefly go over the basics of AutoHotkey. AHK scripts are written in a simple, easy-to-read syntax and can be executed by the AutoHotkey executable. The language supports variables, loops, conditions, and functions, making it suitable for a wide range of automation tasks.

The Script: Batch Rename with a Single Keystroke

Step 1: Setting Up the Environment

First, ensure that you have AutoHotkey installed on your system. You can download it from the official website (https://www.autohotkey.com/). Once installed, you can create a new script file by right-clicking on the desktop, selecting New, and then choosing AutoHotkey Script.

Step 2: Writing the Script

Open the script file in a text editor and paste the following code:

ahk
Persistent
SingleInstance, Force

SetTimer, RenameFiles, 1000

$^r::
Loop, Files, %A_ScriptDir%.txt
{
SplitPath, A_LoopFileName, , , , NameNoExt
NewName := "New_" NameNoExt ".txt"
FileMove, %A_LoopFileName%, %A_ScriptDir%%NewName%, 1
}
MsgBox, Renamed all .txt files in the script directory.
Return

RenameFiles:
MsgBox, Press Ctrl+R to rename files.
Return

Explanation of the Script

- `Persistent` ensures that the script runs indefinitely until it is manually stopped.
- `SingleInstance, Force` ensures that only one instance of the script runs at a time.
- `SetTimer, RenameFiles, 1000` sets up a timer that updates the message box every second.
- `$^r::` is the hotkey that triggers the renaming action when Ctrl+R is pressed.
- `Loop, Files, %A_ScriptDir%.txt` loops through all `.txt` files in the script's directory.
- `SplitPath, A_LoopFileName, , , , NameNoExt` splits the file name from its extension and stores it in `NameNoExt`.
- `NewName := "New_" NameNoExt ".txt"` creates a new file name by prefixing "New_" to the original file name.
- `FileMove, %A_LoopFileName%, %A_ScriptDir%%NewName%, 1` moves the file to the new name.
- `MsgBox, Renamed all .txt files in the script directory.` displays a message box to confirm the action.
- `RenameFiles:` is a label that updates the message box with instructions on how to use the script.

Step 3: Running the Script

Save the script file with a `.ahk` extension and double-click it to run. The script will start running in the background. To rename the files, press Ctrl+R. A message box will appear every second, reminding you to press Ctrl+R when you're ready to rename the files.

Advanced Features

The script provided above is a basic example. Here are some advanced features you can add to enhance the script:

- Customize the File Extension: Modify the `.txt` part of the script to target files with different extensions.
- Customize the Renaming Pattern: Change the `NewName` variable to include different naming patterns.
- Add Error Handling: Implement error handling to manage situations where files cannot be renamed.
- Use Functions: Break down the script into functions for better organization and reusability.

Conclusion

This article has provided a practical guide to creating an AutoHotkey script that allows users to rename files with a single keystroke. By following the steps outlined above, you can automate the renaming of files in a specified format, saving time and effort. With the basic understanding of AutoHotkey and the script provided, you can further customize and enhance the script to suit your specific needs. Happy scripting!