Script that skips files already renamed with the new extension.

已答复 Script that skips files already renamed with the new extension.

  • Wednesday, January 23, 2013 4:53 PM
     
     

    I have VBScript that will change all the files in a directory and add a .jpg extension. This works fine, but I would like it to skip files that already have a .jpg extension instead of changing all of them.

    Path = "C:\images"
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Sub visitFolder(folderVar)
        For Each fileToRename In folderVar.Files
            fileToRename.Name = fileToRename.Name & ".jpg"
        Next
        For Each folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    End Sub

    If FSO.FolderExists(Path) Then
        visitFolder(FSO.getFolder(Path))
    End If


    Rich Bessette

All Replies

  • Wednesday, January 23, 2013 4:59 PM
    Moderator
     
     Answered Has Code

    Here's how to do it quickly in PowerShell:


    get-childitem "C:\Images" -exclude *.jpg -recurse | foreach-object { rename-item $_.FullName "$($_.FullName).jpg" -whatif }

    Remove -whatif from rename-item to actually run the command.

    Bill

    • Marked As Answer by Rich Bessette Wednesday, January 23, 2013 5:57 PM
    •  
  • Wednesday, January 23, 2013 5:13 PM
     
     

    VBScript solution:

    If fileToRename.Type = "JPEG Image" Then


    ¯\_(ツ)_/¯

  • Wednesday, January 23, 2013 5:21 PM
    Moderator
     
      Has Code

    Check the extension and skip those that are not blank, something like this ...

    Path = "C:\images"
    Set FSO = CreateObject("Scripting.FileSystemObject")
      
    Sub visitFolder(folderVar)
        For Each fileToRename In folderVar.Files
          if fso.GetExtensionName(fileToRename.name) = "" then
            fileToRename.Name = fileToRename.Name & ".jpg" 
          end if
        Next
        For Each folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    End Sub
      
    If FSO.FolderExists(Path) Then
        visitFolder(FSO.getFolder(Path))
    End If


    Tom Lavedas

  • Wednesday, January 23, 2013 5:56 PM
     
     
    The PowerShell option works perfect. I'm not familiar writing these scripts, but will put it to good use. Thanks,

    Rich Bessette

  • Wednesday, January 23, 2013 6:07 PM
     
     

    The files have varous extensions, I just need it to be specific to exclude only .jpg files


    Rich Bessette

  • Wednesday, January 23, 2013 6:16 PM
     
     

    The files have varous extensions, I just need it to be specific to exclude only .jpg files


    Rich Bessette


    If fileToRename.Type = "JPEG Image" Then


    ¯\_(ツ)_/¯

  • Wednesday, January 23, 2013 6:27 PM
     
     
    Sorry, I'm not sure where this gets placed in the script.

    Rich Bessette

  • Wednesday, January 23, 2013 6:33 PM
     
     
    Sorry, I'm not sure where this gets placed in the script.

    Rich Bessette

    Not sure what you are referring to.

    The reason to use the file "Type" and not te extension is becuse JPEG files can have many different extensions; JPG, JPEG, JPE, etc.  The FSO tags all files based on the registered file type.  We can get all image files or any particular kind of image using the Type property.

    The construct with an if is basic scripting.  It will filter for all files that match teh criteria.  If you wnat to skip the files then ann Not into the code.

    If fileToRename.Type = "JPEG Image" Then



    ¯\_(ツ)_/¯

  • Wednesday, January 23, 2013 7:08 PM
     
     

    I added the IF NOT statment before the fileToRename and it just keeps adding .jpg to files already with a .jpg extension.

    Path = "C:\TEMP\testpics"
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Sub visitFolder(folderVar)
        For Each fileToRename In folderVar.Files
          If NOT fileToRename.Type = "JPEG Image" then
      fileToRename.Name = fileToRename.Name & ".jpg"
          end if
        Next
        For Each folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    End Sub
     
    If FSO.FolderExists(Path) Then
        visitFolder(FSO.getFolder(Path))
    End If


    Rich Bessette

  • Wednesday, January 23, 2013 7:25 PM
     
     

    If you are on Windows 7 or later then Microsoft changed the names.  Hopefully they will fix this.

    If NOT fileToRename.Type = "JPG File" then


    ¯\_(ツ)_/¯

  • Wednesday, January 23, 2013 7:26 PM
    Moderator
     
      Has Code

    Updated PowerShell version:


    get-childitem "C:\Images" -exclude *.jpg -recurse | 
      where-object { -not $_.PSIsContainer } | 
      foreach-object {
        $currName = $_.FullName
        if ( [IO.Path]::GetExtension($currName) -eq "" ) {
          rename-item $currName "$currName.jpg" -whatif
        }
      }
    

    Bill


  • Wednesday, January 23, 2013 7:28 PM
     
     

    I highly recommend using Bill's script if you are on Windows 7 or later.  Don't waste time learning VBScript if you are not a scripter or an admin.  The PowerShell will give you much greater rewards more quickly.


    ¯\_(ツ)_/¯

  • Wednesday, January 23, 2013 7:29 PM
     
     

    Yes the script is running on Windows 7, and changing it didn't resolve the issue.

    But I did use the "fso.GetExtensionName" successfully.

    Path = "C:\TEMP\testpics"
     Set FSO = CreateObject("Scripting.FileSystemObject")
     
     
    Sub visitFolder(folderVar)
        For Each fileToRename In folderVar.Files
          If NOT fso.GetExtensionName(fileToRename.name) = "jpg" then
            fileToRename.Name = fileToRename.Name & ".jpg"
          end if
        Next
        For Each folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    End Sub
     
    If FSO.FolderExists(Path) Then
        visitFolder(FSO.getFolder(Path))
    End If


    Rich Bessette

  • Wednesday, January 23, 2013 7:34 PM
     
     

    Is it possible that you file associations have been altered?

    The extension method will also work but may miss if jpgs are named jpeg or jpe.  Your choice.


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 1:06 AM
    Moderator
     
     

    I'd suggest one small tweak.  Add an Lcase() to the IF statement, just in case one of the files has an extension of JPG ...\

      If NOT lcase(fso.GetExtensionName(fileToRename.name)) = "jpg" then


    Tom Lavedas