Greetings to all and thank you for any help provided. The code below is not of my making. However, I have been able to use it as a solution to my problem. I am trying to move all the files that include "xls" from my source folder into my destination folder. When I run the macro, it is doing all that it should do, except that it is not grabbing the files that have xls in them and moving them to their destination. I will be honest, since the code is miles above my understanding, I was only able to tweak it in a limited fashion. Can any one point out what I might be leaving out for the code not to work as it should? Thank you
Option Explicit ''MUST set reference to Windows Script Host Object Model in the project using this code! Sub Copy_Files_To_New_Folder() ''This procedure will copy/move all files in a folder to another specified folder''' ''Can be easily modified Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim PathExists As Boolean Dim strSourceFolder As String, strDestFolder As String Dim x, Counter As Integer, Overwrite As String Application.ScreenUpdating = False Application.EnableEvents = False strSourceFolder = Sheets("Controls").Range("C8").Value 'Source path strDestFolder = Sheets("Controls").Range("C12").Value 'destination path 'below will verify that the specified destination path exists, or it will create it: On Error Resume Next x = GetAttr(strDestFolder) And 0 If Err = 0 Then 'if there is no error, continue below PathExists = True 'if there is no error, set flag to TRUE Overwrite = MsgBox("The folder may contain duplicate files," & vbNewLine & _ "Do you wish to overwrite existing files with same name?", vbYesNo, "Alert!") 'message to alert that you may overwrite files of the same name since folder exists If Overwrite <> vbYes Then Exit Sub 'if the user clicks YES, then exit the routine.. Else: 'if path does NOT exist, do the next steps PathExists = False 'set flag at false If PathExists = False Then MkDir (strDestFolder) 'If path does not exist, make a new one End If 'end the conditional testing For Each objFile In objFolder.Files 'for every file in the folder... 'Below: 'If statements can be used to evaluate parts of file name for file type, 'or using the InStr method below, can identify parts of a file name to conditionally 'copy files based on any part of the file name. For non-extension checks, replace 'what is inside the " " to check for that within the file name. If InStr(1, objFile.Name, ".xls") Then ' Will copy only Excel files 'If InStr(1, objFile.Name, ".txt") Then ' Will copy only Text files 'objFile.Copy strDestFolder & "\" & objFile.Name 'use the destination path string, add a / separator and the file name objFile.Move strDestFolder & "\" & objFile.Name 'Syntax for MOVING file only, remove the ' to use Counter = Counter + 1 'increment a count of files copied End If 'where conditional check, if applicable would be placed. ' Uncomment the If...End If Conditional as needed Next objFile 'go to the next file MsgBox "All " & Counter & " Files from " & vbCrLf & vbCrLf & strSourceFolder & vbNewLine & vbNewLine & _ " copied/moved to: " & vbCrLf & vbCrLf & strDestFolder, , "Completed Transfer/Copy!" 'Message to user confirming completion Set objFile = Nothing: Set objFSO = Nothing: Set objFolder = Nothing 'clear the objects Exit Sub NoFiles: 'Message to alert if Source folder has no files in it to copy MsgBox "There Are no files or documents in : " & vbNewLine & vbNewLine & _ strSourceFolder & vbNewLine & vbNewLine & "Please verify the path!", , "Alert: No Files Found!" Set objFile = Nothing: Set objFSO = Nothing: Set objFolder = Nothing 'clear the objects Application.ScreenUpdating = True 'turn screenupdating back on Application.EnableEvents = True 'turn events back on Exit Sub 'exit sub here to avoid subsequent actions ErrHandler: 'A general error message MsgBox "Error: " & Err.Number & Err.Description & vbCrLf & vbCrLf & vbCrLf & _ "Please verify that all files in the folder are not currently open," & _ "and the source directory is available" Err.Clear 'clear the error Set objFile = Nothing: Set objFSO = Nothing: Set objFolder = Nothing 'clear the objects Application.ScreenUpdating = True 'turn screenupdating back on Application.EnableEvents = True 'turn events back on End Sub
Hi
http://www.mrexcel.com/forum/showthr...light=filecopy
has codes which copy excel files from source folder to destination folder. if you want non-excel files also specify *.* instead of *.xls in the code
Ravi
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks