+ Reply to Thread
Results 1 to 2 of 2

Thread: Trying to move all xls files from one folder to another

  1. #1
    Forum Contributor
    Join Date
    05-06-2004
    Posts
    115

    Trying to move all xls files from one folder to another

    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

  2. #2
    Valued Forum Contributor
    Join Date
    02-27-2008
    Posts
    753

    macro

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Move file to folder based on name
    By Slomaro2000 in forum Excel Programming
    Replies: 26
    Last Post: 10-31-2008, 07:57 AM
  2. How to access folder in vba to impot files?
    By annir in forum Excel Programming
    Replies: 4
    Last Post: 07-07-2008, 07:55 PM
  3. Move Excel files to an Outlook folder
    By ninja0059 in forum Excel Programming
    Replies: 3
    Last Post: 02-22-2008, 01:18 AM
  4. Move a cell which in turn moves a folder?
    By icedtc in forum Excel Programming
    Replies: 5
    Last Post: 09-28-2007, 03:31 PM
  5. Creating a folder and copying files to it
    By emmamaki in forum Excel Programming
    Replies: 6
    Last Post: 01-28-2007, 04:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0