简体   繁体   中英

Batch file renames one file twice

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM