我在一个文本文件中列出了25个网站,每个网站都在一行中,我想通过批处理文件以随机顺序打开它们。
websites.txt
...
google.com
facebook.com
...
我知道我需要使用for循环,但不知道如何从随机行中提取网址。我想过用......
for /f "tokens=%rannum%"
但是所有的网站都必须在同一条线上,而且我的测试不能很好。还需要一种方法来确保同一网站不会打开两次。
到目前为止我所拥有的......
@echo off
set file=openweb.txt
set /a total_lines=1
for /f %%a in ('Type %_File%^|Find "" /v /c') Do Set /a total_lines=%%a
set /a start_count=0
set "found=found.txt"
if exist "%found%" del "%found%"
copy NUL found.txt
if %start_count% NEQ %total_lines% (
:run_again
REM Randomly select a number between 1-26
set /a random_number=%random% %% 26-1
REM Validation Random number was not used already
findstr /m "%random_number%" %found%
if %errorlevel%==0 (
echo already found
goto:run_again
)
REM Open each website. Wait 2sec between each.
for /f %%a in (websites.txt) do (
start iexplore %%a
@ping 127.0.0.1 -n 2 -w 1000 > nul
)
REM write out Random Number to the .txt
@echo %random_number%>>%found%
set /a start_count+=1
)
欢迎任何有关如何使此代码更好的输入。谢谢
答案 0 :(得分:0)
试试这个:
@echo off
set file=openweb.txt
set /a lines=25
set /a skip=%random%%%lines%-1
more %file% +%skip% > temp.tmp
set /p target=< temp.tmp
del temp.tmp
Echo %target%