PowerShell Hack: How to replace text in multiple files 💪

avatar

PowerShell Hack: Mit diesem PowerShell-Skript kannst du Text in mehreren Dateien ersetzen.

Ich habe ein kleines PowerShell-Skript für Windows geschrieben, um Text in einem ganzen Verzeichnis und dessen Unterverzeichnissen zu ersetzen.

Das kann manchmal ganz praktisch sein, wenn man zum Beispiel in einem ganzen Ordner mit html-Dateien, etwas in allen Dateien ersetzen möchte und das Projekt nicht in Visual Studio geöffnet hat.

Das folgende Skript ersetzt im aktuellen Verzeichnis und dessen Unterverzeichnissen in allen Textdateien (*.txt) den Text "search" mit "substitute".

$files = Get-ChildItem . *.txt -rec
foreach ($file in $files)
{
(Get-Content -Raw $file.PSPath) -replace "search", "substitute" |
Set-Content $file.PSPath
}

Vorsicht: Bevor man das Skript verwendet, empfehle ich das angepasste Skript in einem Testordner zu testen und ein Backup des Ordners zu erstellen, auf dem man das Skript anwenden möchte.

powershellhero1.jpg

English

PowerShell Hack: With this PowerShell script you can replace text in multiple files.

I wrote a small PowerShell script for Windows to replace text in an entire directory and its subdirectories.

This can sometimes come in handy if, for example, you want to replace something in a folder of html files and you don't have the project open in Visual Studio.

The following script replaces the text "search" with "substitute" in the current directory and its subdirectories in all text files (.txt).*

$files = Get-ChildItem . *.txt -rec
foreach ($file in $files)
{
(Get-Content -Raw $file.PSPath) -replace "search", "substitute" |
Set-Content $file.PSPath
}

Caution: Before using the script, I recommend testing the customized script in a test folder and making a backup of the folder where you want to apply the script to.

Live your Secrets and Steem Prosper 🔥

xx Viki @vikisecrets

Blog post: Text in mehreren Dateien (in einem ganzen Verzeichnis) mit der Windows PowerShell ersetzen



0
0
0.000
11 comments
avatar

Du hast ein Upvote von mir bekommen, diese soll die Deutsche Community unterstützen. Wenn du mich unterstützten möchtest, dann sende mir eine Delegation. Egal wie klein die Unterstützung ist, Du hilfst damit der Community. DANKE!

0
0
0.000
avatar

Echt cool, wusste gar nicht dass du auch Skripte schreibst. LG Michael
!invest_vote

0
0
0.000
avatar

i hope i can be good in programing

0
0
0.000
avatar

Sehr interessant. Versuche mir nur gerade mal einen Use Case vorzustellen. Yours Querdenker !invest_vote

0
0
0.000