I'm working on a Windows batch script. I found a way to set a variable to a random line from a text file. It's the chunk that starts at line 4 and echoes !current_proxy!
.
I tried copying this chunk to a different section of the batch file (the section with the big empty space around it) so I could get another random line if/when a certain file fails to download. Why isn't it working this time? Am I using the wrong symbols (such as %
and !
)? Thanks.
@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
Here is an example to show you that Call :label
can help.
This uses a slightly different method in that it sets every line in %proxlist%
to a variable at the outset, this is especially advantageous if that list isn't huge .
@Echo Off
Set "proxlist=proxies.txt"
For /F "Tokens=1* Delims=:" %%a In ('FindStr/N "^" "%proxlist%"') Do (
Set "line[%%a]=%%b"
Set "total=%%a"
)
Call :SetRand
Echo=%randline%
Call :SetRand
Echo=%randline%
Call :SetRand
Echo=%randline%
Timeout -1
Exit/B
:SetRand
Set/A "rand=(%RANDOM%%%total)+1"
Call Set "randline=%%line[%rand%]%%"
Whenever you need another random line just call the section of your script which does that and return to the point just after the Call
command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.