[VB 2015]正则表达式重命名文件&资料夹 [英] [VB 2015] Regex Renaming Files & Folders

查看:73
本文介绍了[VB 2015]正则表达式重命名文件&资料夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub btnClean_Click(sender As Object, e As EventArgs) Handles btnClean.Click
        listTest.Items.Clear()
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(txtDirectory.Text, SearchOption.SearchAllSubDirectories)
            Dim Pattern As String = ".\d+0p" 'Look for files with ".1080p" or ".720p" indicating Torrent naming format.
            Dim m As Match = Regex.Match(foundFile, Pattern)
            If (m.Success) Then 'If a Torrent formatted name was found...
                Pattern = "(?<=.+)\.[\d\w]+$" 'Isolate the extention name and save it for later.
                Dim Extention As String = Regex.Match(foundFile, Pattern).ToString
                Pattern = ".+(?>[70][28]0p)" 'Delete the excess formatting after 1080/720p such as "BluRay" "x265" etc.
                Dim NewFile As String = Regex.Match(foundFile, Pattern).ToString
                NewFile = NewFile.Replace(".", " ") 'Replace the periods with spaces.
                Pattern = "[^\\]+\\[^\\]+\\[^\\]+\\.+" 'Check for directories with three "\"
                m = Regex.Match(foundFile, Pattern)
                If (m.Success) Then 'If there's three "\" then we need to rename directory.
                    Dim OldDirectory As String = Regex.Match(foundFile, Pattern).ToString
                    Dim regexDirectory As String = txtDirectory.Text.Replace("\", "\\")
                    Pattern = regexDirectory + "(?>[70][28]0p)+?" <------------------------------------------This is my problem.
                    Dim NewDirectory As String = Regex.Match(foundFile, Pattern).ToString
                    NewDirectory = NewFile.Replace(".", " ") 'Replace the periods with spaces.
                    listTest.Items.Add(NewDirectory)
                    MsgBox(OldDirectory + vbCr + NewDirectory)
                End If
            End If
        Next

        For Each foundFile As String In My.Computer.FileSystem.GetFiles(txtDirectory.Text) 'Cycle through files not in a subdirectory.
            Dim Pattern As String = ".+(?>[70][28]0p)" 'Look for files with ".1080p" or ".720p" indicating Torrent naming format.
            Dim m As Match = Regex.Match(foundFile, Pattern)
            If (m.Success) Then 'If a Torrent formatted name was found...
                Pattern = "(?<=.+)\.[\d\w]+$" 'Isolate the extention name to create a directory without it.
                Dim Extention As String = Regex.Match(foundFile, Pattern).ToString
                Pattern = ".+(?>\d+0p)" 'Delete the excess formatting after 1080/720p such as "BluRay" "x265" etc.
                Dim NewFile As String = Regex.Match(foundFile, Pattern).ToString
                NewFile = NewFile.Replace(".", " ") 'Replace the periods with spaces.
                Pattern = "\\(?>[^\\]+$)" 'Isolate file name from directory to name new folder.
                Dim Directory As String = Regex.Match(NewFile, Pattern).ToString
                listCleanTest.Items.Add(NewFile + Directory + Extention) 'Rename the new file attaching the extention back on.
            End If
        Next
    End Sub

大家好.我的4TB外部硬盘看起来很凌乱,因此我试图清理电影"存储区.回到编程时有点麻烦,因为我已经离开了很多年了.在实际修改任何文件名之前,我有两个列表 在我用来显示结果的表单上,确保在真正实现之前得到我想要的(当然,我已经备份了所有内容,以防万一!).我单击一个弹出目录的按钮,选择电影目录,然后信息 进入txtDirectory.Text.我正在访问的目录是"I:\ Movies"

第一个"For Each"搜索子目录以挑选文件,并通过删除每个文件具有的1080p/720p分辨率以外的所有内容来重命名它们.然后,找到具有相同命名方案并应该重命名的子目录 那些使用相同方法的人.问题是,它仅重命名文件,因为正则表达式贪婪,并且会转到行尾.我只是想不通.我尝试了以下一些方法:

Hello everyone. My 4TB external HDD is looking pretty messy so I was trying to clean up the "Movies" folder a bit while getting back into programming because I've been out of it for years. Before actually modifying any filenames I have two lists on the form that I'm using to display the results making sure I get what I want before I do it for real (Of course I've backed everything up just in case!). I click a button which brings up the directory, I select the movie directory and that information goes into txtDirectory.Text. The directory I'm accessing is "I:\Movies"

The first For Each searches through sub-directories picking out files, renaming them by deleting everything past the resolution 1080p / 720p which every file has. It then finds the sub-directories which have the same naming scheme and is supposed to rename those using the same method. Problem is, it's only renaming the file because the regex is greedy and goes to the end of the line. I just can't figure it out. I've tried some of the following:

Pattern = regexDirectory + "^(?>[70][28]0p)" ' To have it return the match from the beginning of the line, I've also added the carrot before regexDirectory
Pattern = "^" + regexDirectory + "(?>[70][28]0p)+?" ' To have it match as few times as possible. I've also tried a combination of the above and this.
Pattern = regexDirectory + "\\[^\\]+(?>[70][28]0p)" ' So it will specifically look for "I:\Movies\[Anything but the next backslash]1080p/720p" (Why this didn't work baffles me the most)

最后一个For Each不会检查子目录,而只是更正文件,然后将它们放在与电影文件同名的新子目录中.这部分效果很好.

我尝试了其他方法,但我记不清了.只是对我不起作用.我一直尝试使用的第一个文件夹名为"Black Mass.2015.1080p.AC3.x264".提前谢谢你的帮助.感谢您抽出宝贵的时间 读这个.

The last For Each doesn't check sub-directories and simply corrects the files and then places them in a new sub-directory with the same name as the movie file. This part works perfectly.

I've tried other things but I can't specifically remember. It's just not working for me. The first folder I've been trying this on is named "Black Mass.2015.1080p.AC3.x264." Thank you for your help in advance. I appreciate you all taking the time to read this.

推荐答案

使用Regex找到感兴趣的文件后,请切换到使用IO.Path来操纵文件路径.您可以GetFilenameWithoutExtension()并隔离目录路径,并在需要时使用DirectoryInfo对象进行进一步分析.我想 您可以完全不用Regex来完成重命名,这样做可以消除问题.
Once you've found the files of interest using Regex, switch to using IO.Path to manipulate the file path.  You can GetFilenameWithoutExtension() and isolate the directory path, further analyzing with DirectoryInfo objects if needed.  I think you can accomplish the rename entirely without Regex and doing so will eliminate the problem.


这篇关于[VB 2015]正则表达式重命名文件&amp;资料夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