如何用PowerShell递归地替换文件和文件和文件夹名称中的字符串?
使用PowerShell(虽然其他建议,欢迎),如何做一个递归循环目录/文件夹,并为B中的所有文件如何用PowerShell递归地替换文件和文件和文件夹名称中的字符串?
- 替换文本A,
- 重命名的所有文件,这样一个被B取代,最后
- 还重命名所有文件夹,以便A被B替换?
少数要求改进,我结束了这个脚本:
$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."
这真棒。当人们意识到这个脚本允许你做什么的时候,你会得到更多的赞扬......谢谢。 – 2012-09-06 21:22:25
我在代码文件中有一些文件夹和命名空间,给我一个像NameSpace.TestConsole(文件夹)这样的问题名称,对于我的一些命名空间也是如此,然后这个脚本创建了很多称为这些名称的空文件,所以它也创建新文件。有什么方法可以确保它只取代而不是创建? – FRoZeN 2013-08-07 19:14:55
未经检验的,但应该给你一个起点:
$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);
我可以说这是未经测试的,没有办法可以运行。 :) – JasonMArcher 2011-04-13 16:12:08
未经检验的,可能是我比较幸运;-)
$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)
要使用相同的递归:
$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) }
这很复杂,所以我会测试它作为一个脚本。 – 2011-04-13 19:35:51
我会去这样的事情:
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
用于确保列出第一个子项(子目录,文件),然后列出目录。 (12345)
它不起作用。 – 2011-04-27 10:15:02
问题是:为什么? :)作为一个“围绕计算机”的人,你可能会知道,“它不工作”就像你不说话一样值得。错误消息很有帮助。否则,不需要说“它不工作”。 :) – stej 2011-04-27 10:58:42
我需要这种为自己和下面稍微好一点的版本的脚本。
我补充如下:
- 支持详细的参数,这样你可以看到什么样的变化脚本做出。
- 能够指定文件夹以便限制更改。
- 添加bower.json,txt和md以包含扩展名。
- 先搜索并替换内容,稍后再重新命名。
- 如果未找到搜索字符串,则不要替换内容(这可避免修改日期中的不必要更改)。
[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"
}
}
要注意,从答案一个变化是,上述递归文件夹只能在指定的文件夹,而不是在根。如果你不想要,那么只需设置$recurse = true
。
@BrokenHeart任何你想要[更改我的编辑](http://stackoverflow.com/posts/23103932/revisions)的原因? – Antony 2014-04-16 08:54:38
@Antony没有理由让它变得更好。 – 2014-04-16 09:05:17
使用记事本++ 如果您没有此工具 - 您错过了许多文件操作所需的功能。
它具有替换功能,允许您在给定的目录中进行搜索,为文件指定模式并使用正则表达式替换,扩展功能或正常替换。
强大的工具。
所以,你想一个命令/函数,将在文件内容,文件名和文件夹名称为B代替A或有A和B分别在各自的那些情况? – 2011-04-13 14:25:19
“就是这样一种情况,”将文件内容,文件名和文件夹名称中的A替换为B的一个命令/函数“就是这种情况。 – 2011-04-13 18:53:48