Windows 7 如何使用PowerShell递归替换文件中的字符串以及文件和文件夹名称?
使用PowerShell(尽管欢迎其他建议),如何递归循环目录/文件夹和Windows 7 如何使用PowerShell递归替换文件中的字符串以及文件和文件夹名称?,windows-7,powershell,Windows 7,Powershell,使用PowerShell(尽管欢迎其他建议),如何递归循环目录/文件夹和 将所有文件中的文本A替换为B 重命名所有文件,使A替换为B,最后 同时重命名所有文件夹,以便将A替换为B 未经测试,但应为您提供一个起点: $a = 'A'; $b = 'B'; $all = ls -recurse; $files = = $all | where{ !$_.PSIsContainer ); $files | %{ $c = ( $_ | get-itemcontent -replace $a,$
未经测试,但应为您提供一个起点:
$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 );
未经测试,也许我更幸运;-) $hello='hello' $world='world' $files=ls-递归|?{-not$\ PSIsContainer} foearch($files中的文件){ gc-路径$file |%{$|-替换$hello,$world}|设置内容$file ri-newname($file.name-replace$hello,$world) } ls-递归|?{$\.psicontainer}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) } $hello='hello' $world='world' $everything=ls-递归 foearch($thing in$everything){ 如果($thing.PSIsContainer-ne$true){ gc-path$thing |%{$|-replace$hello,$world}| Set Content$thing } ri-新名称($thing.name-替换$hello,$world) }
我会这样说:
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' }
排序对象用于确保首先列出子项(子项、文件),然后列出它们之后的目录。(12345)经过一些需求改进,我最终得到了以下脚本:
$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."
我需要为我自己和下面稍微好一点的脚本版本 我补充了以下内容:
请注意,答案的一个变化是,上面只在指定的文件夹中递归文件夹,而不是在根目录中。如果您不想这样做,那么只需设置
$recurse=true
,这样您就需要一个命令/函数,将文件内容、文件名和文件夹名中的A替换为B,或者在每种情况下A和B是分开的?“一个命令/函数,将文件内容、文件名和文件夹名中的A替换为B”是的。我可以说这是未经测试的,没有办法运行。:)它有点复杂,所以我会把它作为一个脚本来测试。问题是:为什么就像“在电脑周围”的人一样,你可能知道,“它不工作”就像你什么都不说一样有价值。错误消息很有用。否则,无需说“它不工作”。)这太棒了。当人们意识到这个脚本将允许你做什么时,你将获得更多的投票。。。谢谢。我的代码文件中有一些文件夹和名称空间,它们给了我一个问题名称,比如NameSpace.TestConsole(文件夹),我的一些名称空间也是如此,这个脚本生成了很多名为这些名称的空文件,所以它还以某种方式创建了新文件。有没有办法确保它只会取代而不是创造?@Breaknheart有什么理由吗?@Antony没有任何理由只是为了让它变得更好。
$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."
[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"
}
}