File Copy Method Help (1 Viewer)

graviz

Registered User.
Local time
Today, 15:58
Joined
Aug 4, 2009
Messages
167
I've been trying to search for all picture files in my documents and copy them to a folder on the desktop. I found this below and it work great for searching however I'm having trouble getting the copy to work.

http://www.ammara.com/access_image_faq/recursive_folder_search.html

Code:
 [COLOR=#000080]Dim[/COLOR] colFiles [COLOR=#000080]As New[/COLOR] Collection
    RecursiveDir colFiles, "C:\Photos", "*.jpg", [COLOR=#000080]True[/COLOR]

    [COLOR=#000080]Dim[/COLOR] vFile [COLOR=#000080]As Variant[/COLOR]
    [COLOR=#000080]For Each[/COLOR] vFile [COLOR=#000080]In[/COLOR] colFiles
        [COLOR=#000080]Debug.Print[/COLOR] vFile
    [COLOR=#000080]Next[/COLOR] vFile[/FONT]
[FONT=Courier New]

I replaced the debug.print vfile with my copy function. I've tried copyfile but it needs to specify the destination with the file name. I tried using the FSO filecopy method and can't get it to work (keep getting a compile error "Expected =") I feel the hard part is done and this part should be simple. Any ideas?
 

graviz

Registered User.
Local time
Today, 15:58
Joined
Aug 4, 2009
Messages
167
Basically all I'm trying to do is search all the directories in my documents for picture files and copy them to a single folder on my desktop. If there's an easier way of doing this besides the code I've found please let me know.
 

GSSDevelopment

PHP Guru
Local time
Today, 17:58
Joined
Dec 31, 2012
Messages
58
I tried using the FSO filecopy method and can't get it to work (keep getting a compile error "Expected =") I feel the hard part is done and this part should be simple. Any ideas?

From From dzone.com:
Code:
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'

    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function


Here's the Sub I ended up with:
Code:
    Dim colFiles As New Collection
    RecursiveDir colFiles, "C:\Users\Admin\Documents\Photobackup\", "*.jpg", True
    Dim vFile As Variant
    Dim targetDir As String
    targetDir = "C:\Users\Admin\test\"
    For Each vFile In colFiles
        Debug.Print vFile
        FileCopy vFile, targetDir & GetFilenameFromPath(vFile)
    Next vFile


Define a target directory, copy to the directory, using JUST the filename from the source.

Note: If you use parentheses after FileCopy, it's expecting that you are using the returned value, which is why it's Expecting "=". It wants you to set do something like "isSuccessful = FileCopy(source, target)"
(Disclaimer: I don't know what FileCopy wants to return, probably a boolean)

If you use a space, then it's expecting a void, so there's no returned value
 

graviz

Registered User.
Local time
Today, 15:58
Joined
Aug 4, 2009
Messages
167
From From dzone.com:
Code:
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
 
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function


Here's the Sub I ended up with:
Code:
    Dim colFiles As New Collection
    RecursiveDir colFiles, "C:\Users\Admin\Documents\Photobackup\", "*.jpg", True
    Dim vFile As Variant
    Dim targetDir As String
    targetDir = "C:\Users\Admin\test\"
    For Each vFile In colFiles
        Debug.Print vFile
        FileCopy vFile, targetDir & GetFilenameFromPath(vFile)
    Next vFile


Define a target directory, copy to the directory, using JUST the filename from the source.

Note: If you use parentheses after FileCopy, it's expecting that you are using the returned value, which is why it's Expecting "=". It wants you to set do something like "isSuccessful = FileCopy(source, target)"
(Disclaimer: I don't know what FileCopy wants to return, probably a boolean)

If you use a space, then it's expecting a void, so there's no returned value
That did the trick! Didn't think to use a get file name function.
 

Users who are viewing this thread

Top Bottom