Batch file 延迟扩张让我陷入了困境

Batch file 延迟扩张让我陷入了困境,batch-file,Batch File,我有一段代码,可以从每个子文件夹中提取N个随机文件。除了带有感叹号的文件外,它工作正常。我使用延迟扩展,它删除第二个for循环中的感叹号。如果我在第二个循环中禁用了扩展,那么我就无法在变量中使用变量来捕获临时结果(File1、File2、File3…)。救命啊 我有正确的答案。我修改了该解决方案以允许子文件夹。以下代码从指定文件夹的每个子文件夹返回N个随机文件 @echo off REM Display N random episodes for each podcast folder (~

我有一段代码,可以从每个子文件夹中提取N个随机文件。除了带有感叹号的文件外,它工作正常。我使用延迟扩展,它删除第二个for循环中的感叹号。如果我在第二个循环中禁用了扩展,那么我就无法在变量中使用变量来捕获临时结果(File1、File2、File3…)。救命啊

我有正确的答案。我修改了该解决方案以允许子文件夹。以下代码从指定文件夹的每个子文件夹返回N个随机文件

 @echo off
 REM Display N random episodes for each podcast folder (~20 min.)
 REM Solution Template found at: https://stackoverflow.com/questions/10978107
 chcp 1254
 setlocal disableDelayedExpansion
 set /p COUNT=Select Desired Number of Random Episodes per Album:
 REM This loop captures the subfolder names and sends them to loop1
 for /d %%f in (G:\itunes\Podcasts\* H:\itunes\Podcasts\*) do (
 set /a ind = 0
 set buffer="%%f"
 call :loop1 %buffer%
 )

 :loop1
   for /f "tokens=* delims=" %%g in ('dir %buffer% /a:-h-s-d /b /s') do (
   setlocal enableDelayedExpansion
   for %%N in (!ind!) do (
     endlocal
 REM The following dynamically creates variables, WP1, WP2 ... which are later randomized
     set "wp%%N=%%g"
   )
   set /a ind += 1
 )

 setlocal enableDelayedExpansion
 for /l %%g in (1, 1, %COUNT%) do (
   set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind%"
   for %%N in (!num!) do echo !wp%%N!
 )

使用for循环内的
Call:label parameters
调用子函数。这样,您就不需要延迟扩展。请根据您的评论做出回答。但是,我会使用raw
call:setfile
并避免提供参数。只需在调用setfile之前设置变量
buffer
。考虑调用另一个子程序<代码>:ECHOFILE 而不是%%A/EXECUT>循环的最后一个<代码>。或者,ToGEL延迟扩展,以便仅在需要时启用,所以在读取<代码> < <代码> >变量>代码> %%I < /代码>时被禁用;您需要注意的是,环境(变量)更改在
endlocal
之后不再可用。感谢您的回复。面条,当我实际使用set“file!n!=%%g”构建参数时,应该传递哪些参数。。。文件1,文件2,文件3…JosefZ,对不起,我不明白你的呼叫:setfile comment。你能详细说明一下吗。回想一下,我需要启用很多扩展功能(例如,如果没有它,随机函数将无法重新设定种子)。
 @echo off
 REM Display N random episodes for each podcast folder (~20 min.)
 REM Solution Template found at: https://stackoverflow.com/questions/10978107
 chcp 1254
 setlocal disableDelayedExpansion
 set /p COUNT=Select Desired Number of Random Episodes per Album:
 REM This loop captures the subfolder names and sends them to loop1
 for /d %%f in (G:\itunes\Podcasts\* H:\itunes\Podcasts\*) do (
 set /a ind = 0
 set buffer="%%f"
 call :loop1 %buffer%
 )

 :loop1
   for /f "tokens=* delims=" %%g in ('dir %buffer% /a:-h-s-d /b /s') do (
   setlocal enableDelayedExpansion
   for %%N in (!ind!) do (
     endlocal
 REM The following dynamically creates variables, WP1, WP2 ... which are later randomized
     set "wp%%N=%%g"
   )
   set /a ind += 1
 )

 setlocal enableDelayedExpansion
 for /l %%g in (1, 1, %COUNT%) do (
   set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind%"
   for %%N in (!num!) do echo !wp%%N!
 )