Search code examples
batch-filefile-rename

The file is renamed with batch file but it won't stop


I need to rename all files in a folder by adding "BV" in front of the files name. My code is functioning but the problem appears when the total amount of file in that folder is more than ca. 200 files. It will rename all files to this: "BV_BV_BV..." and it won't stop.

Here is my code:

@echo off & setlocal

ECHO Moechten Sie das wirklich tun? (Bereits vorhandene Bildverzeichnisse werden geloescht 
ECHO und in den Dateinamen wird AWK2 hinzugefuegt) (j / n)
SET /p wahl=
if '%wahl%' == 'n' goto Nein
if '%wahl%' == 'j' goto Ja

:Ja
ECHO on

FOR %%I IN (*.bmp) DO ren "%%I" BV_"%%~nI".bmp

pause

I am not sure how I can make the batch file stop after all files have been renamed. Thanks for your help.


Solution

  • Assuming you want to rename all files once, with the prefix of BV_ as per your example in the for loop. Notice, I used choice instead of set /p

    @echo off & setlocal
    
    ECHO Moechten Sie das wirklich tun? (Bereits vorhandene Bildverzeichnisse werden geloescht 
    ECHO und in den Dateinamen wird AWK2 hinzugefuegt) (j / n)
    choice /c jn /m "wahl"
    goto opt%errorlevel%
    
    :opt1
    for /f "delims=" %%I IN ('dir /b /a-d *.bmp ^| findstr /v /b "BV_"') do ren "%%~I" "BV_%%~I"
    goto :eof
    :opt2
    echo do what ever you need to do here.