0

优秀的编剧——

我有这个脚本。它完成了它应该做的工作,即提取文件名的前三个字符并将它们移动到文件名的后面。当然有些部分可以写得更好,但我只是一个初学者,这是我可以让它工作的一种方式。

不起作用的是 totalFiles 计数。它最终将旧文件计为 1,将新文件计为 1。因此,对于一个文件夹中的 50 个文件都被重命名,它表示 50 of 100。

建议?谢谢。

' Renames MP3s in C:\Directory to move the 3 digit channel number to the end of the filename.
' If the new file name already exists, it is skipped. 
' If the file has already been renamed, it is skipped.



Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Directory"
Dim sChannel, sRtFile, sNewFile, intCount, totalFiles
intCount = 0
totalFiles =0

Set objFolder = objFSO.GetFolder(objStartFolder)


Set colFiles = objFolder.Files

For Each objFile in colFiles
totalFiles = totalFiles + 1
    If UCase(objFSO.GetExtensionName(objFile.name)) = "MP3" Then


        sOldFile = objFSO.GetBaseName(objFile)
        sChannel = Left(sOldFile, Len(sOldFile) - 14)
        sRtFile = Right(sOldFile, Len(sOldFile) - 3)
        sNewFile = sRtFile + sChannel
        'Wscript.Echo sChannel
        If sChannel = "001" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="002" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If  
        If sChannel="003" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="004" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="005" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="006" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="007" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If
        If sChannel="008" Then
            sNewFilePath = objStartFolder + "\" + sNewFile + ".mp3"
            If objFSO.FileExists(sNewFilePath) Then
            Else
            objFSO.MoveFile objFile.Path, sNewFilePath
            intCount = intCount + 1
            End If
        End If



    End If

Next


If intCount < totalFiles Then
    Wscript.Echo intCount & " of " & totalFiles & " files renamed." & vbCrLf & "Some files appear to have already been renamed."
Else
    Wscript.Echo intCount & " of " & totalFiles & " files renamed."
End If
4

2 回答 2

0

用于objFolder.Files.Count获取循环前的文件数。

添加:

要仅获取 .MP3 文件的数量,请从变量中减去循环中的非 .MP3,然后不命名为 totalFiles,而是命名为 totalMP3s。

于 2013-05-23T14:32:24.337 回答
-1
'Program to count no of .vbs files in a specified folder

Dim count,looping,extension
count = 0
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder("C:\APRIL\Assignments\Second")
Set totalFiles = objFolder.Files
For Each looping  In totalFiles

extension = objFso.GetExtensionName(looping)
    if(extension = "vbs") Then
    count = count + 1
    End If
Next
MsgBox count
于 2019-04-18T06:46:53.847 回答