vba - looping and renaming files and folders

2019-07-19 18:29发布

I'm trying to rename a folder how has many subfolders and files inside a main folder the renaming is based to many values and its need to be dynamic (i'm changing films & subtitles names)

my problem is - when the folder name answer the values for the name change the macro can't find a path to continue the loop (if the folder do not answer the values then the macro works fine)

here what I get so far:

Sub moviestest()
    Call VideoLibrary("C:\movies")
End Sub

Private Sub VideoLibrary(path As String)
    Dim fso As New Scripting.FileSystemObject
    Dim fld As folder, f As folder, fl As File

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(path)

    For Each Q In Range("Qname")
        For Each fl In fld.Files

            myMovie = fl.Name
            extension = InStrRev(myMovie, ".")
            myNewMovie = _
                Replace( _
                    Replace( _
                        Replace( _
                            Replace( _
                                Left(myMovie, extension - 1), _
                            ".", " "), _
                        "-", " "), _
                    "_", " "), _
                Q, "") & _
                Mid(myMovie, extension)
            fso.MoveFile myMovie, myNewMovie
        Next
    Next


    For Each f In fld.SubFolders
          Call VideoLibrary(f.path)
    Next
End Sub

files name are looking something like this :

  • The.something.2013.1080p.BluRay.x264.YIFY.mkv The.something.2013.1080p.BluRay.x264.YIFY.sub Zero.something.2016.1080p.BluRay.x264-[YTS.AG].avi (and many more names)

Qname range is somethig like this : (named range in excel)

 bdrip
 x264
 veto
 heb
 BRRip
 XviD
 AC3
 EVO
 blaa
 1080p
 BluRay
 YIFY

this is my first time asking in this forum. I hope I made my question clear as possible any help will be appreciated

1条回答
做个烂人
2楼-- · 2019-07-19 18:50

The best approach is make a function that will return the new name. This will also make the code easier to read, modify and debug.

Sub moviestest()
    Call VideoLibrary("C:\movies")
End Sub

 Private Sub VideoLibrary(path As String)
    Dim fso As New Scripting.FileSystemObject
    Dim fld As folder, f As folder, fl As File

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(path)

    For Each fl In fld.Files
         fso.MoveFile fl.path, fl.ParentFolder & "\" & getNewMoiveName(fl.Name)
    Next

    For Each f In fld.SubFolders
        Call VideoLibrary(f.path)
    Next
End Sub

Function getNewMoiveName(myMovie As String) As String
    Dim Q
    Dim loc As Long
    Dim extension As String
    loc = InStrRev(myMovie, ".")
    extension = Right(myMovie, Len(myMovie) - loc)
    myMovie = Left(myMovie, loc - 1)
    myMovie = Replace(myMovie, ".", " ")
    myMovie = Replace(myMovie, "-", " ")

    For Each Q In Range("Qname")
        myMovie = Replace(myMovie, Q, "")
    Next

    getNewMoiveName = myMovie & "." & extension
End Function
查看更多
登录 后发表回答