Friday, May 4, 2018

batch - Random numbered files using win7 in bat file




I have a mp3 player that has a poorly designed hard coded random generator for shuffling songs. Every time I turn it on, it plays the same sequence (5-22-3-150-75-86... etc) In order to hear a larger variety I want to be able to periodically randomize the file names on the usb stick and alter the files position in the directory listing.



I am trying to rename all file names in a sub-directory to a random number. This is what I have been trying but it does not work. For some reason %num% is blank in the new file name.



1|    for %%A in (*.mp3) DO (
2| set /A num=%RANDOM%*100/32768+1
3| ren "%%A" %num%.mp3
4| )
5| pause



(Line numbers added for clarity during this discussion)

The output for each file is shown below.




D:\music_playlists\pop-mix [25gb]>(
set /A num=13621*100/32768+1
ren "Country-132.mp3" .mp3
)
A duplicate file name exists, or the file
cannot be found.




As you can see the %NUM% is not being expanded to a file name on line #3...




What do I need to do?



update - not sure exactly what was going on but this seems to work even though the echo shows a blank field



SETLOCAL EnableDelayedExpansion

FOR %%A IN (*.mp3) DO (
SET /A NUM=!RANDOM!
echo !NUM!
REN "%%A" !NUM!.mp3

)

PAUSE

Answer



Try enabling delayed expansion and changing the % signs used with RANDOM and NUM to ! points:



SETLOCAL EnableDelayedExpansion

FOR %%A IN (*.mp3) DO (

SET /A NUM=!RANDOM!*100/32768+1
REN "%%A" !NUM!.mp3
)

PAUSE

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...