Results 1 to 12 of 12
  1. #1
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,420
    Thanks
    124
    Thanked 5 Times in 5 Posts

    Move zip fles , except last zip file

    I would like to move all zip files from C:\pull and the sub-folders within C:\pull to C:\old pull, except the latest zip file in each sub-folder

    For eg if C:\pull has .zip fles dated 22/06/2015, 14/02/2015, 14/03/2014 etc then 22/06/2015 must remain and the other zip files to be moved to C:\pull


    I have attached my workbook, containing the code

    It would be appreciated if someone can kindly assist me
    Attached Files Attached Files
    Last edited by HowardC; 2015-07-23 at 05:58.

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 985 Times in 915 Posts
    This is based on your code and should move everything except the most recent by Last Modified time.
    Note: I haven't tested this.

    cheers, Paul

    Code:
    Public Function BubbleSrt(ArrayIn, Optional Ascending As Boolean = True)
    
    Dim SrtTemp As Variant
    Dim i As Long
    Dim j As Long
    
    
    If Ascending = True Then
        For i = LBound(ArrayIn) To UBound(ArrayIn)
             For j = i + 1 To UBound(ArrayIn)
                 If ArrayIn(i) > ArrayIn(j) Then
                     SrtTemp = ArrayIn(j)
                     ArrayIn(j) = ArrayIn(i)
                     ArrayIn(i) = SrtTemp
                 End If
             Next j
         Next i
    Else
        For i = LBound(ArrayIn) To UBound(ArrayIn)
             For j = i + 1 To UBound(ArrayIn)
                 If ArrayIn(i) < ArrayIn(j) Then
                     SrtTemp = ArrayIn(j)
                     ArrayIn(j) = ArrayIn(i)
                     ArrayIn(i) = SrtTemp
                 End If
             Next j
         Next i
    End If
    
    BubbleSrt = ArrayIn
    
    End Function
    
    
    Sub moveFiles(zFolder As Object, filesLikeThis As String, toHere As String)
    Dim zFile As Object 'File
    Dim zFiles As Object    'Folder
    Dim iCount As Integer
    Dim zPath As String
    
    zToPath = toHere                          'e.g. "C:\Pull\
    
    zFiles = zFolder.Files
    '  We need to Dim the array once we know the number of files
    Dim aFileTS(zFolder.Count) as Integer
    iCount = 1
    For Each zFile In zFiles    'Read timestamps into array
      aFileTS(iCount) = zFile.Datelastmodified
      iCount = iCount + 1
    Next
    
    aFileTS = BubbleSrt(aFileTS, False)  'Sort timestamps descending
    
    For Each zFile In zFiles
      If zFile.Datelastmodified <> aFileTS(1) Then   'Don't move the most recent file
    	If zFile Like filesLikeThis Then
    		zSourceFile = zFolder.Path & "\" & zFile
    		zDestFile = zToPath & zFile
    		On Error Resume Next                'skip if file is open or already there
    		Kill zDestFile                      'delete file (if exists) before moving
    		Name zSourceFile As zDestFile       'move file to destination folder
    		On Error GoTo 0                     'reset error trap
    	End If
    
      Endif
    Next
    
    End Sub
    Last edited by Paul T; 2015-07-25 at 09:08. Reason: Corrected DIM aFileTS line.

  3. #3
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,420
    Thanks
    124
    Thanked 5 Times in 5 Posts
    Hi Paul

    Thanks for the help

    I have copied the function code in the sheet module and the Macro starting with Sub in the macro module

    I cannot get the macro to run. Please let me know where I have gone wrong

    See attached workbook with your code, which you kindly provided

  4. #4
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 985 Times in 915 Posts
    Just run your SubMove macro and it will do the rest, hopefully.

    cheers, Paul

  5. #5
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,420
    Thanks
    124
    Thanked 5 Times in 5 Posts
    Hi Paul

    I have inserted your code in the module, but cannot see the macro, when clicking on the run button

    Please check attached workbook
    Attached Files Attached Files

  6. #6
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 985 Times in 915 Posts
    Sorry, I assumed you had more of a handle on VBA. My bad!

    All I've done is modify the "moveFiles" macro and use "BubbleSrt" to find the latest file. I've not allowed for pulling in the parameters from the spreadsheet or running the macro via the button. I also spotted a mistake in one of the DIM statements and corrected it above - you'll need to copy the code in again.
    I have not checked any of your code to be sure it works, but the copy routine looks iffy (Name zSourceFile As zDestFile).

    I don't have Office any more so I can't test it.

    To run the macro you need to run "moveFiles" and supply the required arguments, e.g. 'moveFiles("C:\Old Pull Files\" as FSO, *.zip*, "C:\Pull\")'.
    Note: You have "zFolder As Object" so you need to have instantiated it as a FileSystemObject before calling the macro.

    Hopefully one of our very good VBA programmers will help you out further.

    cheers, Paul

  7. #7
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,826
    Thanks
    136
    Thanked 482 Times in 459 Posts
    Hi Howard

    I need to create some dummy zip files, then patch their date-timestamps, then create some test folders and subfolders to put them in, all this before I can test the code to do what you want.
    This creation of extra test files takes a little time, so please be patient.

    zeddy
    •Corporate Compensation Complaints Controller
    .

  8. #8
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,826
    Thanks
    136
    Thanked 482 Times in 459 Posts
    Hi Howard

    I've attached a file which will move matching files into a specified destination folder.
    As per your request, this will leave the most recent matching file in the source folder/subfolders.

    Instead of using arrays and bubble sorts, I just looped through the folder to find the latest matching file.
    I then looped through again to move matching files that had an earlier dateTimestamp.

    The code is documented.
    I ran a quick test OK.

    zeddy
    •Renewables Gift Administration

  9. The Following User Says Thank You to zeddy For This Useful Post:

    HowardC (2015-07-27)

  10. #9
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,826
    Thanks
    136
    Thanked 482 Times in 459 Posts
    Hi Paul

    Name zSourceFile As zDestFile
    Is there are more efficient way?

    zeddy
    •Coconut Husky Team Driver
    .

  11. #10
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,420
    Thanks
    124
    Thanked 5 Times in 5 Posts
    Hi Zeddy

    Thanks very much for all your effort.

    I have tested the macro and it works perfectly


    Howard

  12. #11
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 985 Times in 915 Posts
    CHTD, after a closer look I see the first sub to run is " moveFilesDemo", which instantiates zFolder - I can't see which macro is linked to the button in LibreOffice. Then calling the move files sub saves multiple folder lookups, so it seems about right, although converting zFile from the file object to the name is not a good move IMO. You'd be better off doing something like "zFile.move (zDestFile)".

    cheers, Paul

  13. The Following User Says Thank You to Paul T For This Useful Post:

    zeddy (2015-07-27)

  14. #12
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,826
    Thanks
    136
    Thanked 482 Times in 459 Posts
    Hi Paul

    ..good spot on zFile - I changed it to zFilename in updated version attached.
    Your BubbleSrt is a useful method worth knowing.

    zeddy
    •Python Repair Depot
    .

Posting Permissions

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