簡體   English   中英

從Windows批處理中的文本文件中獲取隨機行

[英]Get random line from text file in Windows batch

我正在研究Windows批處理腳本。 我找到了一種方法,可以將變量設置為文本文件中的隨機行。 這是從第4行開始並回聲的塊!current_proxy!

我嘗試將這個塊復制到批處理文件的不同部分(周圍有大空空間的部分)所以如果/當某個文件無法下載時,我可以得到另一個隨機行。 這次為什么不工作? 我使用了錯誤的符號(例如%! )嗎? 謝謝。

@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

這是一個示例,向您展示Call :label可以提供幫助。

這使用了稍微不同的方法,因為它將%proxlist%中的每一行設置為一開始的變量,如果該列表不是很大,這是特別有利的

@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%]%%"

每當你需要另一個隨機行時,只需調用腳本中執行該操作的部分,然后返回到Call命令之后的那一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM