I have code that extracts N random files from each of many sub-folders. It works fine except for files with exclamation points. I use delayed expansion, which erases the exclamation points in the 2nd for loop. If I disabled expansion at the 2nd loop then i lose the ability to use variables within variables to capture the interim results (File1, File2, File3...). Help!
@echo off
chcp 1254
REM Display N random episodes from each sub-folder in g:\itunes\podcasts
setlocal EnableDelayedExpansion
set /p COUNT=Select Desired Number of Random Episodes per Album:
REM Recurse the podcast directory
for /d %%f in (G:\itunes\Podcasts\*) do (
set "buffer=%%f"
set n=0
REM http://stackoverflow.com/questions/18945521/need-to-create-a-batch-file-to-select-one-random-file-from-a-folder-and-copy-to
for %%g in ("!buffer!\*") do (
set /A n+=1
set "file!n!=%%g"
)
for /l %%i in (1, 1, !COUNT!) do (
set /A "rand=(!n!*!random!)/32768+1"
REM http://stackoverflow.com/questions/9700256/bat-file-variable-contents-as-part-of-another-variable
for %%A in ("!rand!") do echo !file%%~A!
)
)