marcoswebot
New Member
- Joined
- Apr 8, 2019
- Messages
- 4
- Thread Author
-
- #1
Hello. I have several hundred text files in a folder. I would like to replace the text "Hello" present in every files with the actual "filename".
How to do that? Can I use a Script or PowerShell?
Thank you!
How to do that? Can I use a Script or PowerShell?
Thank you!
Solution
With PowerShell
Code:
$SearchDirectory = "C:\path\to\directory"
$Files = Get-ChildItem -Path $SearchDirectory -File
foreach($File in $Files)
{
$FileData = Get-Content $File.FullName
$ReplaceString = "Hello"
$ReplaceData = "Hello+$($File.Name)"
$NewFileData = $FileData.Replace($ReplaceString,$ReplaceData)
Out-File -FilePath $File.FullName -InputObject $NewFileData -Force
}
- Joined
- Jul 4, 2015
- Messages
- 8,998
With PowerShell
Code:
$SearchDirectory = "C:\path\to\directory"
$Files = Get-ChildItem -Path $SearchDirectory -File
foreach($File in $Files)
{
$FileData = Get-Content $File.FullName
$ReplaceString = "Hello"
$ReplaceData = "Hello+$($File.Name)"
$NewFileData = $FileData.Replace($ReplaceString,$ReplaceData)
Out-File -FilePath $File.FullName -InputObject $NewFileData -Force
}
marcoswebot
New Member
- Joined
- Apr 8, 2019
- Messages
- 4
- Thread Author
-
- #3
With PowerShell
Code:$SearchDirectory = "C:\path\to\directory" $Files = Get-ChildItem -Path $SearchDirectory -File foreach($File in $Files) { $FileData = Get-Content $File.FullName $ReplaceString = "Hello" $ReplaceData = $File.Name $NewFileData = $FileData.Replace($ReplaceString,$ReplaceData) Out-File -FilePath $File.FullName -InputObject $NewFileData -Force }
Thanks Neemobeer, but this did not work for me.
I did not explain very good. I try again:
What I need to do is to change the TEXT "Hello" with the TEXT "Hello+filename"
marcoswebot
New Member
- Joined
- Apr 8, 2019
- Messages
- 4
- Thread Author
-
- #5
Code changed to reflect that
Thanks again!! This is not working :-(
marcoswebot
New Member
- Joined
- Apr 8, 2019
- Messages
- 4
- Thread Author
-
- #7
Neemobeer, thanks again for your help.
It works but somehow when I open the file with my program i get this: ÿþ% instead of the full text.
If I open the program with text editor it works but with my special text editor no :-/ Do you know which could be the reason?
Thanks a lot :-D
It works but somehow when I open the file with my program i get this: ÿþ% instead of the full text.
If I open the program with text editor it works but with my special text editor no :-/ Do you know which could be the reason?
Thanks a lot :-D
Similar threads
- Article
- Replies
- 0
- Views
- 8
- Replies
- 0
- Views
- 301
- Article
- Replies
- 0
- Views
- 74
- Solved
- Replies
- 1
- Views
- 1K