Hello everyone,

Is been a while since i've been trying to rename a file
using the ren batch command, but for some reasons is not
working for me. could some one please give me an idea on
how to reanme a file with a batch file?
Thanks in advance.

This is my example:

@ECHO OFF
REN C:\Testing.txt Testing-Renamed.txt

Dani AI

Generated

Quick summary for future readers: ’s REN line is a perfectly valid approach but commonly fails for a few predictable reasons — wrong path/extension, spaces not quoted, running the .bat by double‑click so the window closes before you see an error, or trying to use REN to move a file to a different folder/drive (REN only changes the name in the same directory). (learn.microsoft.com)

Troubleshooting checklist (fast): run the batch from an already‑open Command Prompt so you can read any error text; add a pause at the end while debugging so the window doesn’t vanish; verify the file actually exists and you have permission to edit it; and always quote paths that contain spaces. These steps catch most problems quickly. (learn.microsoft.com)

If you need to rename or move reliably from a batch file, MOVE is simpler because it accepts full source and target paths (it will also rename if the destination filename is provided). Example pattern to keep behavior explicit and show errors:

if exist "C:\My Files\old name.txt" (
  move /Y "C:\My Files\old name.txt" "C:\My Files\new name.txt"
) else (
  echo File not found: "C:\My Files\old name.txt"
)
pause

Use MOVE when you must change folders or drives. (learn.microsoft.com)

If you prefer PowerShell (as mentioned), Rename-Item is the cmdlet to use for pure renames and Move-Item for move+rename. Rename-Item expects a new name (not a different path), so use Move-Item if the destination includes a path. Example:

Rename-Item -Path "C:\My Files\old name.txt" -NewName "new name.txt"

PowerShell gives nicer error messages and easier bulk/templated renames. (learn.microsoft.com)

If none of this helps, running the commands interactively from cmd/powershell will show the exact error message to fix (file not found, access denied, file in use, etc.).

Recommended Answers

All 3 Replies

>> but for some reasons is not working for me

Unable to guess the reasons. Is there ANY output produced when you run this script?

I'm not a batch file expert, but do you REALLY need to use batch file only? If not I highly recommend using something like Cygwin and do the same using a shell script instead.

I also understand that in newer Windows there is something called PowerShell, which is better programmable.

Hi, actually no, i don't get any msg, or anything poiting a reason or error.
All i need is to be able to change the name of any file from a batch file.
by the way, am i posting this thread in the wrong topic? i am new in this site.
another question, does daniweb deals with batch programming?

I solved the problem myself :-) i'm glad I did it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.