2011-04-13 79 views
7

使用PowerShell(雖然其他建議,歡迎),如何做一個遞歸循環目錄/文件夾,併爲B中的所有文件如何用PowerShell遞歸地替換文件和文件和文件夾名稱中的字符串?

  1. 替換文本A,
  2. 重命名的所有文件,這樣一個被B取代,最後
  3. 還重命名所有文件夾,以便A被B替換?
+0

所以,你想一個命令/函數,將在文件內容,文件名和文件夾名稱爲B代替A或有A和B分別在各自的那些情況? – 2011-04-13 14:25:19

+0

「就是這樣一種情況,」將文件內容,文件名和文件夾名稱中的A替換爲B的一個命令/函數「就是這種情況。 – 2011-04-13 18:53:48

回答

12

少數要求改進,我結束了這個腳本:

$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." 
+1

這真棒。當人們意識到這個腳本允許你做什麼的時候,你會得到更多的讚揚......謝謝。 – 2012-09-06 21:22:25

+0

我在代碼文件中有一些文件夾和命名空間,給我一個像NameSpace.TestConsole(文件夾)這樣的問題名稱,對於我的一些命名空間也是如此,然後這個腳本創建了很多稱爲這些名稱的空文件,所以它也創建新文件。有什麼方法可以確保它只取代而不是創建? – FRoZeN 2013-08-07 19:14:55

0

未經檢驗的,但應該給你一個起點:

$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); 
+1

我可以說這是未經測試的,沒有辦法可以運行。 :) – JasonMArcher 2011-04-13 16:12:08

0

未經檢驗的,可能是我比較幸運;-)

 
$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) 
} 
+0

這很複雜,所以我會測試它作爲一個腳本。 – 2011-04-13 19:35:51

3

我會去這樣的事情:

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)

+0

它不起作用。 – 2011-04-27 10:15:02

+2

問題是:爲什麼? :)作爲一個「圍繞計算機」的人,你可能會知道,「它不工作」就像你不說話一樣值得。錯誤消息很有幫助。否則,不需要說「它不工作」。 :) – stej 2011-04-27 10:58:42

0

我需要這種爲自己和下面稍微好一點的版本的腳本。

我補充如下:

  1. 支持詳細的參數,這樣你可以看到什麼樣的變化腳本做出。
  2. 能夠指定文件夾以便限制更改。
  3. 添加bower.json,txt和md以包含擴展名。
  4. 先搜索並替換內容,稍後再重新命名。
  5. 如果未找到搜索字符串,則不要替換內容(這可避免修改日期中的不必要更改)。
[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

+0

@BrokenHeart任何你想要[更改我的編輯](http://stackoverflow.com/posts/23103932/revisions)的原因? – Antony 2014-04-16 08:54:38

+0

@Antony沒有理由讓它變得更好。 – 2014-04-16 09:05:17

0

使用記事本++ 如果您沒有此工具 - 您錯過了許多文件操作所需的功能。

它具有替換功能,允許您在給定的目錄中進行搜索,爲文件指定模式並使用正則表達式替換,擴展功能或正常替換。

強大的工具。

它是免費的:(http://notepad-plus-plus.org/download/v6.6.9.html

相關問題