1

I have many folder ex: folder1,folder2,folder3... about folder100

In those folder have many files ex: 1.html,2.html,3.html,4.html...about 20.html

I want to replace some text in those all html file in all folder but not all text i want to replace is same. ex:(for 1.html, i want to replace ./1_files/style.css to style.css) and (for 2.html, i want to replace ./2_files/style.css to style.css)....

So i try something like this and it work well

Get-ChildItem "*\1.html" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace './1_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\2.html" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace './2_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\3.html" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace './3_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\4.html" -Recurse | ForEach-Object -Process {
    (Get-Content $_) -Replace './4_files/style.css', 'style.css' | Set-Content $_
}

but i have to write many of those code "\4.html" "\5.html" "*\6.html" ...

i try this but it do not work

Do { 
    $val++ 
    Write-Host $val

    $Fn = "$val.html"

    Get-ChildItem "*\$Fn" -Recurse | ForEach-Object -Process {
        (Get-Content $_) -Replace './$val_files/style.css', 'style.css' | 
            Set-Content $_
    }
} while($val -ne 100) 

Please show me correct way to do..loop replace thanks you

3
  • please add a sample of one of the data files you want changed and how the result should look. please add it to your Question & wrap it in code formatting so that folks can find & read it easily. Commented Dec 25, 2021 at 10:03
  • 1
    -Replace './$val_files/style.css' --> -Replace "./$val_files/style.css". Single-quoted strings are not interpolated. Commented Dec 25, 2021 at 11:50
  • BTW, -Recurse is superfluous because it only works when -Path argument points to a folder. You could even replace Get-ChildItem by Get-Item. Commented Dec 25, 2021 at 11:52

1 Answer 1

5

Assuming all your subfolders can be found inside one source folder path, you can do below to do the replacement in all those files:

# the path where all subfolders and html files can be found
$sourcePath = 'X:\Wherever\Your\Subfolders\Are\That\Contain\The\Html\Files'
Get-ChildItem -Path $sourcePath -Filter '*.html' -Recurse -File |
# filter on html files that have a numeric basename
Where-Object {$_.BaseName -match '(\d+)'} | ForEach-Object {
    # construct the string to repace and escape the regex special characters
    $replace = [regex]::Escape(('./{0}_files/style.css' -f $matches[1]))
    # get the content as one single multiline string so -replace works faster
    (Get-Content -Path $_.FullName -Raw) -replace $replace, 'style.css' |
    Set-Content -Path $_.FullName
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks you this work well, but can you explain to me about use of 1. (\d+) 2. [regex]::Escape and {0} and $matches[1] i am new in powershell
and how to convert string (3.html) to integer (3) then sub(-) 1 from it i try this $string = "3.html" $integer = [int]$string-1 but not work
@rarksoca (\d+) means to (regex) capture 1 or more digits in a $matches object. [regex]::Escape() is a method to escape characters that have special meaning in regular expressions like the dot by placing a backslash in front of that character. {0} is a placeholder that wil be filled in through the -f Format operator.
@rarksoca You cannot do arithmatic on a string like 3.html. You CAN do that on the 3 by casting it to [int]. That is what the $matches[1] value stores in this case (remember the (\d+) regex)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.