@echo off
setlocal EnableDelayedExpansion
set /a N=0
for /f "tokens=* delims=" %%g in ('dir !FOLDERPATH! /b') do (
setlocal DisableDelayedExpansion
set "item=%%g"
endlocal
set /a N+=1
REM next line loses exclamation marks. replacing %%g with %%item%% gives error: not defined $$ variable
call set "$$%%N%%=%%g"
)
set "ind=%N%"
REM View the results
echo !ind!
for /f "tokens=* delims=" %%i in ('set $$') do echo %%i
pause
EXIT /b
我看我的問題(stackoverflow.com/questions/3262287許多解決方案的變量; stackoverflow.com/questions/28682268; stackoverflow.com/questions/29869394; stackoverflow.com/questions/3262287),我仍然很困惑。任何幫助讚賞保留跨DisableDelayedExpansion ENDLOCAL
我有一個包含感嘆號(和百分號和&符號)的mp3文件名的文件夾。上面的子程序應該用這些文件名填充一個數組= $$。它被稱爲2000次,每次換一個不同的文件夾。
我想使用EnableDelayedExpansion作爲主setlocal(快兩倍)。在我的整個批處理程序中,這只是行(設置「item = %% g」),我需要DisableDelayedExpansion。我需要知道如何有效地通過DisableDelayedExpansion endlocal邊界(並進入$$集)傳遞這個變量(item)。另外,我想我可以填充殘疾人環境中的$$設置,然後我需要跨越邊界傳遞設置。
我在尋找速度。我可以在整個子程序中使用Disabled,但是這會使處理時間加倍(對於很少的感嘆號和百分比符號)。
根據我的反應,我發現包括整個批處理腳本(簡化版)可能會很好。該腳本需要28分鐘才能在我的小型機器上運行我的數據庫。它處理感嘆號,百分號,&符號以及其他任何可以扔到它的東西。
@echo off
chcp 1254>nul
setlocal DisableDelayedExpansion
set /p COUNT=Select Desired Number of Random Episodes per Album:
for /d %%f in (H:\itunes\Podcasts\*) do (
set buffer="%%f"
set /a ind = 0
call:Set$$Variables
setlocal EnableDelayedExpansion
if !COUNT! LEQ !ind! (set DCOUNT=!COUNT!) ELSE set DCOUNT=!ind!
for /l %%g in (1, 1, !DCOUNT!) do (
call:GenerateUniqueRandomNumber
for %%N in (!num!) do echo !$$%%N!>>"%USERPROFILE%\Desktop\RandomEpisodes.m3u8"
)
endlocal
)
pause
Exit /b
:Set$$Variables
set /a N = 0
for /f "tokens=* delims=" %%g in ('dir %buffer% /b') do (
set "item=%%g"
set /a N+=1
call set "$$%%N%%=%%item%%"
)
set "ind=%N%"
EXIT /b
:GenerateUniqueRandomNumber
:nextone
set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% !ind! + 1"
for %%N in (!num!) do (
if !RN%%N!==1 (
goto:nextone
)
set "RN%%N=1"
)
EXIT /b
對以下內容進行簡單更改,並在13分鐘後運行。但它不處理感嘆號(bangs =!)。
@echo off
chcp 1254>nul
setlocal EnableDelayedExpansion
set /p COUNT=Select Desired Number of Random Episodes per Album:
for /d %%f in (H:\itunes\Podcasts\*) do (\
setlocal
set buffer="%%f"
set /a ind = 0
call:Set$$Variables
if !COUNT! LEQ !ind! (set DCOUNT=!COUNT!) ELSE set DCOUNT=!ind!
for /l %%g in (1, 1, !DCOUNT!) do (
call:GenerateUniqueRandomNumber
for %%N in (!num!) do echo !$$%%N!>>"%USERPROFILE%\Desktop\RandomEpisodes.m3u8"
)
endlocal
)
pause
Exit /b
:Set$$Variables
set /a N = 0
for /f "tokens=* delims=" %%g in ('dir %buffer% /b') do (
set "item=%%g"
set /a N+=1
call set "$$%%N%%=%%item%%"
)
set "ind=%N%"
EXIT /b
:GenerateUniqueRandomNumber
:nextone
set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% !ind! + 1"
for %%N in (!num!) do (
if !RN%%N!==1 (
goto:nextone
)
set "RN%%N=1"
)
EXIT /b
處理包含驚歎號的極少數文件名的處理時間超過了兩倍。資源 - 時間豬是子例程Set $$ Variables。 所以我希望有人能在這兩次之間找到一個快樂的中間地帶:13分鐘比28分鐘。在我看來,必須有一種方法來處理15分鐘差異中的十幾個受影響的文件。
有沒有你的文件名cotain'!'? – jeb
你的setlocal disabledDelayedExpansion/endlocal塊不會設置任何變量,因爲它在endlocal後被銷燬 – jeb
我想你的代碼通常是一個函數,或者你是否將所有代碼按順序從上到下寫入? – jeb