繁体   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