Search code examples
vbscriptfile-rename

VBS - Counting total files in directory twice


Scripters extraordinaire -

I have this script. It does the job it's supposed to do which is pull the first three characters of a file name and move them to the back of the file name. Surely there are parts that could be written better, but I'm just a beginner and this was a way I could get it to work.

What doesn't work about it is the totalFiles count. It ends up going counting the old file as 1 and the new file as 1. So for 50 files in a folder that all get renamed it says 50 of 100.

Suggestions? Thanks.

' 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

Solution

  • Use objFolder.Files.Count to get the number of files before the loop.

    Added:

    To get the number of .MP3 files only, substract the non-.MP3s in the loop from the variable then not named totalFiles but totalMP3s.