Add one variable to another loop

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
thatsameer
Posts: 16
Joined: Fri Aug 03, 2018 7:02 am

Add one variable to another loop

Post by thatsameer » Mon Feb 18, 2019 7:50 am

Hi,

Im very confused.

I've read the webbrowser help but haven't got anywhere.

Here is my situaton:

I have 2 WebBrowser objects placed in my Sapien form ($webbrowser1 and $webbrowser 2).

I have a list of links in c:\users\admin\desktop\url.txt (https://google.co.uk and https://bbc.co.uk).

I want each line of the text file to be navigated by each $webbrowser in order.

$url = get-content c:\users\admin\desktop\url.txt
$webbrowser1.navigate($url)

This works if there is only https://google.co.uk in the txt file.

How can I make a kind of foreach loop for both variables, so for each $link in $url navigate to each $webbrowser1, 2, 3, 4 etc?

I need both variables to loop together, i dont know how.

Thanks

User avatar
jvierra
Posts: 13302
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add one variable to another loop

Post by jvierra » Mon Feb 18, 2019 8:34 am

Just enumerate the contents of the file in a ForEach-Object.

help foreach -online

Read the examples to learn how to use it.

Or use foreach()

help about_foreach

thatsameer
Posts: 16
Joined: Fri Aug 03, 2018 7:02 am

Re: Add one variable to another loop

Post by thatsameer » Mon Feb 18, 2019 8:56 am

Im sorry but I still don't understand how to make each line from text file .navigate to each sequential $webbrowserX

User avatar
jvierra
Posts: 13302
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add one variable to another loop

Post by jvierra » Mon Feb 18, 2019 9:14 am

You also have to select the browser.

Another way is:

$urls = Get-Content file
$webBrowser1.navigate($urls[0])
$webBrowser2.navigate($urls[1])

thatsameer
Posts: 16
Joined: Fri Aug 03, 2018 7:02 am

Re: Add one variable to another loop

Post by thatsameer » Mon Feb 18, 2019 2:19 pm

Thanks, your replies allowed me to probe further.
I've solved this in a clever way, by creating a new tab for each line in the notepad, adding to the form and adding webbrowser on top. Works very well.

I have another question on the tabcontrol but will post new topic as not very related to this one. thanks

Locked