0

I tried to change names *.txt files, but i have a problem I can't solve. I would like to give prefixes to *.txt files entered by user input, but the code I have adds, without knowing why, a doubled prefix for one file.

@echo off
SET /p Input=Enter prefix wanted: 
Echo You entered: "%Input%"
Pause
for %%a in (*.txt) do ren "%%a" "%Input%%%a"

before using the code: 1.txt 2.txt 3.txt

after using the code with prefix added by user: test1.txt test2.txt testest3.txt

Do you know the solution to my problem?

kacperk
  • 11
  • 3

1 Answers1

1

Use

for /f "delims=" %%a in ('dir /b *.txt') do ...

The problem is that when the file is renamed, the for looks for the next file in the directory - which could be the new name of the file.

The code I've provided builds a file list in memory, then processes the list, so it doesn't see the new name of the file.

Magoo
  • 77,302
  • 8
  • 62
  • 84