chanchal1987
chanchal1987

Reputation: 2367

For loop in batch programming

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?

Upvotes: 1

Views: 2508

Answers (2)

clifgriffin
clifgriffin

Reputation: 2053

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%

Upvotes: 3

chris
chris

Reputation: 4026

you don't need a for loop:

rename File- "" File-*.txt

Upvotes: 0

Related Questions