tags:

views:

478

answers:

3

I have a list of websites. I want a batch file which will open only first website in the list when i execute the bat file for first time and when i execute the batch file for second time it will open only the second website in the list and when i execute it for third time it will open only third website in the list and so on. Someone suggested me 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. Can someone help me further. 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
+2  A: 

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.

Joey
Thanks Johannes Rossel,But it seems something is wrong here. I made a list.txt file on my desktop containing list of above urls. Then I copied your above script edited set /p URL=<list.txt into set /p http://meebo.com=<list.txt as http://meebo.com being first line in my list.txt and then saved your script as Sequential execution.bat at desktop. But when i executed it opened my desktop window instead of opening http://meebo.com in firefox which is my default browser as new tab.
Why would you do that change? As long as your file is named `list.txt` and is in the same location as the batch file you don't have to change anything. What you've done now is creating an environment variable named `meebo.com`, the original one, namely `URL` is therefore empty. Please revert that change and try it again. I'm certain it works.
Joey
I edited it in.
Joey
Thanks for all help Johannes Rössel."Solution is peace of mind".
A: 

Thanks

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

Also, I found that if IE 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  allow IE to load, otherwise it will only show 1 URL   
 rem  N=Nothing shown  D=default (Y for Y/N)  T=timeout (7 sec)  
@choice /N /D Y /T 7 > nul:
ThanqQ
A: 

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 :)

ThanqQ