1

I have a set of files in a directory like below:

File-MyFile.txt
File-AnotherFile.txt
File-ThirdFile.txt

I want to rename all files like below:

MyFile.txt
AnotherFile.txt
ThirdFile.txt

How can I use a for loop to do that?

2 Answers 2

3

Chris shows the best way to do it. I think this might come close to what you asked for, though I didn't test it:

@echo off
for /f %%a IN ('dir /b *.txt') do call :dorename %%a
goto :eof

:dorename
set oldfile=%1
set newfile=%oldfile:File-=%
rename %oldfile% %newfile%
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, still not perfect but it does what you want it to do. YMMV.
newfile=%oldfile:File-=% can also be newfile=%oldfile:~5%
Just for completeness (this version is a bit more readable), you can use *.txt instead of dir /b *.txt and %%~nxa instead of %%a.
0

you don't need a for loop:

rename File- "" File-*.txt

2 Comments

Not working for me. Showing "The syntax of the command is incorrect."
Sorry, must have overlooked the dos-batch tag. This is for unix rename.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.