How to replace string in files and file and folder names recursively using PowerShell?

With PowerShell (although other suggestions are welcome) how to iterate over a directory / folder recursively and

  • replace text A with B in all files,
  • rename all files to replace A with B and the last
  • rename all folders also to replace A with B?
+7
source share
6 answers

With some clarifications to the requirements, I ended up with this script:

$match = "MyAssembly" $replacement = Read-Host "Please enter a solution name" $files = Get-ChildItem $(get-location) -filter *MyAssembly* -Recurse $files | Sort-Object -Descending -Property { $_.FullName } | Rename-Item -newname { $_.name -replace $match, $replacement } -force $files = Get-ChildItem $(get-location) -include *.cs, *.csproj, *.sln -Recurse foreach($file in $files) { ((Get-Content $file.fullname) -creplace $match, $replacement) | set-content $file.fullname } read-host -prompt "Done! Press any key to close." 
+12
source

I would say something like this:

 Get-ChildItem $directory -Recurse | Sort-Object -Descending -Property { $_.FullName } | ForEach-Object { if (!$_.PsIsContainer) { ($_|Get-Content) -replace 'A', 'B' | Set-Content $_.FullName } $_ } | Rename-Item { $_.name -replace 'A', 'B' } 

Sort-Object to ensure that the first children (sub-folders, files), and then the directories after them are listed. (12345)

+3
source

Unconfirmed, but should give you a starting point:

 $a = 'A'; $b = 'B'; $all = ls -recurse; $files = = $all | where{ !$_.PSIsContainer ); $files | %{ $c = ( $_ | get-itemcontent -replace $a,$b ); $c | out-file $_; } $all | rename-item -newname ( $_.Name -replace $a,$b ); 
0
source

Unconfirmed, maybe I’m better luck -)

  $ hello = 'hello'
 $ world = 'world'

 $ files = ls -recurse |  ?  {-not $ _. PSIsContainer} 

 foearch ($ file in $ files) {
   gc -path $ file |  % {$ _ -replace $ hello, $ world} |  Set-content $ file
   ri -newname ($ file.name -replace $ hello, $ world)
 }

 ls -recurse |  ?  {$ _. PSIsContainer} |  ri -newname ($ _. name -replace $ hello, $ world)

To use the same recursion:

  $ hello = 'hello'
 $ world = 'world'

 $ everything = ls -recurse 

 foearch ($ thing in $ everything) {
   if ($ thing.PSIsContainer -ne $ true) {
      gc -path $ thing |  % {$ _ -replace $ hello, $ world} |  Set-content $ thing
   }
   ri -newname ($ thing.name -replace $ hello, $ world)
 }
0
source

I need this for myself and below is a slightly better version of the script.

I added the following:

  • Detailed parameter support so you can see what changes the script has made.
  • Ability to specify folders so that you can limit changes.
  • Adding bower.json, txt and md to enable extensions.
  • First find and replace the content, rename later.
  • Do not replace the contents if the search string is not found (this avoids unnecessary changes on the modified date).
 [CmdletBinding(SupportsShouldProcess=$true)] Param() $match = "MyProject" $replacement = Read-Host "Please enter project name" $searchFolders = @("MyProject.JS", "MyProject.WebApi", ".") $extensions = @("*.cs", "*.csproj", "*.sln", "bower.json", "*.txt", "*.md") foreach($searchFolderRelative in $searchFolders) { $searchFolder = join-path (get-location) $searchFolderRelative Write-Verbose "Folder: $searchFolder" $recurse = $searchFolderRelative -ne "." if (test-path $searchFolder) { $files = Get-ChildItem (join-path $searchFolder "*") -file -include $extensions -Recurse:$recurse | Where-Object {Select-String -Path $_.FullName $match -SimpleMatch -Quiet} foreach($file in $files) { Write-Verbose "Replaced $match in $file" ((Get-Content $file.fullname) -creplace $match, $replacement) | set-content $file.fullname } $files = Get-ChildItem $searchFolder -filter *$match* -Recurse:$recurse $files | Sort-Object -Descending -Property { $_.FullName } | % { Write-Verbose "Renamed $_" $newName = $_.name -replace $match, $replacement Rename-Item $_.FullName -newname $newName -force } } else { Write-Warning "Path not found: $searchFolder" } } 

Note that one of the responses to the change is that the recurses folder above is only in the specified folders, not the root. If you do not want this, simply set $recurse = true .

0
source

Use Notepad ++ If you do not have this tool, you are missing a lot of functions necessary for working with files.

It has a replacement function that allows you to search in a given directory, give templates for files and replace them with regexp, an extended function, or a regular replacement.

Powerful tool.

It is free: ( http://notepad-plus-plus.org/download/v6.6.9.html )

0
source

All Articles