2008-08-19 10 views
0

문자열 바꾸기 작업을 적용하여 파일과 폴더의 이름을 재귀 적으로 바꾸고 싶습니다..NET에서 파일 시스템 분기의 이름을 바꾸는 가장 좋은 방법은 무엇입니까?

예. 파일 및 폴더에서 "상어"라는 단어는 "orca"라는 단어로 대체되어야합니다. 뿐만 아니라, 각 폴더 레벨에서 각 하위 개체에 적용 당연히 있어야

C:\Program Files\Orca Tools\Wire Orca\Orcay 10\Orca.exe

동일한 조작 :

C:\Program Files\Shark Tools\Wire Shark\Sharky 10\Shark.exe

가 이동한다.

System.IO.FileInfoSystem.IO.DirectoryInfo 클래스의 일부 멤버를 실험했지만 쉽게 할 수있는 방법을 찾지 못했습니다.

fi.MoveTo(fi.FullName.Replace("shark", "orca")); 

트릭을 수행하지 마십시오.

나는 이런 종류의 작업을 수행 할 수있는 "천재적 인"방법이 있기를 바랬습니다.

답변

1

그래서 재귀를 사용합니다. 여기에 C 번호로 쉽게 변환 할 수 있어야 PowerShell은 예입니다

function Move-Stuff($folder) 
{ 
    foreach($sub in [System.IO.Directory]::GetDirectories($folder)) 
     { 
     Move-Stuff $sub 
    } 
    $new = $folder.Replace("Shark", "Orca") 
    if(!(Test-Path($new))) 
    { 
     new-item -path $new -type directory 
    } 
    foreach($file in [System.IO.Directory]::GetFiles($folder)) 
    { 
     $new = $file.Replace("Shark", "Orca") 
     move-item $file $new 
    } 
} 

Move-Stuff "C:\Temp\Test" 
0
string oldPath = "\\shark.exe" 
string newPath = oldPath.Replace("shark", "orca"); 

System.IO.File.Move(oldPath, newPath); 

자신의 전체 경로에 기입