2010-07-14 8 views
0

참고 :이 코드는 시스템 조작이 아닌 코드입니다.RegEx를 사용하여 폴더 이름 바꾸기

나는 몇 개의 드릴 다운 (기본적으로 폴더 안의 폴더 안에있는 폴더 ...)이 있습니다. RegEx를 사용하여 폴더 이름의 이름을 바꾸는 방법을 아는 사람이 있는지 궁금합니다. 기본적으로 ~ ~을 제거해야합니다. "# % & * : <>?/\ {| } 폴더 이름에. 나는 내가 "밑바닥"부터 (마지막 가능한 드릴 다운 폴더를 의미하고 가장 낮은 레벨에서 가장 높은 레벨까지 폴더의 이름을 바꾸기 시작해야 함) 시작할 필요가 있다고 생각하기 때문에 이것을 수행하는 방법을 궁금해하고있었습니다.

아무도 아이디어가 있습니까?

그래서 나는 다음이 코드를 가지고 :

내가 여기 직면하고있어 가장 큰 문제는 이러한 경로는 최고 수준의 폴더에 가장 낮은 수준 폴더에서 이름을 변경해야하거나 내가 점점 계속 것이다
public partial class CleanPathResults : Form 
{ 
    public CleanPathResults() 
    { 
     InitializeComponent(); 
    } 

    public void Sanitizer(List<string> dirtyPaths) 
    { 
     string regPattern = (@"[~#&!%+{}]+"); 
     string replacement = " "; 

     Regex regExPattern = new Regex(regPattern); 
     Regex regExPattern2 = new Regex(@"\s{2,}"); 

     StreamWriter errors = new StreamWriter(@"S:\test\Errors.txt", true); 


     var dirCount = new Dictionary<string, int>(); 


     dataGridView1.Rows.Clear(); 

      try 
      { 

       foreach (string invalidPaths in dirtyPaths) 
       { 
       string sanitizedPath = regExPattern.Replace(invalidPaths, replacement); 
        sanitizedPath = regExPattern2.Replace(sanitizedPath, replacement); 


         DataGridViewRow clean = new DataGridViewRow(); 
         clean.CreateCells(dataGridView1); 
         clean.Cells[0].Value = invalidPaths; 
         clean.Cells[1].Value = sanitizedPath; 

         dataGridView1.Rows.Add(clean); 
         System.IO.Directory.Move(invalidPaths, sanitizedPath); 




       } 
       } 

      catch (Exception e) 
      { 
       throw; 
       //errors.Write(e); 


      } 


    } 

디버깅 중에 발생하는 오류입니다.

내 질문은 어떻게 하나의 드라이브로 드릴 다운합니까, 트리 뷰에서 LOWEST 폴더로 이동 폴더를 위쪽으로? 경로 : G : \ Test ~ \ 예 : % a Test \ test & test \ testing !! \ ## test.txt

어떻게 이름을 바꿀 수 있습니까? 테스트! 폴더와 내 길을 그래서 내가 어떤 오류가 발생하지 않습니다?

답변

0

코드가 너무 복잡해 보입니다. 다음은 디렉토리 이름을 삭제하는 스 니펫입니다.

static void SanitizeFolders(DirectoryInfo root) 
{ 
    string regPattern = (@"[~#&!%+{}]+"); 
    string cleanName = ""; 
    Regex regExPattern = new Regex(regPattern); 

    foreach (DirectoryInfo sub in root.GetDirectories()) 
    { 
     if (regExPattern.IsMatch(sub.Name)) 
     { 
      /* set cleanName appropriately... */ 

      sub.MoveTo(cleanName); 
     } 
    } 

    //recurse into subdirectories 
    foreach (DirectoryInfo sub in root.GetDirectories()) 
    { 
     SanitizeFolders(sub); 
    } 
} 
+0

그러나 첫 번째 foreach 루프에서이 폴더를 시도했지만 foldername에 GetDirectories() 메서드가 없습니다. 파일을위한 것이 아닌가? – yeahumok

+0

@yeahumok :이 컴파일되지 않았거나 당신이 보낸 DirectoryInfo 개체에 하위 폴더가 없다는 말입니까? –

0

DirectoryInfoName 속성을 원합니다. 디렉토리와 새 이름이 있으면 Directory.Move 함수를 사용하여 이름을 바꿀 수 있습니다.

디렉토리의 하위 디렉토리로 드릴 다운하려는 경우 null 또는 빈 배열을 반환 할 때까지 상위 디렉토리에서 DirectoryInfo.GetDirectories 메소드를 사용할 수 있습니다. 가장 쉬운 방법은 재귀를 이용하는 것입니다.

이 기능을 사용하면 드릴 다운 할 수 있습니다, 당신은 상위 디렉토리 시작 : 폴더의 이름을 변경 할 경우

private void DrillDown(string folderPath) { 
     DirectoryInfo info = new DirectoryInfo(folderPath); 

     DirectoryInfo[] directories = info.GetDirectories(); 
     foreach(DirectoryInfo directory in directories) { 
      fixFolder(directory.FullName); 
     } 
     renameFolder(folderPath); 
    } 

renameFolder이다.

+0

나는 미안하지만, C/C를 처음 접했을 때 어떻게 구현합니까? – yeahumok

관련 문제