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
This is actually not that hard.
myfile
becomes the file name in which you store your list of URLs andblabla
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
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:
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:
This will the first line in
list.txt
to be stored in the environment variableURL
. Usuallyset /p
prompts for user input. By redirecting the file into this command we are basically pretending the file's contents were user input.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 followingstart
are necessary when using quotation marks withstart
at all, unfortunately. Otherwisestart
would interpret the URL as the window title for a new console window which may not be exactly what we want here.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 whatmore +1 list.txt
does here.Then, whatever
more
prints gets passed intofindstr
.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
causesfindstr
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.This appends the URL just opened to our temporary list. Nothing fancy going on here.
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:
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.will cause five pages to open instead of three.
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.
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:
I just found an old
CHOICE.COM
that didn't support /D and /T in the same way:Just use choice /? if in doubt :)