0

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.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Taha Sami
  • 1,565
  • 1
  • 16
  • 43

2 Answers2

1

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

@echo off
setlocal enabledelayedexpansion
set "res=" & set "rnd="
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 (
    set "name=%%~ni"
    if "!name:~19,1!" == "" call :digits "%%i"
)
Popd
goto :EOF
:digits
echo "%~n1" | findstr /VRC:"[a-Z]" && goto :EOF
set "res=" & set "rnd="
for /l %%i in (1,1,20) do (
    set /a rnd=!random! %% 10
    set "res=!res!!rnd!"
)

if exist "%~dp1!res!%~x1" goto :digits
echo ren "%~1" "!res!%~x1"
goto :EOF

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 /?
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

Here is another solution for renaming PNG files in an entire directory tree with a random 20 digit number:

@echo off & setlocal EnableExtensions DisableDelayedExpansion
(for /F "delims=" %%I in ('dir "%~dp0*.png" /A-D-L /B /S 2^>nul') do set "FullName=%%I" & set "FilePath=%%~dpI" & call :RenameFile %%~xI) & exit /B
:RenameFile
setlocal EnableDelayedExpansion
:CreateName
set "NewName=" & for /L %%J in (1,1,5) do set "Number=000!RANDOM!" & set "NewName=!NewName!!Number:~-4!"
if not exist "!FilePath!!NewName!%1" (ren "!FullName!" "!NewName!%1" & endlocal & goto :EOF) else goto CreateName

This solution renames all PNG files in the directory of the batch file and its subdirectories even on a subdirectory or a PNG file having an ugly name like Development & Test(!) 100% with an ampersand, an exclamation mark, round brackets and a percent sign. %~dp0 in second command line referencing the batch file path with a backslash at end can be removed to rename the files in the current working directory and its subdirectories or replaced by any other directory path ending with a backslash.

The batch file code is written to avoid batch file open/seek/read/close operations as much as possible by using single lines with multiple commands to reduce the file system and batch file accesses to (nearly) a minimum. (It would be possible to optimize the code further, but that would make reading the command lines really difficult.)

There is never renamed a PNG file more than once and there is never skipped a PNG file even on running this batch file on a FAT32 or exFAT formatted drive because of getting first loaded into the memory of batch file processing cmd.exe all PNG file names with their full paths before starting the renaming FOR loop.

The dynamic variable RANDOM holds a random decimal number between 0 and 32767. The solution uses just the last four digits which means a decimal number in range 0000 to 9999 and concatenates five such random numbers together to a random number with 20 digits.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • ren /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143