title: Get random line from text file in Windows batch
body: <p>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 <code>!current_proxy!</code>.</p>
  
  <p>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 <code>%</code> and <code>!</code>)?  Thanks.</p>
  
  <pre><code>@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 "" ^&lt; %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" &amp; 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 "" ^&lt; %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" &amp; goto continue
              :continue
              echo/!current_proxy!
  
  
  
  
  
  
           )
  
  
        )
     )
  )
  
  pause
  </code></pre>