首页 文章

如何使用VBScript以递归方式重命名子文件夹中的文件

提问于
浏览
1

我写了这个脚本来组织我的视频库 . 为了保持一致的命名标准,我想要一种方法来重命名子文件夹中的文件 . 下面的代码的问题是,当我在结尾设置fso = Nothing时,它会破坏FileSystemObject,我无法找到一种方法来使用标准的For Each Next语句来调用它 . 与此同时,我已将其关闭,但如果我单步执行所有文件夹重命名,则无论文件夹如何,附加数字都会随着每个文件继续提升 . 理想情况下,这是我正在寻找的结果:

(我们以电视节目为例)文件夹:Breaking Bad Subfolder:Season 1 Files:Breaking S01E01,Breaking S01E02,Breaking S01E03 Subfolder:Season 2 Files:Breaking S02E01,Breaking S02E02,Breaking S02E03

当前结果:文件夹:Breaking Bad Subfolder:Season 1 Files:Breaking S01E01,Breaking S01E02,Breaking S01E03 Subfolder:Season 2 Files:Breaking S02E04,Breaking S02E05,Breaking S02E06

注意:我设置了日志记录,因此您可以忽略所有AddLog行 .

Option Explicit

Dim fso, oFolder, oFile, iCount, strSeries, strSeason, folder, fsoSubFolders, msg, objLogFile, strLogFile, sMsg
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(fso.GetAbsolutePathName("."))
Set fsoSubFolders = oFolder.SubFolders
strLogFile = oFolder & "\" & "Rename.log"
iCount = 0

CreateLogFile
AddLog(sMsg)

'CREATE LOG FILE ROUTINE
Sub CreateLogFile()
    Set objLogFile = Nothing
    If fso.FileExists(strLogFile) Then
        Set objLogFile = fso.GetFile(strLogFile)
        Set objLogFile = fso.OpenTextFile(strLogFile, 8, True)
        AddLog "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
        AddLog "|||||||||||||||||||||||||||||||||||||||||| NEW LOG FILE EVENT STARTED |||||||||||||||||||||||||||||||||||||||||||"
        AddLog "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
        AddLog "Preparing log file."
        AddLog "Log File found."
        AddLog "Appending to existing Log File: " & strLogFile
    Else
        Set objLogFile = fso.CreateTextFile(strLogFile)
        objLogFile.Close
        Set objLogFile = fso.OpenTextFile(strLogFile, 2, True)
        AddLog "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
        AddLog "|||||||||||||||||||||||||||||||||||||||||| NEW LOG FILE EVENT STARTED |||||||||||||||||||||||||||||||||||||||||||"
        AddLog "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
        AddLog "Preparing log file."
        AddLog "Log File found not."
        AddLog "Creating new Log File: " & strLogFile
    End If
End Sub

'APPEND TO LOG ROUTINE
Sub AddLog(sMsg)
    If Not ObjLogFile Is Nothing Then
        If sMsg = "" Then
            objLogFile.WriteLine(sMsg)
        Else
            objLogFile.WriteLine( Date & " - " & Time & ": " & "|" & sMsg)
        End If
    End If
End Sub

'SEARCH SUBFOLDERS
AddLog "||||||||||||||||||||||||||||||||||||||||||||||||| START SCRIPT ||||||||||||||||||||||||||||||||||||||||||||||||||"
For Each folder in fsoSubFolders
    AddLog " "
    AddLog "Working folder: " & folder
    msg = MsgBox("CAUTION!" & vbCrLf & vbCrLf & _
    "You are about to rename files in folder: " & folder.Name & ". " & _
    "Do you want to continue?", vbYesNo, "Renaming Service")
    If msg = vbYes Then
        GetSeries
    Else
        AddLog "Skipping folder: " & folder
    End If
Next

