Can you create a RegEx using folder structures in file names?

ロック済み Can you create a RegEx using folder structures in file names?

  • 2012年4月14日 3:38
     
     

    I have many folders structured thus:-

    Author1
    --first book title.txt
    --second book title.rtf
    --third book
    ----third book Title.doc
    ----Cover.jpg

    Author2
    --Another book title.epub

    etc.

    What I want:-

    Author1
    --first book title - Author1.txt
    --second book title - Author1.rtf
    --third book
    ----third book Title - Author1.doc
    ----Cover.jpg

    Is there a RegEx that will append an " - " and the Author to each book title using the Multi Rename Tool?  The first 2 books are difficult enough and I suspect the third book structure is impossible?

    There can be 1 to many book titles and different file types involved in each Author folder.

    I also suspect that RegExs only work at one level  :(

    I'll be more than happy with any solution that will save me manual effort  :)

    I have about 400 authors and I have already done a couple of hundred manually

    Thanks in anticipation  :D

すべての返信

  • 2012年4月16日 8:31
    モデレータ
     
     回答済み コードあり

    Hi Louwind,

    Welcome to the MSDN Forum.

    Actually, I don't suggest you to use Regular expression.

    Please try the following code:

        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
            Dim dir As DirectoryInfo = New DirectoryInfo("D:\ChangeAuthors")
            For Each subDir In dir.GetDirectories()
                RenameFiles(subDir.FullName, subDir.Name)
            Next
        End Sub
    
        Private Sub RenameFiles(filePath As String, authorName As String)
            Dim dir As DirectoryInfo = New DirectoryInfo(filePath)
    
            For Each f In dir.GetFiles()
                My.Computer.FileSystem.RenameFile(f.FullName, f.Name & " - " & authorName & "." & f.Extension)
            Next
    
            For Each dic In dir.GetDirectories()
                RenameFiles(dic.FullName, authorName)
            Next
        End Sub

    During my test, my folder structure is like this:

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 2012年4月16日 9:49
     
      コードあり

    Try

    "^(?<head>\w+\sbook\s)(?=\w+\.(txt|rft|doc|equb))(?<title>.+)$"

    Then use Replace method as follows(Use replace method):

    Replace(input, @"${head}-${title}");

    string

    pattern = @"(?<head>\w+\sbook\s)(?=[^.]+\.(txt|rft|doc|equb))(?<title>.+)";
    Regex r = new Regex(pattern);
    string input = @"first book aa.txt";
    Console.WriteLine(r.Replace(input, @"${head}- ${title}"));


    Please Mark it as answer, if it helps solve your problem.




    • 編集済み Phape 2012年4月16日 9:52
    • 編集済み Phape 2012年4月17日 5:31
    • 編集済み Phape 2012年4月17日 5:33
    •