Monday, September 14, 2009

VB script to move files based on creation date

My friend had this issue where he was supposed to move thousands of files sitting in one directory to separate directories based on their creation time.  Right now this script will only check the month and year of creation.  But this can be easily extended to consider up to seconds in creation time.  Explanation for each step is given as comments itself.


'Declare all varibles in advance
Dim curPath,srcDir,dstDir,myDir,objFSO,objFolder,objFiles,objFile

'Create a file sytem object as a base for manipulating files and directories
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Process command line arguments
Select Case WScript.Arguments.Count
'If only one argument is given, take it as source directory
'then create a destination directory in the current working directory
       Case 1
       srcDir = WScript.Arguments.Item(0)
       curPath = objFSO.GetAbsolutePathName(".")
     dstDir = curPath & "\Split_Move_" & Day(Now()) & Month(Now()) & Year(Now()) & Hour(Now()) & Minute(Now()) & Second(Now())
       Case 2
'Two arguments given, first one is source directory
'Second one is destination directory
       srcDir = WScript.Arguments.Item(0)
     dstDir = WScript.Arguments.Item(1)
'In all other cases display Usage and exit script
       Case Else
       Wscript.Echo "Usage: " & Wscript.ScriptName & "<Source Directory> [<Destination Directory>]"
       WScript.Quit 1
End Select

'If source directory does not exist, display error message and exit script
If ( Not objFSO.FolderExists(srcDir) ) Then
   Wscript.Echo "Invalid Source Directory"
   WScript.Quit 1
End If

'If destination directory does not exist, create it.
If Not objFSO.FolderExists(dstDir) Then
End If

'Get list of files in source directory
Set objFolder = objFSO.getFolder( srcDir )
Set objFiles = objFolder.files

'For each file in source directory, destination should be:
'a directory under destination directory,
'that directory's name should constitute month and year of the file's creation time.
For Each objFile in objFiles
'Construct destination directory name with month and year of file's creation time
myDir = dstDir & "\" & Month(objFile.DateCreated) & "_" & Year(objFile.DateCreated)
'If directory exists, move the file there.
If objFSO.FolderExists(myDir) Then
    objFSO.MoveFile objFile.Path , myDir & "\" & objFile.Name
'Otherwise create the directory and then move the file there.
   objFSO.MoveFile objFile.Path , myDir & "\" & objFile.Name
End If

1 comment:

  1. Thank You, Thank You, Thank You!!!

    This is just what I needed! :)