Home > Enterprise >  What is the best way to rename a huge number of images from xxx to random 20 digits in Windows 10?
What is the best way to rename a huge number of images from xxx to random 20 digits in Windows 10?

Time:11-10

I have 80 folders, and inside every folder, there are between 1 to 30 images.

I'm looking for a way to rename every image to random 20 digits. Here is an example:

Folder_1
|
--- J0tNNchs7U.png -> 59106128950679048106.png
--- nB9HodYxov.png -> 95787130775876521419.png
--- 4yZswgC7xh.png -> 86675183902449304154.png
--- Ax9xwx1e4L.png -> 00276988431900233660.png

Folder_2
|
--- a1yoCwGeUE.png -> 82032328129568492832.png
--- xwItDSLNg4.png -> 98505854158768600999.png
--- 5beJ52yhD1.png -> 90915835999997422646.png

Folder_3
|
--- oSWqLBsymz.png -> 42132595053848488418.png
--- AgoS8guAxi.png -> 76836254163466666967.png

Folder_4
|
--- 5xLO5IXwRd.png -> 39762534969789244484.png

Etc...

It may take 2 to 4 hours if I want to do it manually, is it possible to do that in Windows 10 using cmd?

Thank you.

CodePudding user response:

It is very possible. Here is a way to generate a 20 digit random number:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,20) do (
    set /a rnd=!random! %% 10
    set res=!res!!rnd!
)
echo !res!

Which can then be incorporated with file renaming:

@echo off
pushd "C:\path to png files"

setlocal enabledelayedexpansion
for /R %%i in (*.png) do call :digits "%%i"

goto :EOF
:digits
set res= & set rnd=
for /l %%i in (1,1,20) do (
    set /a rnd=!random! %% 10
    set "res=!res!!rnd!"
)
echo ren "%~1" "!res!%~x1"
popd

Note the echo on the last line is for QA purposes, only remove echo once the printed results look correct. Always test it in a test folder first before running it in production.

Explanation: I run a for loop to get each png file, then call the :digits label with the file name. The for loop in the :digits label simply generates one random number per cycle with a maximum digit of 1, hence %% 9. It will then just append each digit next to the other in the set res=!res!!rnd! line until all 20 digits are appended to become a single variable, then simply rename the file to the variable created.

You can build in some checks to ensure that you do not have existing files with the random numbers already and also to only rename files which is not already a 20 digit numbers as well, but the question was more around how to create a random 20 digit number.

For some more help on the commands used, open cmd and run:

for /?
set /?
setlocal /?
  • Related