Rename Photo's and AVI's

Author Message
mbouchard

  • Total Posts : 2110
  • Scores: 29
  • Reward points : 0
  • Joined: 5/15/2003
  • Location: USA
  • Status: offline
Rename Photo's and AVI's Saturday, January 19, 2008 4:43 PM (permalink)
4
A while ago I created a script that would rename a file (photo) based on the date last modified, I posted it here so if you would like to see that one, search for Move and Rename pictures and you should find it.

One of the issues with this is if I connect my camera directly to my PC the datelastmodified can get screwed up.  Had it happen once and it was a PITA to fix it.

Not too long ago, someone posted a script or a link to a script that read exif data and I started the basics then.  Well, tonight I got a bug up my.... and decided to work on the script.  I started with NameSpace and with the help of DM to get past a strange issue (thanks again DM_4ever), I was able to get a working script.  The only issue I saw was when dealing with AVI files.  My camera, and from what I read it seems to be standard, creates the avi file but then creates a THM file.  This file is essentially a jpg and if you rename it to JPG you get exif data.  But maybe I was just tired or whatever but with the way the original script was written I was having difficulty getting this working.  So, I started searching for some EXIF command line tools and found jhead.exe.  I started playing with this and one of the features that it has is if you give it a THM file it will rename it to JPG, get the exif data and rename the AVI file based on that info.

