Windows batch file pattern rename

Learn windows batch file pattern rename with practical examples, diagrams, and best practices. Covers windows, batch-file, rename development techniques with visual explanations.

Mastering Pattern Renaming with Windows Batch Files

Mastering Pattern Renaming with Windows Batch Files

Learn how to efficiently rename multiple files using powerful pattern matching techniques in Windows batch scripts, covering basic to advanced scenarios.

Renaming large numbers of files can be a tedious task, especially when dealing with specific patterns. Windows batch files provide a robust scripting environment to automate such operations. This article will guide you through creating effective batch scripts for pattern-based file renaming, from simple prefix/suffix changes to more complex regular expression-like matching.

Understanding Basic Renaming Operations

Before diving into patterns, let's review the fundamental REN (or RENAME) command in Windows. This command allows you to change the name of a single file or a group of files using wildcards. The basic syntax is REN [drive:][path]filename1 filename2.

REN old_name.txt new_name.txt

Renaming a single file.

REN *.txt *.bak

Changing the extension of all .txt files to .bak.

Pattern Renaming with FOR Loops and Substring Manipulation

For more intricate renaming tasks, especially when you need to extract parts of a filename or insert new characters, the FOR loop combined with variable substring manipulation is indispensable. This allows you to iterate through files and construct new filenames dynamically.

A flowchart showing the pattern renaming process with a FOR loop. Steps include: Start, List files, FOR loop (for each file), Extract original filename, Apply pattern logic (e.g., substring, replace), Construct new filename, Preview rename command, Execute rename command, End. Use blue rounded rectangles for start/end, green rectangles for processes, orange diamond for FOR loop, and arrows indicating flow.

Flowchart of pattern renaming using a FOR loop.

@ECHO OFF
SETLOCAL
FOR %%f IN (*.jpg) DO (
    ECHO REN "%%f" "prefix_%%f"
    REM REN "%%f" "prefix_%%f"
)
ENDLOCAL

This script adds 'prefix_' to all JPG files. The ECHO helps preview the command.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%f IN (*.txt) DO (
    SET "oldname=%%f"
    SET "newname=!oldname:old_part=new_part!"
    IF NOT "!oldname!"=="!newname!" (
        ECHO REN "!oldname!" "!newname!"
        REM REN "!oldname!" "!newname!"
    )
)
ENDLOCAL

Replaces 'old_part' with 'new_part' in all TXT files. EnableDelayedExpansion is crucial here.

Advanced Renaming with Date/Time Stamps

Often, you might want to include the current date or time in a filename for versioning or archival purposes. Windows batch files can access system date and time variables, which can then be formatted and integrated into your filenames.

@ECHO OFF
SETLOCAL
FOR /F "tokens=1-4 delims=/ " %%i IN ('DATE /T') DO (
    SET "current_date=%%k-%%j-%%i"
)

FOR %%f IN (*.log) DO (
    ECHO REN "%%f" "%%~nf_!current_date!%%~xf"
    REM REN "%%f" "%%~nf_!current_date!%%~xf"
)
ENDLOCAL

Appends the current date (YYYY-MM-DD) to LOG files. Note the date format parsing might vary by locale.

1. Step 1

Open a text editor (like Notepad) and paste your batch script code.

2. Step 2

Save the file with a .bat extension (e.g., rename_script.bat).

3. Step 3

Place the .bat file in the directory containing the files you want to rename.

4. Step 4

Double-click the .bat file to execute it. Review the ECHO output first, then uncomment the REN commands to perform the actual renaming.