Search code examples
batch-rename

Batch script to rename files in subfolders


I need a script which rename all files in all sub folders of script's root. I did search and found something which works and I can modify (I am a little bit newbie/laic)

@echo off
chcp 65001
setlocal enabledelayedexpansion
set filename=image
set /a x=1
>@rename.txt (
for /r %CD% %%f in (*.jpg) do (
        echo rename "%%f" "!filename!_!x!.jpg"
        rename "%%f" "!filename!_!x!.jpg" 
    set /a x+=1
    )
)
endlocal
pause

But I want this to rename files to random string.

I found a lots of scripts which can generate random strings, but I just can't make them to work inside the FOR brackets.

for now im stuck with

@echo off
chcp 65001
setlocal enabledelayedexpansion
set /a x=%RANDOM%/99
>@rename.txt (
for /r %CD% %%f in (*.jpg) do (
        echo "%%f" renamed to "!x!.jpg"
        rename "%%f" "!x!.jpg"
    set /a x+=%RANDOM%/99
    )
)
endlocal

Which works fine, but has his limitations.

Any help will be appreciated


Solution

  • @echo off
    chcp 65001
    setlocal enableextensions enabledelayedexpansion
    set "alphabet=a b c d f g h i j k l m n p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"
        set "size=0"
        for %%a in (%alphabet%) do (
            set "a.!size!=%%a"
            set /a "size+=1"
        )
    for /r %CD% %%f in (*.jpg) do (
        set "k="
        for /l %%a in (1 1 64) do (
            set /a "r=!random! %% size"
            for %%b in (!r!) do set "k=!k!!a.%%b!"
        )
        rename "%%f" "!k!.jpg"
       )
    endlocal