'GET SERIES NAME
Sub GetSeries()
    strSeries = InputBox("ENTER SERIES NAME:" & vbCrlf & "(e.g.: Breaking Bad, The Walking Dead, Firefly, etc)", "Rename Series")
    If strSeries = "" Then
        AddLog "Exiting script before complete."
        QuitApp()
    ElseIf IsBlank(strSeries) = True Then
        AddLog "ERROR: No value entered, restarting."
        MsgBox "ERROR!" & vbCrLf & vbCrLf & "Field cannot be blank. Try again."
        GetSeries
    ElseIf IsNumeric(strSeries) Then
        AddLog "ERROR: Value cannot be numeric, restarting."
        MsgBox "ERROR!" & vbCrLf & vbCrLf & "Field does not support numeric values. Try again."
        GetSeries
    Else
        AddLog "Series Title Set: " & strSeries
        GetSeason
    End If
End Sub

'GET SEASON NAME
Sub GetSeason()
    strSeason = InputBox ("ENTER SEASON:" & vbCrlf & "(e.g.: 01, 02, 03, etc)", "Rename Season")
    If strSeason = "" Then
        AddLog "Exiting script before complete."
        QuitApp()
    ElseIf IsBlank(strSeason) = True Then
        AddLog "ERROR: No value entered, restarting."
        MsgBox "ERROR!" & vbCrLf & vbCrLf & "Field cannot be blank. Try again."
        GetSeries
    ElseIf Not IsNumeric(strSeason) Then
        AddLog "ERROR: Value cannot be non-numeric, restarting."
        MsgBox "ERROR!" & vbCrLf & vbCrLf & "Field does not support non-numeric values. Try again."
        GetSeason
    Else
        AddLog "Season Title Set: " & strSeason
        AddLog "================================================================================================================="
        Rename
    End If
End Sub

'CHECK FOR BLANK VALUES
Function IsBlank(Value)
    If IsEmpty(Value) or IsNull(Value) Then
        IsBlank = True
    ElseIf IsObject(Value) Then
        If Value Is Nothing Then
            IsBlank = True
        End If
    Else
        IsBlank = False
    End If
End Function

'RENAME FILES
Sub Rename()
    For Each oFile In folder.Files
        iCount = iCount + 1
        If oFile.Name <> "Rename.vbs" Then
        AddLog "Old file name: " & oFile.Name
            If iCount = 1 Or iCount = 2 Or iCount = 3 Or iCount = 4 Or iCount = 5 Or iCount = 6 Or iCount = 7 Or iCount = 8 Or iCount = 9 Then
                oFile.Name = strSeries & " S" & strSeason & "E0" & iCount & "." & fso.GetExtensionName(oFile.Name)
                AddLog "New file name: " & oFile.Name
            Else
                oFile.Name = strSeries & " S" & strSeason & "E" & iCount & "." & fso.GetExtensionName(oFile.Name)
                AddLog "New file name: " & oFile.Name
            End If
    End If
    Next
    Set oFile = Nothing
    Set folder = Nothing
    'Set fso = Nothing
End Sub

'PREMATURE QUIT
Sub QuitApp()
    AddLog "|||||||||||||||||||||||||||||||||||||||||||||||||| END SCRIPT |||||||||||||||||||||||||||||||||||||||||||||||||||"
    WScript.Quit()
End Sub

1 回答

  • 0

    每次访问磁盘都需要文件系统对象 . 因此,只要您仍需要处理文件,就无法将其设置为空 . 所以在你的情况下,你最后只能将它设置为空 .

    然而,编号错误的原因是不同的 . Icount是一个全局变量,只在脚本开头设置为0 . 在每个季节文件夹中,剧集再次从1开始,每当您输入其中一个文件夹时,您还必须重置icount . 在重命名函数的第一行(在for之前)将其设置为0,它应该可以工作 .

    要记住的一件事是folder.files将为您提供按字母顺序排序的剧集的当前列表 . 因此,只有当剧集以格式命名时,您的重命名才有效,因此按字母顺序排序时它们的顺序正确 .

相关问题