Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/b7efa978f420ac9de03a to your computer and use it in GitHub Desktop.
Save antsmartian/b7efa978f420ac9de03a to your computer and use it in GitHub Desktop.
Move files from one directory to the given directory
Function MoveFiles()
Dim bob
Dim eob
'Change here if e(b)ob should be in caps
bob = "bob"
eob = "eob"
Dim directoryLocation
directoryLocation = InputBox("Give the directory location","DirectoryLocation")
Dim destinationLocation
destinationLocation = InputBox("Give the desitination Location","Destination")
Dim fileName
fileName= InputBox("File names to be moved","File Name")
'Create file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'Check the directory given is valid
exists = fso.FolderExists(directoryLocation)
Assert exists, "Please provide valid directory"
Set folder = fso.GetFolder(directoryLocation)
Set files = folder.Files
'create the folder before moving & update the location variable
destinationLocation = createFolderWithDate(fso,destinationLocation)
'Make pattern matching string
Dim movePath
movePath= directoryLocation + "\" + fileName + "*." + bob
fso.MoveFile movePath, destinationLocation
movePath= directoryLocation + "\" + fileName + "*." + eob
fso.MoveFile movePath, destinationLocation
'assert the files moved
For each folderIdx in files
If ((fso.GetExtensionName(folderIdx.name) = bob) Or (fso.GetExtensionName(folderIdx.name) = eob)) Then
Assert False, "Move failed for the file name " + folderIdx
End If
Next
'now we are good to go, return true.
MoveFiles = true
End Function
Function createFolderWithDate( fso , location )
dtmValue = Now()
'Set the date. Windows not allowing - in file names, so creating here . . .
strDate = Day(dtmValue) & "_" & Month(dtmValue) & "_" & Year(dtmValue)
'Destination path where the folder will be created
destFullPath = location+strDate
'Check if the folder Already exists
If Not fso.FolderExists(destFullPath) Then
fso.CreateFolder destFullPath
End If
'return the path of the created folder
createFolderWithDate = strDate
End Function
'Custom assert funtion which will be used to throw error @ runtime
sub Assert ( boolValue, strOnFail )
if not boolValue then
Err.Raise vbObjectError + 9999, , strOnFail
end if
end sub
MoveFiles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment