<p>I would like to rename files and folders recursively by applying a string replacement operation.</p>

<p>E.g. The word "shark" in files and folders should be replaced by the word "orca".</p>

<p><code>C:\Program Files\Shark Tools\Wire Shark\Sharky 10\Shark.exe</code> </p>

<p>should be moved to:</p>

<p><code>C:\Program Files\Orca Tools\Wire Orca\Orcay 10\Orca.exe</code></p>

<p>The same operation should be of course applied to each child object in each folder level as well.</p>

<p>I was experimenting with some of the members of the <code>System.IO.FileInfo</code> and <code>System.IO.DirectoryInfo</code> classes but didn't find an easy way to do it.</p>

<pre><code>fi.MoveTo(fi.FullName.Replace("shark", "orca"));
</code></pre>

<p>Doesn't do the trick.</p>

<p>I was hoping there is some kind of "genius" way to perform this kind of operation.
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­</p>

[{'answer_id': 17028, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 1, 'selected': False, 'text': '<p>So you would use recursion. Here is a powershell example that should be easy to convert to C#:</p>\n\n<pre><code>function Move-Stuff($folder)\n{\n    foreach($sub in [System.IO.Directory]::GetDirectories($folder))\n      {\n        Move-Stuff $sub\n    }\n    $new = $folder.Replace("Shark", "Orca")\n    if(!(Test-Path($new)))\n    {\n        new-item -path $new -type directory\n    }\n    foreach($file in [System.IO.Directory]::GetFiles($folder))\n    {\n        $new = $file.Replace("Shark", "Orca")\n        move-item $file $new\n    }\n}\n\nMove-Stuff "C:\\Temp\\Test"\n</code></pre>\n'}, {'answer_id': 17052, 'author': 'John Sheehan', 'author_id': 1786, 'author_profile': 'https://Stackoverflow.com/users/1786', 'pm_score': 0, 'selected': False, 'text': '<pre><code>string oldPath = "\\\\shark.exe"\nstring newPath = oldPath.Replace("shark", "orca");\n\nSystem.IO.File.Move(oldPath, newPath);\n</code></pre>\n\n<p>Fill in with your own full paths</p>\n'}]
['https://Stackoverflow.com/questions/16945', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']