Batch Script to Rename and Organize Files: A Step-by-Step Guide

Managing files efficiently is crucial for productivity, especially when dealing with large numbers of files. If you’ve ever needed to rename files in bulk (e.g., removing a common prefix like sigma-) and organize them into a specific folder, a batch script can automate the process.

In this guide, I’ll walk you through a customizable batch script that: ✔ Renames files by removing a specified string ✔ Moves or copies them to a target folder ✔ Works for any file extension (e.g., .png, .jpg, .txt)

Why Use a Batch Script?

  • Saves time – No need to manually rename and move files one by one.
  • Reduces errors – Automated scripts eliminate human mistakes.
  • Flexible – Works for any file type and naming pattern.

The Batch Script

Here’s the complete script with user prompts for folder name, string to remove, file extension, and copy/move choice:

@echo off
setlocal enabledelayedexpansion

:: Prompt user for the folder name
set /p foldername="Enter the folder name to save files to: "
if not exist "%foldername%" mkdir "%foldername%"

:: Prompt user for the string to remove from filenames
set /p removestring="Enter the string to remove from filenames (e.g., sigma-): "

:: Prompt user for the file extension
set /p extension="Enter the file extension (e.g., png): "

:: Prompt user to choose between copy or move
set /p action="Do you want to COPY or MOVE the files? (copy/move): "
if /i "%action%" neq "copy" if /i "%action%" neq "move" (
    echo Invalid choice. Please enter either "copy" or "move".
    pause
    exit /b
)

:: Loop through all files with the specified extension and rename/copy or move them
for %%F in (*%removestring%*.%extension%) do (
    set "filename=%%~nxF"
    set "newname=!filename:%removestring%=!"
    if /i "%action%"=="copy" (
        copy "%%F" "%foldername%\!newname!" >nul
    ) else (
        move "%%F" "%foldername%\!newname!" >nul
    )
)

echo Files %action%ed and renamed successfully.
pause

How to Use the Script

Step 1: Create the Batch File

  1. Open Notepad.
  2. Copy and paste the script above.
  3. Save the file as rename_and_organize.bat (ensure “All Files” is selected in the “Save as type” dropdown).

Step 2: Run the Script

  1. Place the .bat file in the folder containing your files.
  2. Double-click rename_and_organize.bat.
  3. Follow the prompts:
    • Enter the target folder name (e.g., icons).
    • Enter the string to remove (e.g., sigma-).
    • Enter the file extension (e.g., png).
    • Choose whether to copy or move the files.

Step 3: Verify the Results

  • Check the target folder to confirm files were renamed and moved/copied correctly.

Example Use Case

Suppose you have files named:

  • sigma-icon1.png
  • sigma-icon2.png

Running the script with:

  • Folder name: icons
  • String to remove: sigma-
  • Extension: png
  • Action: move

Result:

  • Files are renamed to icon1.png and icon2.png and moved to the icons folder.

Customization Tips

  • Change the file extension to .jpg, .txt, etc., as needed.
  • Modify the script to handle subfolders by adding for /r instead of for.
  • Add logging to track renamed files by appending >> log.txt to the copy/move commands.

Conclusion

This batch script is a powerful tool for bulk renaming and organizing files. Whether you’re a developer, designer, or just someone who wants to keep files tidy, automating repetitive tasks saves time and reduces frustration.

Try it out!

Download the script, tweak it for your needs, and enjoy a more organized workspace.


Have questions or improvements? Share your thoughts in the comments! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *