Home > OS >  How to individually rename the files in each separate folder in a batch file
How to individually rename the files in each separate folder in a batch file

Time:09-13

with the batch file below, I numerically rename all the .mp4 files in the folders, incremented by 1, but it doesn't rename them as I would like.

@Set "count=0"
@For /F "Delims=" %%I In ('Dir /B/S/A-D-S "*.mp4" 2^>NUL')Do @(
    For %%J In ("%%~pI.")Do @(Set /A count =1
        SetLocal EnableDelayedExpansion
        Ren "%%I" "!count!%%~xI"
        EndLocal))
@Pause

Now the rename is like this:

  1. Folder1 (60 files in it)

    • 1.mp4, 2.mp4....60.mp4
  2. Folder2 (60 files in it)

    • 61.mp4, 62.mp4....120.mp4
  3. Folder3 (30 files in it)

    • 121.mp4, 122.mp4....160.mp4

But I would like to be

  1. Folder1 (60 files in it)

    • 1.mp4, 2.mp4....60.mp4
  2. Folder2 (60 files in it)

    • 1.mp4, 2.mp4....60.mp4
  3. Folder2 (30 files in it)

    • 1.mp4, 2.mp4....30.mp4
  4. Folder2 (45 files in it)

    • 1.mp4, 2.mp4....45.mp4

Each sub-folder should start renaming from 1 and end at how many files are in the folder.

Can someone help me in this direction?

CodePudding user response:

I would assume that the following modification would suit your needs:

@For /F "Delims=" %%G In ('Dir /A:D-S /B /S 2^>NUL') Do @(
    Set "Count=0"
    For /F "Delims=" %%H In (
        'Set "PathExt=" ^& %SystemRoot%\System32\where.exe "%%G":"*.mp4" 2^>NUL'
    ) Do @(
        Set /A Count  = 1
        SetLocal EnableDelayedExpansion
        Ren "%%H" "!Count!%%~xH"
        EndLocal
    )
)
@Pause
  • Related