This is what I have so far:
My folder structure is
D:\NewPictures\Tools
D:\NewPictures\Backup (used while the script is in dev)
The script and jhead.exe are both in the tools folder

 
 'Todo 
 ' Convert current rename to rename using exif 
 
 
 ' NOTE: Exif does not contain Seconds.  CHECK for file before creating.  Add suffix if needed. 
 ' Build in support for files that do not have exif info 
 ' Keep THM file for video's as this will contain exif info 
 ' Rename THM to JPG to get EXIF date taken info? 
 '    Date Modified on THM MAY be different than date modified on movie file 
 ' 
 Option Explicit  
 
 Dim strFolder 
 Dim IgnoredExtensions 
 Dim ie 
 Dim ret  
 Dim scriptFolder  
 Dim f, f1, fc, myArray,fileExtension,newFolder,strNewFile 
 Dim picCount : picCount = 0 
 Dim aviCount : aviCount = 0 
 Dim folderCount : folderCount = 0 
 Dim skipCount : skipCount = 0 
 
 Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") 
 Dim wshShell : Set WshShell = WScript.CreateObject("WScript.Shell") 
 
 scriptFolder = fso.GetParentFolderName(WScript.ScriptFullName) 
 
 'Check if ignoredExtensions file is present.  This will be used to list extensions that do not need to be processed 
 If fso.FileExists(scriptFolder & "\IgnoredExtensions.txt") Then 
     Set ie = fso.OpenTextFile(scriptFolder & "\IgnoredExtensions.txt", 1) 
     IgnoredExtensions = ie.ReadAll 
     ie.Close 
 Else 
     IgnoredExtensions = "" 
 End If  
 
 'Path is Parent of Tools folder i.e.  
 ' ScriptFolder = d:\NewPictures\Tools 
 ' strFolder default = d:\newPictures 
 strFolder = InputBox("enter in path to folder","Source",fso.GetParentFolderName(scriptFolder)) 
 If strFolder = "" Then  
     WScript.Echo "Cancel" 
     WScript.Quit 
 End If  
 
 'Set current folder to inputbox as this is where the files will be processed.  needed for jhead to function correctly as files need to be in same folder as exe 
 WshShell.CurrentDirectory = strFolder 
 
 'While in testing, create backup of all files. 
 ret = MsgBox("This script is still in test.  Do you want to create a backup?",vbYesNo) 
 If ret = vbYes Then 
     Call doBackup  
 End If  
 
 'Rename all JPG's to MM_DD_YYY_HHMMSS format (_ can be changed as needed) 
 WshShell.Run "cmd /c " & scriptFolder & "\jhead -nf%m_%d_%Y_%H%M%S *.jpg -v >" & scriptFolder & "\jpg.txt",0,True   
 
 'Rename all AVI's and THM's to MM_DD_YYY_HHMMSS format.  All THM's will be renamed to JPG 
 WshShell.Run "cmd /c " & scriptFolder & "\jhead -a -nf%m_%d_%Y_%H%M%S *.THM -v >" & scriptFolder & "\avi.txt",0,True  
 
 Set f = fso.GetFolder(strFolder) 
 Set fc = f.Files 
 
 For Each f1 in fc 
      strNewFile = fnDate(f1.name)'set filename to be Month_DD_YYYY_HHMMSS format 
      fileExtension = fso.GetExtensionName(lcase(strNewFile)) 
      If fileExtension = "jpg" Or fileExtension = "bmp" then 
         picCount = picCount + 1'increment picCount(only for show) 
         Call CopyFiles(f1.Path,strNewFile) 
     ElseIf fileExtension = "avi" Then  
         aviCount = aviCount + 1'increment aviCount(only for show) 
         Call CopyFiles(f1.Path,strNewFile) 
     ElseIf lcase(strNewFile) = "thumbs.db" Then 'Delete thumbs.db if present 
         f1.attributes = f1.attributes - 6 
         fso.DeleteFile(f1.path) 
     Else 
         If InStr(IgnoredExtensions,fileExtension) = 0 Then 
             ret = Msgbox(f1.Name & " is an unknown extension.  Do you want to add it to the Ignored Extensions file?",vbYesNo) 
             If ret = vbYes Then 
                 Call UpdateIgnoredExtensions(fileExtension) 
             End If  
         End If  
     End If  
 Next 
 
 MsgBox picCount & " pictures have been processed" & vbcr _ 
 & aviCount & " movies have been processed" & vbCr _  
 & folderCount & " folders have been created" & vbCr _  
 & skipCount & " files have been skipped"   
   
 Sub DoBackup() 
     Dim backupCount : backupCount = 0 
     Set f = fso.GetFolder(strFolder) 
     Set fc = f.Files 
      
     For Each f1 in fc 
          strNewFile = f1.name 
          fileExtension = fso.GetExtensionName(lcase(strNewFile)) 
          If Not (fileExtension = "jpg" Or fileExtension = "bmp" Or fileExtension = "avi") Then  
                 'DoNothing 
         End If  
     backupCount = backupCount + 1     
     fso.CopyFile f1.Path,strFolder & "\backup\" & f1.Name  
     Next 
     MsgBox backupCount & " files have been backuped" 
 End Sub 
   
 Sub CopyFiles(originalFile,theFile) 
     Dim cf 
     myArray = Split(theFile,"_") 
     'Set newfolder to equal first 3 parts of the File which will be 
     ' Month(spelled out) DD and YYYY 
     'ex January_01_2008 
     newFolder = strFolder & "\" & myArray(0) & "_" & myArray(1) & "_" & myArray(2) 
 
     If Not fso.FolderExists(newFolder) Then  
         folderCount = folderCount + 1'increment folderCount(only for show) 
         Set cf = fso.CreateFolder(newFolder) ' Create the folder as needed 
     End If 
     If Not fso.FileExists(newFolder & "\" & theFile) Then  
         'Move file to new folder renaming it in the process 
         fso.MoveFile originalFile,newFolder & "\" & theFile 
     Else 
         skipCount = skipCount + 1'increment skipCount(only for show) 
     End If  
 End Sub 
 
 
 Function FnDate(Dt) 
 'Replace MM with Month 
 ' i.e. 01 becomes January 
     Dim tFnDate,Rnm 
     tFnDate = Left(dt,2) 
 
     Select Case tFnDate 
         Case "01" Rnm = "January" 
         Case "02" Rnm = "February" 
         Case "03" Rnm = "March" 
         Case "04" Rnm = "April" 
         Case "05" Rnm = "May" 
         Case "06" Rnm = "June" 
         Case "07" Rnm = "July" 
         Case "08" Rnm = "August" 
         Case "09" Rnm = "September" 
         Case "10" Rnm = "October" 
         Case "11" Rnm = "November" 
         Case "12" Rnm = "December" 
     End Select 
      
     FnDate =  Rnm & "_" & Mid(dt,4,len(dt)) 
 End Function 
 
 Sub UpdateIgnoredExtensions(theExtension) 
 'Update IgnoredExtensions 
     Dim s 
     Set s = fso.OpenTextFile(scriptFolder & "\IgnoredExtensions.txt", 8,True)'ForAppending 
     s.WriteLine(theExtension) 
     s.Close 
 End Sub  
 
 


Thoughts?  suggestions?

Well off to watch TV with the wife.  Night all.
<message edited by mbouchard on Saturday, October 31, 2009 1:38 PM>
Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.
 
#1
    mbouchard

    • Total Posts : 2110
    • Scores: 29
    • Reward points : 0
    • Joined: 5/15/2003
    • Location: USA
    • Status: offline
    Re:Rename Photo's and AVI's Saturday, October 31, 2009 1:37 PM (permalink)
    0
    Wow, over a year since I have worked on this script.  Have been using it for a while but recently started to have issues with the AVI rename.  If I remove the SD card from my camera and move the files off using a card reader, I get a THM file along with the AVI file.  THM files are essentially jpgs (you can just rename them as needed to JPG) that contain exif info for the avi file.  Well, if I just connect my camera to my PC and copy the files over, the THM file is not present, not sure why though.

    Well, what was happening was my avi files were coming out as _4001.avi, _4002.avi etc.  And I would then have to figure out what to rename them and do so manually. 

    Today I took a look at the properties on the AVI file and noticed a Media Created Date, not sure if it has always been there or if it is new with Windows 7, so be careful using this on a windows vista/XP box.

    So, I knew the info was there, just had to figure out how to get it out.  Then I came across a script I wrote a long time ago to get info on jpgs using namespace, did some editing, expanded the number of loops a couple times and low an behold found it.

    Here is the script in case anyone is interested
    Dim strFolder 
     Dim oShell 
     Dim fso  
     Dim objFolder 
     Dim i 
     
     strFolder = "M:\NewPictures\TestRename"'\TestRename\January_12_2008" 
     
     Set fso = CreateObject("Scripting.FileSystemObject") 
     Set oShell = CreateObject("Shell.Application") 
     Set objFolder = oShell.Namespace(strFolder) 
     
     Set f = fso.GetFolder(strFolder) 
     Set fc = f.Files 
     For Each f1 in fc 
     strFileName = f1.Name 
         If fso.GetExtensionName(strFilename) = "AVI" Then      
             for i = 0 to 200 
                 Wscript.Echo strFileName & vbTab & objFolder.GetDetailsOf(objFolder.Parsename(strFileName),i) 
             Next  
              
         End If  
     Next 
     


    With that info in hand, here is my updated move/rename script.  I still use jhead for Jpg's, but am now using file info for AVI's.  Do have a lot of cleaning to do, but meh, not feeling it right now.

    'Todo 
     ' Convert current rename (jhead) to rename using exif 
     ' NOTE: Exif does not contain Seconds.  CHECK for file before creating.  Add suffix if needed. 
     ' Build in support for files that do not have exif info 
     ' Clean up script big time 
     '  
     
     option Explicit  
     Dim strFolder 
     Dim IgnoredExtensions 
     Dim ie 
     Dim ret  
     Dim scriptFolder  
     Dim f, f1, fc, myArray,fileExtension,newFolder,strNewFile 
     Dim picCount : picCount = 0 
     Dim aviCount : aviCount = 0 
     Dim folderCount : folderCount = 0 
     Dim skipCount : skipCount = 0 
     Dim objFolder 
     Dim strFilename,strMediaCreatedDate 
     Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") 
     Dim wshShell : Set WshShell = WScript.CreateObject("WScript.Shell") 
     Dim oShell : Set oShell = CreateObject("Shell.Application") 
     
     scriptFolder = fso.GetParentFolderName(WScript.ScriptFullName) 
     
     'Check if ignoredExtensions file is present.  This will be used to list extensions that do not need to be processed 
     If fso.FileExists(scriptFolder & "\IgnoredExtensions.txt") Then 
         Set ie = fso.OpenTextFile(scriptFolder & "\IgnoredExtensions.txt", 1) 
         IgnoredExtensions = ie.ReadAll 
         ie.Close 
     Else 
         IgnoredExtensions = "" 
     End If  
     
     'Path is Parent of Tools folder i.e.  
     ' ScriptFolder = d:\NewPictures\Tools 
     ' strFolder default = d:\newPictures 
     strFolder = InputBox("enter in path to folder","Source",fso.GetParentFolderName(scriptFolder)) 
     Set objFolder = oShell.Namespace(strFolder) 
     If strFolder = "" Then  
         WScript.Echo "Cancel" 
         WScript.Quit 
     End If  
     
     'Set current folder to inputbox as this is where the files will be processed.  needed for jhead to function correctly as files need to be in same folder as exe 
     WshShell.CurrentDirectory = strFolder 
     
     'While in testing, create backup of all files. 
     ret = MsgBox("This script is still in test.  Do you want to create a backup?",vbYesNo) 
     If ret = vbYes Then 
         Call doBackup  
     End If  
     
     'Rename all AVI's and THM's to MM_DD_YYY_HHMMSS format.  All THM's will be renamed to JPG 
     'set objFolder = wshShell.Namespace(strFolder) 
     
     Set f = fso.GetFolder(strFolder) 
     Set fc = f.Files 
     For Each f1 in fc 
     strFileName = f1.Name 
         If fso.GetExtensionName(ucase(strFilename)) = "AVI" Then      
             'Set strmediacreateddate based on info taken from properties of the file, 190 is the media Created Date 
             strMediaCreatedDate = objFolder.GetDetailsOf(objFolder.Parsename(strFileName),190) 
             If Len(strMediaCreatedDate) = 0 Then'If data is not present, skip file 
                 Wscript.Echo strFilename & " - was Skipped, Unknown Media Created Date" 
                 skipCount = skipCount + 1'increment skipCount(only for show) 
             Else 
                 'Rename file and leave in current folder 
                 fso.MoveFile f1.Path, strFolder & "\" & CleanDate(strMediaCreatedDate) & ".avi" 'File name comes back as Month_DD_YYY_HHMM Seconds not needed as 2 videos will not be taken in the same second 
             End If  
         End if  
     Next 
     
     'Rename all JPG's to MM_DD_YYY_HHMMSS format (_ can be changed as needed) 
     WshShell.Run "cmd /c " & scriptFolder & "\jhead -nf%m_%d_%Y_%H%M%S *.jpg -v >" & scriptFolder & "\jpg.txt",0,True   
     'msgbox "Pause" 'Uncomment out pause to see the the files name before they are moved. 
     
     Set f = fso.GetFolder(strFolder) 
     Set fc = f.Files 
     
     For Each f1 in fc 
         If fso.GetExtensionName(ucase(f1.Name)) = "AVI" Then  
             strNewFile = f1.Name'If AVI file, does not need to go through fndate function again doing so would turn October_31_2009 into _ober_31_2009 
          Else 
             strNewFile = fnDate(f1.name)'set filename to be Month_DD_YYYY_HHMMSS format 
          End if  
          
         fileExtension = fso.GetExtensionName(lcase(strNewFile)) 
           
         If fileExtension = "jpg" Or fileExtension = "bmp" then 
             picCount = picCount + 1'increment picCount(only for show) 
             Call CopyFiles(f1.Path,strNewFile) 
         ElseIf Lcase(fileExtension) = "avi" Then  
             aviCount = aviCount + 1'increment aviCount(only for show) 
             Call CopyFiles(f1.Path,strNewFile) 
         ElseIf lcase(strNewFile) = "thumbs.db" Then 'Delete thumbs.db if present 
             f1.attributes = f1.attributes - 6 
             fso.DeleteFile(f1.path) 
         Else 
             If InStr(IgnoredExtensions,fileExtension) = 0 Then 
                 ret = Msgbox(f1.Name & " is an unknown extension.  Do you want to add it to the Ignored Extensions file?",vbYesNo) 
                 If ret = vbYes Then 
                     Call UpdateIgnoredExtensions(fileExtension) 
                 End If  
             End If  
         End If  
     Next 
     
     MsgBox picCount & " pictures have been processed" & vbcr _ 
     & aviCount & " movies have been processed" & vbCr _  
     & folderCount & " folders have been created" & vbCr _  
     & skipCount & " files have been skipped"   
       
     Sub DoBackup() 
         Dim backupCount : backupCount = 0 
         Set f = fso.GetFolder(strFolder) 
         Set fc = f.Files 
          
         For Each f1 in fc 
              strNewFile = f1.name 
              fileExtension = fso.GetExtensionName(lcase(strNewFile)) 
              If Not (fileExtension = "jpg" Or fileExtension = "bmp" Or fileExtension = "avi") Then  
                     'DoNothing 
             Else 
                 backupCount = backupCount + 1     
                 fso.CopyFile f1.Path,strFolder & "\backup\" & f1.Name  
             End If  
         Next 
         MsgBox backupCount & " files have been backuped" 
     End Sub 
       
     Sub CopyFiles(originalFile,theFile) 
         Dim cf 
         myArray = Split(theFile,"_") 
         'Set newfolder to equal first 3 parts of the File which will be 
         ' Month(spelled out) DD and YYYY 
         'ex January_01_2008 
         newFolder = strFolder & "\" & myArray(0) & "_" & myArray(1) & "_" & myArray(2) 
     
         If Not fso.FolderExists(newFolder) Then  
             folderCount = folderCount + 1'increment folderCount(only for show) 
             Set cf = fso.CreateFolder(newFolder) ' Create the folder as needed 
         End If 
         If Not fso.FileExists(newFolder & "\" & theFile) Then  
             'Move file to new folder renaming it in the process 
             fso.MoveFile originalFile,newFolder & "\" & theFile 
         Else 
             Wscript.Echo theFile & " - was Skipped" 
             skipCount = skipCount + 1'increment skipCount(only for show) 
         End If  
     End Sub 
     
     Function CleanDate(strDate) 
         Dim RegEx : Set RegEx = New RegExp 
         RegEx.Pattern = "[^\w\s\/:]" ' not a word (A-Z, a-z, 0-9, _) or colon 
         RegEx.IgnoreCase = True 
         RegEx.Global = True 
         If fso.GetExtensionName(ucase(f1.Name)) = "AVI" then  
             CleanDate = FnDateAVI(RegEx.Replace(strDate, "")) 
         Else 
             CleanDate = FnDate(RegEx.Replace(strDate, ""))     
         End if  
     End Function 
     
     Function FnDateAVI(Dt) 
         Dim tFnDate,Rnm 
         tFnDate = FnN(Month(Dt), 2) 
     
         Select Case tFnDate 
             Case "01" Rnm = "January" 
             Case "02" Rnm = "February" 
             Case "03" Rnm = "March" 
             Case "04" Rnm = "April" 
             Case "05" Rnm = "May" 
             Case "06" Rnm = "June" 
             Case "07" Rnm = "July" 
             Case "08" Rnm = "August" 
             Case "09" Rnm = "September" 
             Case "10" Rnm = "October" 
             Case "11" Rnm = "November" 
             Case "12" Rnm = "December" 
         End Select 
         FnDateAVI =  rNm & "_" & FnN(Day(Dt), 2) & "_" & FnN(Year(Dt), 4) & "_" & Replace(FormatDateTime(dt,vbShortTime),":","") 
     End Function 
     
     Function FnN(V, N) 
      FnN = Right(String(N,"0") & V, N) 
     End Function 
     
     Function FnDate(Dt) 
     'Replace MM with Month 
     ' i.e. 01 becomes January 
         Dim tFnDate,Rnm 
         tFnDate = Left(dt,2) 
     
         Select Case tFnDate 
             Case "01" Rnm = "January" 
             Case "02" Rnm = "February" 
             Case "03" Rnm = "March" 
             Case "04" Rnm = "April" 
             Case "05" Rnm = "May" 
             Case "06" Rnm = "June" 
             Case "07" Rnm = "July" 
             Case "08" Rnm = "August" 
             Case "09" Rnm = "September" 
             Case "10" Rnm = "October" 
             Case "11" Rnm = "November" 
             Case "12" Rnm = "December" 
         End Select 
          
         FnDate =  Rnm & "_" & Mid(dt,4,len(dt)) 
     End Function 
     
     Sub UpdateIgnoredExtensions(theExtension) 
     'Update IgnoredExtensions 
         Dim s 
         Set s = fso.OpenTextFile(scriptFolder & "\IgnoredExtensions.txt", 8,True)'ForAppending 
         s.WriteLine(theExtension) 
         s.Close 
     End Sub  
     


    Mike

    For useful Scripting links see the Read Me First stickey!

    Always remember Search is your friend.
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9