I have the following macro below to move files in C:\temp to C:\old temp.
If C:\old temp does not exist, I need this folder created
It would be appreciated if someone could kindly amend my code to accommodate this
If C:\old temp does not exist, I need this folder created
It would be appreciated if someone could kindly amend my code to accommodate this
Code:
Sub moveFiles()
zFromPath = "C:\temp\" '<< specify source folder here
zToPath = "C:\old Temp\" '<< specify destination folder here
zExt = Array("*.csv", "*.xls*") '<< define file extensions for files to be moved
For Each x In zExt 'process each specified file extension
zFile = Dir(zFromPath & x) 'get filename in source folder
Do While zFile <> "" And (zFile <> ThisWorkbook.Name) 'ignore THIS file
zFetch = zFromPath & zFile 'source file to be moved
On Error Resume Next 'skip if file is open or already there
Kill (zToPath & zFile) 'delete file (if exists) before moving
Name zFetch As (zToPath & zFile) 'move file to destination folder
On Error GoTo 0 'reset error trap
zFile = Dir 'fetch next matching file in source folder
Loop 'continue processing matching files
Next 'process next file extension type
'**************************************
'CHECK FOR ANY FILES NOT MOVED..
'**************************************
For Each x In zExt 'process each specified file extension
zFound = zCountFiles(zFromPath, x)
zCount = zCount + zFound
Next
If zCount > 0 Then
saywhat = saywhat & zCount
saywhat = saywhat & " file(s) were not moved because they are currently open" & vbCr
saywhat = saywhat & "or copies are already in the destination folder." & vbCr
saywhat = saywhat & vbCr & vbCr
End If
'**************************************
'DISPLAY COMPLETION MESSAGE..
'**************************************
saywhat = saywhat & "You can find the files from:" & vbCr
saywhat = saywhat & zFromPath & vbCr & vbCr
saywhat = saywhat & "in folder location:" & vbCr
saywhat = saywhat & zToPath & vbCr
MsgBox saywhat
End Sub
Function zCountFiles(zFolder, zExt)
zPath = zFolder
If Right(zPath, 1) <> "\" Then zPath = zPath & "\"
zFile = Dir(zPath & zExt)
Do While zFile <> "" And (zFile <> ThisWorkbook.Name) 'ignore THIS file
zCountFiles = zCountFiles + 1
zFile = Dir 'fetch next matching file in source folder
Loop 'continue processing matching files
End Function