I am using excel with Macro to accomplish this action Hope the below makes sense, as I am lost as to why it post and error and does not copy/move files
The below code I am having trouble with copy/move files to a directory
I receive an error that the "directory does not exist" I have validated the the directory is vaild with the correct spelling.
The files are names ABBCCDD - BBBBBBB - CCCCCCC.docx
The destination folder would be the first part of the file name i.e. ABBCCDD and the files should be copied / moved into that folder.
I have watched that the variables myFilePrefix, mySrc, & myDest has the correct data.
when the code gets to "DelFile = True" the "FileCopy mySrc, myDest" at that point it goes to the "No Folder routine"
The below code I am having trouble with copy/move files to a directory
I receive an error that the "directory does not exist" I have validated the the directory is vaild with the correct spelling.
The files are names ABBCCDD - BBBBBBB - CCCCCCC.docx
The destination folder would be the first part of the file name i.e. ABBCCDD and the files should be copied / moved into that folder.
I have watched that the variables myFilePrefix, mySrc, & myDest has the correct data.
when the code gets to "DelFile = True" the "FileCopy mySrc, myDest" at that point it goes to the "No Folder routine"
VBA Code:
Private Sub CommandButton1_Click()
Dim myDestDir As String
Dim myFileExt As String
Dim i As Long
Dim myFilePrefix As String
Dim myFile
Dim mySrc As String
Dim myDest As String
Dim DelFile As Boolean
' Set up an array for all the different directories you wish to copy files from
' Number in parentheses of variable declaration should be number of items in array - 1
Dim mySourceDir(1)
mySourceDir(0) = Environ$("USERPROFILE") & "\my documents\shells\" '"C:\Users\keith\Documents\shells\"
' Set source directory where subfolders are found
myDestDir = "C:\RIPS_AUTO\Mail\"
' Designate file extensions to move
myFileExt = "*.docx*"
Loop through all each directory
For i = LBound(mySourceDir) To UBound(mySourceDir)
' Loop through each Excel file in each directory
myFile = Dir(mySourceDir(i) & myFileExt)
Do While Len(myFile) > 0
' Get file prefix
myFilePrefix = Left(myFile, InStr(1, myFile, "-") - 1)
' Build source and destination references
mySrc = mySourceDir(i) & myFile
myDest = myDestDir & myFilePrefix & "\" & myFile
' Set boolean value to delete file
DelFile = True
' Copy file from source to destination
On Error GoTo No_Folder
FileCopy mySrc, myDest
On Error GoTo 0
' Delete source file, if flag is true
If DelFile = True Then Kill mySrc
' Reinitialize myFile
myFile = Dir
Loop
Next i
MsgBox "Moves complete!"
Exit Sub
No_Folder:
' If cannot find direcory for a file, do not delete, return message box, and continue
If Err.Number = 76 Then
DelFile = False
MsgBox "Folder " & myDestDir & myFilePrefix & " does not exist.", vbOKOnly, _
"Cannot move file " & mySrc & "!!!"
Err.Clear
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
Last edited by a moderator: