My batch file is trying to access random numbers stored in an simple list array by assigning the random numbers in the array to a variable. The batch file will then loop through to the next number.
@echo off
setlocal enabledelayedexpansion
set /a count=0
:loop
set /a num=%random% %% 135 + 1
if "!numlist!" == "" (
set numlist=!num!
set /a count+=1
) else (
for %%i in (!numlist!) do (
if !num! == %%i (
goto loop
)
if !num! == %%i+1 (
goto loop
)
if !num! == %%i-1 (
goto loop
)
)
set numlist=!numlist! !num!
set /a count+=1
)
if !count! lss 135 (
goto loop
)
echo !numlist!
set /a i=0
for %%n in (!numlist!) do (
set arr[!i!]=%%n
set /a i+=1
)
rem Display the array elements
for /l %%i in (0,1,135) do (
echo !arr[%%i]!
)
rem Set the path to the folder containing the files to be renamed
set folderPath="D:\Path\To\Files"
rem Rename up to 135 files in the specified folder using the random numbers from the array
set /a g=0
for %%f in ("%folderPath%\*.*") do (
if !g! geq 135 goto endloop
rem echo var g is !g!
set "num=!arr[!g!]!"
rem echo var num is !num!
ren "%%f" "!num! - %%~nxf"
set /a g+=1
set /a i+=1
)
:endloop
endlocal
As only an occasional coder I took a long time to write this script. It is designed to add a random number to the filename of a folder containing 135 files (this will be modified to add a filenum variable for use on any folder). The numbers will not be duplicated or consecutive.
It works as expected until the set "num=!arr[!g!]!" line. The value of variable "num" is not set to the random number from the array but is set to "g" in every iteration of the loop giving the output "g - [file_name]. extension" and not "[random_number] - [file_name].extension".
I have spent hours googling and experimenting but I can't find a solution. I have a feeling it may be something obvious to a more experienced coder. I would be grateful to be pointed in the right direction.