Results 1 to 14 of 14
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Error using FSO.CopyFile


    In the segment of code below, two sets of statements are copying files from one folder to another. The first set from strFldrGAP1 are copied verbatim, while the second set from strFldrGAP2 have their file names prefixed with a "1". The code is stopped in debug where noted. If the code is continued, error number 5 is raised with the description: "Invalid procedure call or argument". Does anyone see what the issue might be?
    Code:
    Kill (StrFldrPath & "\PlayList\*.*")      'Empty old playlist
    
    
    '*=*=*=(Copy 1st half of GAP to PlayList)=*=*=*
    strFrom = StrFldrPath & "\" & strFldrGAP1 & "\*.*"
    strTO = StrFldrPath & "\playlist\"
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Call FSO.CopyFile(strFrom, strTO)
    
    
    '*=*=*=(Now the 2nd half)=*=*=*
    intnT = Left(strPLOV, 2)       'How many tracks in the playlist?
    intTn = Right(strPLOV, 2)      'What's the first track?
    
    
    For I = intTn To intnT
        strNamePrfx = Right("00" & I, 2)
        strFrom = StrFldrPath & "\" & strFldrGAP2 & "\" & strNamePrfx & "*.*"
        strNamePrfx = "1" & strNamePrfx
        strTO = StrFldrPath & "\playlist\" & strNamePrfx & "*.*"
        Call FSO.CopyFile(strFrom, strTO)         <<<<<<<<<< Breakpoint is here
    Next I
    
    
    Set FSO = Nothing
    
    
    End Function
    And looking at the argument values in the IW:
    Code:
    ? strfrom
    D:\MCatWorks\CD-0268\01*.*
    ? strTo
    D:\MCatWorks\playlist\101*.*

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    destination Required. Character string destination where the file or files from source are to be copied. Wildcard characters are not allowed.
    https://docs.microsoft.com/en-us/off...opyfile-method

    Since your using fso cant you use a For Each loop rather than a For next loop?
    Along the lines of:

    Code:
    Dim fol As Folder    
        Dim fil As File
        Dim fso As FileSystemObject
    
        Set fso = New FileSystemObject
    
        Set fol = fso.GetFolder(strFolder)
    
        For Each fil In fol.Files
    
    'your  code here
    
        Next
    You could then use the fil.name property to construct your file names.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    The "For" loop is simply because only a subset of the source folder is being copied. ("For I = intTn To intnT", read number of files starting at file number. All files are serialized 01, 02, 03......n). From what you're saying, I would need to use the Dir function to find the first file of interest and go from there?

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Here's an example which will delete all the files in the desination and then copy from the source folder to the destination folder.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I already do that in my code where I copy ALL the files. The 2nd portion of the code has to deal with copying a subset of its source and modifying the file name in the process.

    Code:
    Kill (StrFldrPath & "\PlayList\*.*")      'Empty old playlist
    
    
    '*=*=*=(Copy 1st half of GAP to PlayList)=*=*=*
    strFrom = StrFldrPath & "\" & strFldrGAP1 & "\*.*"
    strTO = StrFldrPath & "\playlist\"
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Call FSO.CopyFile(strFrom, strTO)

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Quote Originally Posted by GraeagleBill View Post
    The "For" loop is simply because only a subset of the source folder is being copied. ("For I = intTn To intnT", read number of files starting at file number. All files are serialized 01, 02, 03......n). From what you're saying, I would need to use the Dir function to find the first file of interest and go from there?
    That wasn't clear from your posted code. I wondered what strPLOV was. You should post complete code.

    How are you selecting your files to copy?
    How many are you copying?
    Will they always be in sequence "07", "08", "09", "10", "11", etc? Or will there be gaps? ("07,"09","10")
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    reading the partial code you posted I think you want to do the following:

    start with a file which has a 2 digit number as a prefix.
    get n number of files that follow the starting file in sequence.
    copy the file to a new folder and append a 1 to the begining of the file name.
    09SomeFile.txt becomes 109SomeFile.txt, etc

    Here's another example which uses fso and a dictionary object.
    Attached Files Attached Files
    Last edited by moke123; 09-12-2021 at 05:12 AM.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Why no debug.prints to see what you actually have, instead of what you think you have?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Quote Originally Posted by Welshgasman View Post
    Why no debug.prints to see what you actually have, instead of what you think you have?
    Good point. Would make the error glaringly obvious.
    The problem remains that you cant use wildcards in the destination path, especially with a name change.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    See screenshot of IM in post #1

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Moke123,
    I've attempted to mimic your use of FSO from attachment in #7. Obviously, I still don't have things right, witness screenshot collage of debug revelations below:
    Click image for larger version. 

Name:	000.jpg 
Views:	8 
Size:	130.9 KB 
ID:	46199
    I'm also posting the entire piece of code involved that shows my changes plus the comment narrative of the function in the hopes it will help clear up the intent.
    Code:
    Private Function PlayGap()
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  When a user wants to play the works of a GAP concert, the request is initiated by
    '  right-clicking on the performance date and choosing Play Works or Cast Works.
    '  In either case, our "calling code" has already identified the volumes that contain
    '  the mp3/mp4 files that will comprise the playlist.  Our task here is simple, we
    '  copy the 1st half files to a temporary folder named "Playlist" and then append the
    '  2nd half files from their original source folder adding 100 to the leading file number.
    '  The PLOV value we need to identify the 2nd half files is already set in strPLOV.
    '
    '  PLOV of the form "nn/ss" and defines the subset of files we need to copy from the 2nd
    '  half folder, "strFldrGAP2.  ALL files numerically sequenced, i.e., "01name1.mp4";
    '  "02name2.mp4";"03name3.mp4"......."99name99.mp4".  '  Example: PLOV = "04/05" defines
    '  the subset of 4 files beginning with file number 5.  We need to essentially add 100
    '  to the prefix values of the copy, lest we collide with files already copied into the
    '  playlist folder  for the 1st half of the GAP.  For purposes of the temporary playlist,
    '  we'll not need the file names, only the sequence numbers as names.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim FSO As New FileSystemObject
    Dim FileName As File
    Dim FldrName As folder
    
    
    Dim strFrom As String
    Dim strTO As String
    Dim strNamePrfx As String
    
    
    Kill (StrFldrPath & "\PlayList\*.*")      'Empty old playlist
    
    
    '*=*=*=(Copy 1st half of GAP to PlayList)=*=*=*
    strFrom = StrFldrPath & "\" & strFldrGAP1 & "\*.*"
    strTO = StrFldrPath & "\playlist\"
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Call FSO.CopyFile(strFrom, strTO)
    
    
    '*=*=*=(Now the 2nd half)=*=*=*=*=*=*=*=*=*=*=*=*
    intnT = Left(strPLOV, 2)       'How many tracks in the playlist?
    intTn = Right(strPLOV, 2)      'What's the first track?
    
    
    Set FldrName = FSO.GetFolder(StrFldrPath & "\" & strFldrGAP2)
    
    
    strFrom = StrFldrPath & "\" & strFldrGAP2 & "\"
    strTO = StrFldrPath & "\playlist\1"
    
    
    J = intTn    'First file of subset bares this prefix
    K = intnT    'Then sequentially "intnT" many files
    
    
    For Each FileName In FldrName
        If Left(FileName.Name, 2) = Right("0" & J, 2) Then
            FSO.CopyFile strFrom & FileName.Name, strTO & FileName.Name
            J = J + 1
            K = K - 1
            If K = 0 Then Exit For
        End If
    Next FileName
    
    
    Set FSO = Nothing
    
    
    End Function

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Not really following your logic of naming conventions but this line needs correcting

    Code:
    For Each FileName In FldrName
    should be
    Code:
    For Each FileName In FldrName.Files
    Also if your testing the right hand side of the file name wont that always be .mp4?

    You need to use fso.GetBaseName() method to test the filename.
    fso.GetBaseName returns the file name without the extension.

    https://docs.microsoft.com/en-us/off...asename-method
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  13. #13
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Also if your testing the right hand side of the file name wont that always be .mp4?
    True, some of the folders might have mp3 audio files. The only guarantee is that it will always be one or the other.

    And yes, the naming had me scratching my head a few times. This app is my first encounter with Dim'g FSO related objects. I even had to figure out what References I needed to add. Anyway, I'll give further thought to your choices of "fol" and "fil".

    Thanks for all your help. Your sample DB's were really helpful.
    Bill

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Fol and Fil are just variables for Folder and File without using a reserved word.

    You could probably change Right("0" & J, 2) to Format(J, "00")

    Filesystemobjects are great and get easier as you use them.
    Dictionary Objects are really helpful too.

    To explain how I used the dictionary in my code:

    Code:
    Sub CopySomeFiles(intStart As Integer, IntEnd As Integer)
    'intStart and intEnd are just integers passed in. In your case the first 2 numbers of the filename
    
    Dim strSource As String
    Dim strDest As String
    
    'These are the source and destination folder paths
    strSource = CurrentProject.Path & "\SourceF\"
    strDest = CurrentProject.Path & "\DestinationF\"
    
    ' variables
        Dim fol As Folder  
        Dim fil As File
        Dim NewName As String
     
        Dim dict As New Scripting.Dictionary   'instantiates a dictionary named "dict"
        
        Dim fso As New FileSystemObject       'instantiates an FSO object
        
            Set fol = fso.GetFolder(strDest)     'set the fso folder object = to the destination folder
    
    
        For Each fil In fol.Files   ' for each file in the folder's file collection
            fil.Delete                  'delete the file
        Next
        
        Set fol = fso.GetFolder(strSource)      'set the fso folder object = to the Source folder
    
    
    'A dictionary has 2 values, A key and an Item.
    'A Key must be unique or it errors
    'An Item can be almost anything
    'dictionaries have some methods that collections and arrays dont have
    
    'In this section I am iterating over all the files in your source folder.
    'I am adding the left 2 characters of the file name as the KEY value. (ie. 01,02,03,etc)
    'I am adding the filename as the ITEM value to dictionary 
        For Each fil In fol.Files
             dict.Add Left(fil.Name, 2), fil.Name
                    ' can also be written like this
                    ' dict.Add Key:=Left(fil.Name, 2), Item:=fil.Name  
        Next
    
        Dim i As Integer
        Dim k As String
    
    
    
    'To access an item , in your case the file name, in the dictionary you basically use the Key to specify the Item to use (ie. dict(KEY) or dict(03) 
    
        For i = intStart To IntEnd
    
            k = Format(i, "00")    ' formating i as 2 digits in the variable k
    
            If dict.Exists(k) Then   'using the dictionaries EXISTS method to test and make sure it is in the dictionary  (ie. Is there a key which matches "03" for instance)
    
                NewName = "1" & dict(k) ' if the key exists I'm setting the variable NewName to "1" plus the file name (ie. 103Somefile.mp4)
    
                fso.CopyFile strSource & dict(k), strDest & NewName  'use fso.CopyFile  where I  concatenate the source and destination paths to the file names
    
            End If
    
           'Debug.Print NewName
    
    
        Next i
    
    
    End Sub
    Some Links:
    https://docs.microsoft.com/en-us/off...tionary-object
    https://docs.microsoft.com/en-us/off...mobject-object
    https://analystcave.com/vba-filesyst...-fso-in-excel/
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need help with fso.copyfile
    By NightWalker in forum Programming
    Replies: 8
    Last Post: 10-18-2016, 10:26 AM
  2. why use timer for copyfile?
    By vicsaccess in forum Programming
    Replies: 7
    Last Post: 04-05-2016, 12:39 PM
  3. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  4. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  5. Replies: 6
    Last Post: 05-30-2012, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums