Skip to main content

How to Rename a Directory in Batch Script

Renaming a directory is a common file management task, often needed to update a project's name, archive a folder by adding a date stamp, or simply to correct a typo. The standard, built-in command for this in Windows is REN, short for RENAME. This command is simple, fast, and effective for this purpose.

This guide will teach you how to use the REN command to rename directories, how its behavior differs from the MOVE command, and the best practices for handling paths and potential errors in your scripts.

The Core Command: REN (RENAME)

The REN command (and its identical alias, RENAME) is the primary utility for renaming both files and directories. The syntax is straightforward and intuitive.

Syntax:REN "path\to\OldFolderName" "NewFolderName"

  • The first argument is the full path to the directory you want to rename.
  • The second argument is only the new name for the directory, not a full path. The directory will be renamed in its current location.

Basic Example: Renaming a Folder

Let's rename a folder from ProjectA to Project_Phoenix.

Consider the following intial state of folders:

C:\PROJECTS\
| \---ProjectA\
| ...

And we run the following script:

@ECHO OFF
ECHO Renaming "ProjectA" to "Project_Phoenix"...
REN "C:\Projects\ProjectA" "Project_Phoenix"

IF EXIST "C:\Projects\Project_Phoenix\" (
ECHO [SUCCESS] Directory renamed.
) ELSE (
ECHO [FAILURE] An error occurred.
)

The folder is renamed in the same location, as shown by the output:

C:\PROJECTS\
| \---Project_Phoenix\
| ...

The REN Command vs. the MOVE Command

Both REN and MOVE can be used to rename a directory, but they have a critical difference:

  • REN can only rename a directory in its current location. You cannot use it to move a directory to a different path.
    • REN "C:\FolderA" "FolderB" -> Works.
    • REN "C:\FolderA" "D:\FolderB" -> Fails.
  • MOVE can both rename and move a directory. If the destination path is on the same drive and doesn't exist, it acts as a rename. It can also move the folder to an entirely different drive.
    • MOVE "C:\FolderA" "C:\FolderB" -> Works (renames FolderA to FolderB).
    • MOVE "C:\FolderA" "D:\FolderB" -> Works (moves and renames).
note

Rule of Thumb: If your only goal is to rename a folder in place, REN is the most direct and clearest command for the job. If you need to move the folder as well, use MOVE.

Common Pitfalls and How to Solve Them

Problem: The Path Contains Spaces

If the path to the directory or the new name contains spaces, the command will fail unless it is properly quoted.

Let's see the error in action:

REM This will FAIL.
REN C:\My Projects\Old Project "New Project Name"

Solution: Always Quote Your Paths

This is a universal best practice in batch scripting. Enclose both arguments in double quotes.

REM This is the correct, safe syntax.
REN "C:\My Projects\Old Project" "New Project Name"

Problem: A Folder with the New Name Already Exists

If you try to rename a folder to a name that is already taken by another folder or file in the same location, the command will fail.

Let's see the error in action:

C:\> REN "ProjectA" "ProjectB"
A duplicate file name exists, or the file
cannot be found.

Solution: Check Before Renaming

A robust script should first check if the target name is already in use before attempting to rename.

@ECHO OFF
SET "OLD_NAME=C:\Projects\ProjectA"
SET "NEW_NAME=ProjectB"

IF EXIST "C:\Projects\%NEW_NAME%\" (
ECHO [ERROR] A directory named "%NEW_NAME%" already exists. Cannot rename.
) ELSE (
REN "%OLD_NAME%" "%NEW_NAME%"
ECHO [SUCCESS] Renamed directory.
)

Problem: The Directory is in Use

If the directory you are trying to rename (or any file within it) is currently open in another program (like File Explorer or a text editor), the REN command will fail.

The process cannot access the file because it is being used by another process.

Solution: Ensure the Directory is Not in Use

There is no command-line way to force a rename on a locked directory. The locking program must be closed first. Ensure your script is not operating from within the directory you are trying to rename, and close any applications that might have a handle on it.

Practical Example: Appending a Date Stamp to a Folder Name

This is a common use case for archiving. The script renames a Logs folder to include the current date, for example, Logs_2023-10-27.

@ECHO OFF
SETLOCAL
SET "TARGET_FOLDER=C:\Application\Logs"

ECHO --- Log Archival Script ---
ECHO.

IF NOT EXIST "%TARGET_FOLDER%\" (
ECHO [INFO] Logs folder not found. Nothing to do.
GOTO :End
)

REM --- Create a date stamp like YYYY-MM-DD ---
SET "DATE_STAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
SET "NEW_NAME=Logs_%DATE_STAMP%"

ECHO Renaming "%TARGET_FOLDER%" to "%NEW_NAME%"...

IF EXIST "C:\Application\%NEW_NAME%\" (
ECHO [ERROR] An archived log folder for today already exists.
GOTO :End
)

REN "%TARGET_FOLDER%" "%NEW_NAME%"
ECHO [SUCCESS] Folder has been renamed.

:End
ENDLOCAL

Conclusion

The REN (or RENAME) command is the dedicated, simple, and effective tool for renaming directories in place.

For reliable scripting:

  • Use the syntax REN "OldPath\OldName" "NewName".
  • Always enclose both arguments in double quotes to handle spaces and special characters.
  • For robust logic, check if the target name already exists (IF EXIST "NewName\") before attempting the rename.
  • Ensure the directory is not in use by another process.

For the simple task of renaming a folder in its current location, REN is the clearest and most direct command for the job.