C:\inetpub\wwwroot\apple\login.aspx
C:\inetpub\wwwroot\orange\login.aspx
C:\inetpub\wwwroot\banana\login.aspx
C:\inetpub\wwwroot\pear\login.aspx
I just came across this example:
# PowerShell Foreach File Example
Clear-Host
$Path = "C:\Windows\System32\*.dll"
Get-ChildItem $Path | Foreach-Object {
Write-Host $_.Name
}
So I would do:
# PowerShell Foreach File Example
Clear-Host
$Path = "C:\Windows\System32\*.dll" //here is where I'm not sure of what to do
Get-ChildItem $Path | Foreach-Object {
//insert stuff
}
In my "not sure of what to do" comment how would I change it so it would loop through different sub-directories of windows? Or back to the beginning, loop through each fruit?
Do you want to look for dll files in C:\Windows\System32 or fruit-named aspx files in C:\Inetpub\wwwroot?
Hi,
This will find all aspx files under your fruit folders:
Get-ChildItem 'C:\inetpub\wwwroot\*\*.aspx' | ForEach { $_.FullName }
All the loop does at this point in time is display the full path of the file.
Makes sense. I'll try it out.
Thanks!
OK. It works, but only for the first file. I have:
$LoginFolderPath = "C:\inetpub\wwwroot\*\login.aspx"; $LoginFile = $(get-childitem "$LoginFolderPath"); foreach($file in $LoginFile){ (Get-Content "login.aspx") | ForEach-Object{$_ -replace "<title>Welcome</title>","<title>Welcome to Synergy!</title>"} | Set-Content "login.aspx" }
It changes the first login.aspx which is in "C:\inetpub\wwwroot\". However, it does not change the other login.aspx in the subdirectories. I think I understand why - because the "Get-Content" in the foreach loop asks for login.aspx and not like it says in $LoginFolderPath.
How do I fix it? Can I do "(Get-Content "$LoginFolderPath")...."?
Just tried the "(Get-Content "$LoginFolderPath") and it did not work
It's not going into the subdirectories it looks like. I moved the login.aspx from C:\inetpub\wwwroot\ into a test folder, so no login.aspx was in it. I then ran it and nothing happened to the file. So what's going on?
Hi,
Try it this way:
Get-ChildItem 'C:\inetpub\wwwroot\*\login.aspx' | ForEach { (Get-Content $_.FullName) | ForEach { $_ -replace "<title>Welcome</title>","<title>Welcome to Synergy!</title>" } | Set-Content $_.FullName }
This keeps everything in the pipeline.
Still does not work
How so? Worked just fine in my testing.
- Edited by OregonState16 19 hours 41 minutes ago
How'd you test it? I'll try a different type of test. And you're using powershell 1.0 right? I may have figured out my issue but let me see if I'm right.
I created your folder structure and dummy files for testing.
Also, almost no one is using PowerShell 1.0 any longer. Minimum version you'll find now is 2.0. I'm runnin
I have another related question. Could I set the "<title>Welcome</title>" as a variable and the ""<title>Welcome to Synergy!</title>" as a variable. So for example:
$variable1 = "<title>Welcome</title>"
$variable2 = "<title>Welcome to Synergy!</title>"
Get-ChildItem 'C:\inetpub\wwwroot\*\login.aspx' | ForEach {
(Get-Content $_.FullName) | ForEach { $_ -replace $variable1,$variable2 } | Set-Content $_.FullName }
??
I'm in the process. I know to test it myself but I thought I'd post this question in case there was an obvious no, and then I would ask why etc etc.
I tried it and it did work.
- Edited by OregonState16 19 hours 9 minutes ago
- Edited by OregonState16 Thursday, May 22, 2014 2:52 PM
I'm in the process. I know to test it myself but I thought I'd post this question in case there was an obvious no, and then I would ask why etc etc.
I tried it and it did work.
- Edited by OregonState16 Thursday, May 22, 2014 3:24 PM