jeudi 22 août 2019

Batch file set to pick a random file from preset categories is not picking a random file (keeps picking the last file!)

Good evening. I enjoy modding a game in my free time, it's one those games where individual mods are packed into zip files and loaded in by simply placing said zips into a certain folder. I have hundreds of these zips and was hoping to make a batch that would pick out a random mod for me each time I use it instead of having to pick through them all myself, while placing all the unused ones into a disabled folder that I made to keep everything not being used all in one place. Every mod was prefixed with a name referring to what category it'd belong to, for example 'weapon_shotgun_GiantAngryGun9000.zip' or 'character_huck_Clown.zip'

How this batch script would work is that first it'd take every file with the specified prefixes currently in the mod folder and put them all into the disabled folder. It would then go through every single file in the disabled folder that has all the specified prefixes, pick a random file from each category, and move them into the mod folder. Currently I've gotten my batch to do all that is explained, except rather than pick a random of each, it keeps picking the alphabetically last one from each prefix list. I believe it's because the Num var being used to designate the index for the entries in the File array is not being modified or read, and in-spite my best efforts I cannot get it to work the way I'm hoping.

Any advice or solutions is appreciated, thank you for reading.

setlocal EnableDelayedExpansion

SET modFolder="C:\Game\Mods\"
SET disabledFolder="C:\Game\Mods\Disabled\"
SET itemListIndex=0

::Prefix names
SET Prefix1=Weapon_Shotgun_
set itemList[%itemListIndex%]=%Prefix1%
set /A itemListIndex+=1

SET Prefix2=Character_ThisPerson_
set itemList[%itemListIndex%]=%Prefix2%
set /A itemListIndex+=1

SET Prefix3=Weapon_Rifle_
set itemList[%itemListIndex%]=%Prefix3%
set /A itemListIndex+=1


for /l %%G in (0 1 %itemListIndex%) do (
    if not "!itemList[%%G]!" == "" (
        set num=0
        for %%f in (!itemList[%%G]!*.zip) do (
           set /A num+=1
           set file[%num%]=%%f
        )
        set /A pick = !random! * %num% / 32768 + 1
        echo Moving "!file[%pick%]!" to the mod folder
        move "%disabledFolder%!file[%pick%]!" %modFolder%
    )
)

set file[%num%]=%%f is echoed as set file[]='[name of file]' instead of the expected set file['(index number)']='[name of file]'

the written echo says Moving !file[]! to the mod folder instead of the expected Moving (name of file) to the mod folder

The pick variable echos as blank, implying num or random are not valid




Aucun commentaire:

Enregistrer un commentaire