Home > Net >  Powershell script to read all files in folders and replace a text
Powershell script to read all files in folders and replace a text

Time:10-12

I've few folders and nested sub-folders. I would like to filter all the files in the folder including the sub folders and list files with extension .XSD and search for the text called NewDataSet and replace the text with the name of the File.

I would like to perform this using Windows Powershell (ps1) script. Please help me out.

CodePudding user response:

Try following :

using assembly System
using assembly System.IO

$folder = "c:\temp"

$schemas = [System.IO.Directory]::GetFiles("$folder", "*.XSD")

foreach($schema in $schemas)
{
   $text = [System.IO.File]::ReadAllText($schema)
   $text = $text.Replace("NewDataSet", $schema)
   [System.IO.File]::WriteAllText($schema, $text)
 
}
  • Related