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

时间:2013-05-23 14:19:39

标签: vbscript file-rename

Scripters extraordinaire -

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

不起作用的是totalFiles计数。它最终将旧文件计为1,将新文件计为1.因此对于文件夹中的所有重命名的50个文件,它表示50个为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

2 个答案:

答案 0 :(得分:0)

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

<强>加了:

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

答案 1 :(得分:-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