0

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.

1 Answer 1

1

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)
 
}
Sign up to request clarification or add additional context in comments.

2 Comments

The entire file path is being replaced. Instead, I've slightly modified your script to suit the requirement. using assembly System using assembly System.IO $folder = "D:\Test db\Forms\Test" $schemas = [System.IO.Directory]::GetFiles("$folder", "*.XSD") foreach($schema in $schemas) { $fileName = [System.IO.Path]::GetFileNameWithoutExtension($schema) $text = [System.IO.File]::ReadAllText($schema) $text = $text.Replace("NewDataSet", $fileName+"DS") [System.IO.File]::WriteAllText($schema, $text) }
I suspected that was going to happen. I wasn't sure if you needed the full pathname or just the base name of the file. I figured you would need to make some modification of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.