VBS - 计算目录中的总文件两次

问题描述 投票:0回答:2

Scripters extraordinaire -

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

什么不起作用的是totalFiles计数。它最终将旧文件计为1,将新文件计为1.因此对于文件夹中的所有重命名的50个文件,它表示50个中的50个。

建议?谢谢。

' 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
vbscript file-rename
2个回答
0
投票

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

添加:

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


-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
© www.soinside.com 2019 - 2024. All rights reserved.