AutoHotkey 语言 批量重命名音乐专辑文件示例

AutoHotkey阿木 发布于 6 天前 5 次阅读


AutoHotkey Language: Batch Renaming Music Album Files Example

Introduction

AutoHotkey (AHK) is a powerful scripting language for automating tasks on Windows. 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 explore how to create an AutoHotkey script to batch rename music album files. This script will be useful for organizing your music collection by automatically renaming files based on metadata such as artist name, album title, and track number.

Prerequisites

Before we dive into the code, ensure you have the following prerequisites:

1. AutoHotkey installed on your Windows system.
2. Access to the music album files you want to rename.
3. Optionally, a music player or library management software that can provide metadata for the files.

Script Overview

The script will perform the following steps:

1. Prompt the user to select the directory containing the music album files.
2. Read the metadata from each file using the built-in AHK functions.
3. Generate a new file name based on the metadata.
4. Rename the file using the new name.
5. Repeat the process for all files in the selected directory.

Sample Code

Below is a sample AutoHotkey script that demonstrates how to batch rename music album files:

ahk
; Prompt the user to select the directory containing the music album files
InputBox, directoryPath, Select Directory, Please select the directory containing the music album files:, , 400, 200
IfError, directoryPath =
MsgBox, No directory selected. Exiting script.
ExitApp

; Loop through all files in the selected directory
Loop, Files, %directoryPath%., 2
{
; Extract file metadata
file := A_LoopFileLongPath
artist := ""
album := ""
track := ""

; Attempt to read metadata from the file
If (FileGetVersionInfo(file, versionInfo))
{
artist := versionInfo.company
album := versionInfo.product
track := versionInfo.fileDescription
}

; If metadata is not available, prompt the user to enter it manually
If (artist = "" || album = "" || track = "")
{
InputBox, artist, Artist, Please enter the artist name:, , 400, 200
InputBox, album, Album, Please enter the album title:, , 400, 200
InputBox, track, Track, Please enter the track number:, , 400, 200
}

; Generate new file name
newFileName := artist . " - " . album . " - " . track . ".mp3"

; Rename the file
If (FileRenameDir(directoryPath, newFileName))
{
MsgBox, File renamed successfully: %newFileName%
}
Else
{
MsgBox, Failed to rename file: %file%
}
}

Explanation of the Code

1. InputBox: The script starts by prompting the user to select the directory containing the music album files. If the user cancels the selection, the script exits.

2. Loop: The script then loops through all files in the selected directory using the `Files` command.

3. FileGetVersionInfo: The script attempts to read metadata from each file using the `FileGetVersionInfo` function. This function retrieves information about the file, such as the company, product, and file description, which can be used as the artist, album, and track name, respectively.

4. InputBox: If the metadata is not available or incomplete, the script prompts the user to enter the missing information manually.

5. NewFileName: The script generates a new file name based on the artist, album, and track information, appending the file extension (e.g., ".mp3").

6. FileRenameDir: The script attempts to rename the file using the `FileRenameDir` function. If the rename is successful, a message box confirms the action; otherwise, an error message is displayed.

Conclusion

This AutoHotkey script provides a simple and effective way to batch rename music album files based on metadata. By automating this process, you can save time and ensure consistency in your music collection's file names. Remember to customize the script to fit your specific needs, such as changing the file extension or adding additional metadata fields. Happy scripting!