我正在使用Windows批處理腳本。我發現了一種將變量設置爲文本文件中隨機行的方法。它是從第4行開始並且響應!current_proxy!
的塊。從Windows批處理文本文件中獲取隨機行
我試圖將這個塊複製到批處理文件的不同部分(其周圍有大的空白部分),以便在某些文件無法下載時我可以獲得另一個隨機行。爲什麼這次不工作?我是否使用錯誤的符號(如%
和!
)?謝謝。
@echo off
setlocal EnableDelayedExpansion
set proxies_list="proxies.txt"
:: # Count the number of lines in the text file and generate a random number.
for /f "usebackq" %%c in (`find /V /C "" ^< %proxies_list%`) do set lines=%%c
set /a random_number=%RANDOM% * lines/32768 + 1, skiplines=random_number-1
:: # Extract the line from the file.
set skip=
if %skiplines% gtr 0 set skip=skip=%skiplines%
for /f "usebackq %skip% delims=" %%c in (%proxies_list%) do set "current_proxy=%%c" & goto continue
:continue
echo/!current_proxy!
for %%a in (xml\*.xml) do (
for /l %%b in (0,1,337) do (
set /a "x=%%b%%26"
set /a "y=%%b/26"
set /a "tile_number=%%b+1"
if not exist "C:\Users\User\Desktop\panoid\tiles\%%~na\%%~na_tile!tile_number!.jpg" (
echo C:^\Users^\User^\Desktop^\panoid^\tiles^\%%~na^\%%~na_tile!tile_number!.jpg
"C:\Portable programs\wget64.exe" --read-timeout=10 --tries=3 -e use_proxy=on -e http_proxy=!current_proxy! -O "C:\Users\User\Desktop\panoid\tiles\%%~na\%%~na_tile!tile_number!.jpg" "http://updatethis.com"
FOR /F "usebackq" %%d IN ("C:\Users\User\Desktop\panoid\tiles\%%~na\%%~na_tile!tile_number!.jpg") DO set size=%%~zd
if !size! GTR 0 (
echo File not empty.
) ELSE (
echo File empty.
for /f "usebackq" %%c in (`find /V /C "" ^< %proxies_list%`) do set lines=%%c
set /a random_number=%RANDOM% * lines/32768 + 1, skiplines=random_number-1
set skip=
if %skiplines% gtr 0 set skip=skip=%skiplines%
for /f "usebackq %skip% delims=" %%c in (%proxies_list%) do set "current_proxy=%%c" & goto continue
:continue
echo/!current_proxy!
)
)
)
)
pause
每個在括號化代碼塊內部變化的變量都必須使用延遲擴展(例如'!skiplines!','!skip!'和'!RANDOM!')來讀取。 **但是:**延遲擴展不能在'for/F'的選項字符串中使用,所以'!skip!'在那裏不起作用;因此您必須將'for/F'循環移入子例程;然後你可以提供'!skip!'作爲參數,並在子例程中作爲'%1'訪問它,該函數在'for/F'的選項字符串中運行... – aschipfl
另請參閱[this answer](http ://stackoverflow.com/a/39653402)... – aschipfl