Batch file for opening one of a list of URLs

2020-06-29 08:44发布

I have a list of websites. I want a batch file which will open only the first website in the list when I execute the bat file for first time, and when I execute the batch file for the second time it will open only the second website in the list, and when I execute it for third time it will open only the third website in the list and so on.

Someone suggested me the following solution:

@echo off & setLocal EnableDELAYedExpansion

if not exist %TEMP%\runnum echo 1> %TEMP%\runnum
set /p R=<%TEMP%\runnum
set N=

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! equ !R! echo firefox %%a blabla
)
set /a R+=1
echo !R!> %TEMP%\runnum

But I don't know what he meant by (myfile) and blabla, because I don't know anything about scripting or programming.

How can I do this?

Assuming my list of websites are:

http://meebo.com
http://yahoo.com
http://google.com
http://orkut.com
http://facebook.com
http://msn.com
http://cnn.com
http://myspace.com
http://twitter.com

标签: batch-file
4条回答
你好瞎i
2楼-- · 2020-06-29 09:30

This is actually not that hard. myfile becomes the file name in which you store your list of URLs and blabla are simply additional parameters passed to Firefox.

Though there are a few points one could improve:

  • Relying on Firefox isn't the best thing. I'd suggest using

    start "" "%%a"
    

    instead, since that spawns the default browser instead of hardcoding a specific one.

  • Your batch will fail when the number of websites in your file is reached and probably just spawn a new Firefox window. Below I have created a batch which eliminates both problems:

    @echo off
    setlocal enableextensions
    rem fetch the first URL
    set /p URL=<list.txt
    rem Open the browser with that URL
    start "" "%URL%"
    rem Remove the URL from the front of the file ...
    more +1 list.txt | findstr /r /v "^$" > tmp_list.txt
    rem ... and put it to the end
    echo.%URL%>>tmp_list.txt
    del list.txt
    ren tmp_list.txt list.txt
    endlocal
    

This version doesn't rely on a specific browser. It will simply roll the file itself, by removing the first URL and sticking it to the end again. So as long as you have your URL file (which is list.txt in this case) it's pretty much self-contained. Code may be found in my SVN repository as well.

ETA: Explaining some parts of that batch:

set /p URL=<list.txt

This will the first line in list.txt to be stored in the environment variable URL. Usually set /p prompts for user input. By redirecting the file into this command we are basically pretending the file's contents were user input.

start "" "%URL%"

will open a web page, document, folder, whatever. start does The Right Thing™ automagically (mostly :)). If we give it a URL it will open the default browser with it, which is what we're using here. The two quotation marks around the URL will ensure that characters like & in URLs will get passed correctly to the browser, they have a special meaning otherwise. The two quotation marks directly following start are necessary when using quotation marks with start at all, unfortunately. Otherwise start would interpret the URL as the window title for a new console window which may not be exactly what we want here.

more +1 list.txt | findstr /r /v "^$" > tmp_list.txt

This has several parts. First of all more +1 causes a file to be output, skipping the first line. As we remember, the first line is the first URL we wanted to open (which should have happened already). What we want to do is to remove that URL from the start of the file and put it to the end. So the first step is to remove it from the start, which is what more +1 list.txt does here.

Then, whatever more prints gets passed into findstr. findstr is a handy utility to search for strings usually. What we do here is enable regular expressions with /r (sort of programmers' dream tool for handling text – if they could, they would write complete programs in regular expressions, but I digress). Then /v causes findstr to print every line not matching what we specify after that. The pattern we are searching for here is "^$" which is just reg-ex speak for "empty line".

So in one line we remove the first line from the file and remove any empty lines from it. (Those empty lines would cause the batch file to do weird things. Remember that start does mostly the right thing? This is one such case. An empty line in your file would cause an Explorer window with your current folder to appear, instead of a browser with the next web page. So we need to get rid of those.)

Finally we write everything those commands print into a new file, called tmp_list.txt. Don't worry, it won't linger around for too long.

echo.%URL%>>tmp_list.txt

This appends the URL just opened to our temporary list. Nothing fancy going on here.

del list.txt
ren tmp_list.txt list.txt

Finally we delete the old list and rename the temporary one to the old name again.

ETA: Since you requested a version which can open multiple pages in one go, what follows is a quick and dirty hack which enables just that:

@echo off
setlocal enableextensions
set num=3
for /l %%i in (1,1,%num%) do call :start
endlocal
goto :eof

:start
set /p URL=<list.txt
start "" "%URL%"
more +1 list.txt | findstr /r /v "^$" > tmp_list.txt
echo.%URL%>>tmp_list.txt
del list.txt
ren tmp_list.txt list.txt
goto :eof

No lengthy explanation this time, though. This post is already long enough. You can control the number of pages opening by changing the num variable near the top of the file.

set num=5

will cause five pages to open instead of three.

查看更多
乱世女痞
3楼-- · 2020-06-29 09:30

I expanded on Ventero's "Dirty hack" to get the lines in the text file to loop through the entire text file opening them up then ending. It works like a charm.

@echo off
setlocal enableextensions
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" Give.txt | find /C ":""

for /f %%a in ('!cmd!') do set number=%%a

set num=%number%
for /l %%i in (1,1,%num%) do call :start
endlocal
goto :eof

:start
set /p URL=<Give.txt
start "" "%URL%"
more +1 Give.txt | findstr /r /v "^$" > tmp_list.txt
echo.%URL%>>tmp_list.txt
del Give.txt
ren tmp_list.txt Give.txt
goto :eof
查看更多
走好不送
4楼-- · 2020-06-29 09:39

I had a URL with a space (%20) in it and really needed: start "" "(URL with space)"

Also, I found that if Internet Explorer wasn't loaded it would ignore the hardcoded list in my batch file while loading, resulting in just one URL from the list actually working. A delay after the first start works well:

 rem Allows Internet Explorer to load, otherwise it will only show one URL
 rem N=Nothing shown  D=default (Y for Y/N)  T=timeout (7 sec)
@choice /N /D Y /T 7 > nul:
查看更多
够拽才男人
5楼-- · 2020-06-29 09:40

I just found an old CHOICE.COM that didn't support /D and /T in the same way:

 rem Older versions of choice may need:  choice /N /Ty,7 > nul:

Just use choice /? if in doubt :)

查看更多
登录 后发表回答