2

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
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
grgoelyk
  • 397
  • 1
  • 3
  • 12
  • Every variable that changes inside of a parenthesised block of code must be read using delayed expansion (like `!skiplines!`, `!skip!` and `!RANDOM!`). **But:** Delayed expansion cannot be used in the option string for `for /F`, so `!skip!` does not work there; therefore you must move the `for /F` loop into a sub-routine; you can then provide `!skip!` as an argument and access it as `%1` in the sub-routine, which works within the option string of `for /F`... – aschipfl Feb 08 '17 at 14:51
  • See also [this answer](http://stackoverflow.com/a/39653402)... – aschipfl Feb 08 '17 at 15:13

1 Answers1

0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Why does this pause ("Press any key to continue...") after every echo? – grgoelyk Feb 09 '17 at 00:40
  • It doesn't, it pauses once using `timeout` after having output three random lines. – Compo Feb 09 '17 at 00:44
  • Oh, it was because I put "pause" as the last line. This works now. I removed the "Timeout -1" and "Exit/B" code. I also had to move the last three lines to the bottom of my entire batch script, or else it wouldn't work. Thanks. – grgoelyk Feb 09 '17 at 04:18