-1

The goal of this code eventually will be go get all other web pages within a folder and create hyperlinks to file with the name of the files. This code mostly works but puts all elements of the array on both links. I need help to separate them links to 1 per file (per element of array until all created)

Create a new web page

New-Item web.htm -Type file -force

Put default opening html tag in file

Add-Content -Path web.htm -Value '<HTML>'

Create an array to contain web hyperlinks to create

$pages = @('web1.htm', 'web2.htm')

for each item add a link with the file name to open.

FAILS HERE CREATING BOTH PAGES FOR EACH MEMBER OF ARRAY

for ($i = 0; $i -lt $pages.Count; $i++)
{Add-Content -Path "Web.htm" -Value "<a href=$pages[i]> $pages[i] </><br>" }

Close the html file tag

Add-Content -Path web.htm -Value '</HTML>'
2
New-Item web.htm -Type file -force
Add-Content -Path web.htm -Value '<HTML>'
$pages = @('web1.htm', 'web2.htm')
foreach($page in $pages)
    {
    Add-Content -Path web.htm -Value "<a href=$page> $page</a><br />" 
    }
Add-Content -Path web.htm -Value '</HTML>'
  • Your mistake is that there is no </a> tag. I also changed the looping mechanism to a foreach as it is easier to follow and debug. – Tony Hinkle Apr 20 '15 at 2:11
  • Thanks for the quick answer Tony that did the trick but I do not have enough points to mark as the answer. Moderator please set Tony as answer. – Joel T Apr 20 '15 at 2:13
  • @JoelT you can perform all actions on your own questions. I can see you've already given this the checkmark, but you should also be able to upvote it. – Mark Henderson Apr 20 '15 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.