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 /?