Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ok I want to make a batch file that takes a filename and replaces certain characters. EX: Replace all spaces with underscores or replace all A's with Q's

Ok, this is the wrong forum.... But, its a mistake that alot of people make. Its not really clear that Win2k does not use dos. So people get confused.
but.. here you go
First you need a seperate bat file to pass the filenames individually.
Something like...
@echo offfor /r c:\Batfiles\ %%K in (*.jpg) do call c:\rename.bat "%%K"
pause
exit
REM**************************************
Then you have the bat that renames them.
REM**************************************
@ECHO OFF
REM Set the File name without ext to Flenme variable
Set Flenme="%~n1"
REM Set Extn to extension only
Set Extn=%~x1
REM Find Special Characters and Replace them
REM Variable equals variable colon character to replace equals replace with percent
REM Replace Spaces with underscores
Set Flenme=%Flenme: =_%
REM Replace A's with Q's
Set Flenme=%Flenme:A=Q%REm ADD if statement to end it if the filename has not been changed
REM Should have an acceptable filename so Show it
ECHO %~nx1 Changed to %Flenme%%Extn%
REM ADD If Statement to Rename the file if there is not already a file with that name
REM Now Rename the file
REN "%~f1" %Flenme%%Extn%
Thats it,
you don't have to use a bat to call the other bat. but if you are doing other things, (like copying) this is a good way.
Let me know if it works for you.

by the way...
I put in to set the filename and the ext to seperate variables if you wanted to find a extra"." (periods) you could replace them too. Remove it if you want to rename file extensions as well.

mit,
"getting wiser in replying dos batch question"
DOS batch is fine in the DOS forum of course. But unfortunately this is not a DOS batch question. Instead, it's a *NT* batch question! For example, I don't think "for /r" will work in DOS.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |