Monday, June 15, 2009

Common Sense: Bulding Script file to move/merge picture folders

I have pictures on various hard drives that I've gotten from various
cameras and phones. Many of them were retrieved with a program that saved them with a prefix of my choice and added a sequential number, for example FL VAC 001. I moved them into various different folders and at times retrieved the same pictures various times. I placed the pictures into sub folders by themes.

Objective is to be able to merge pictures from various folders on a SD or USB drive to similar folders on the hard drive. If the folder exists on the hard drive then I want to move the files to that folder, but I do not want to replace a file with the same name. I will want to rename the file so it is unique. Once the pictures are all moved to the same hard drive and grouped by themes, then I will be able locate the pictures in one area.

How do I apply common sense when creating a VB script? First thing is to write down what needs to be done - in any order at this point. Initially just write your thoughts on a piece of paper like the image on the right. I then translate that to an electronic document (as was done below). I use that document as my specifications to building the script file.

VBScript: MovePicScript.vbs

Script Initialization Section
Set To/From Constants - future prompt for the locations
Set indicator (Yes/No, True/False) to retain files after coping
Set constant of image types (.jpg.png.mov.mpg)
Set type variable to indicate if file is a video or a picture.
Set numeric variables to keep count of: Optional
Total folders in from drive
Total image files in the from drive
Total video files in the from drive
Total new image files moved
Total new video files moved

Script Main Processing Section
Initialize variables: set all numeric variables to 0.
Validate the to and from root path exists
Perform the processing to transverse the from folders and move the picture image files to the correct folder in the To folders.
Display summary when completed.

Script Procedures Section
(identify the procedures needed)
  • Function GetNbrFolders passing from path return total number of folders in folder tree
  • Function GetNbrFiles passing from path and mode (what file types to count) return total number of files in folder tree (will only count the file types based on the mode.
  • Sub TransverseFolders passing from path and mode (what action will be done in each folder; i.e. Pic only, Video only, pic and Video etc.)
  • Function Validatefolder passing from path and True/False to create folder return to path
  • Function ValidateFileName passing From File Name return To File Name
  • Sub MoveFile passing from path, from file (to path will be from ValidateFolder and to file name will be from ValidateFileName function)
  • Function Shell returning the Shell Object
  • Function FileType passing from file returning type variable (Pic, Video)
Review Design: (Did we think of all of the procedures needed?)
Manually set the to and from path and mode constants then run the script.
Get a count of folders and files in the from path, display them in a message box.
Starting at the from path transverse down all the folders and in each from folder that has the desired file types perform the move file function.
The movefile file function will take the from path and from file name and determine the to path and to file name then move the file.

Next Steps: write the code for each procedure then the code for the main processing section.
Look for my MovePicScript post as I build the script using a common sense approach.