• Archive Files (VB6)

    Author
    Topic
    #400145

    I’ve been working on the archiving section of my application.
    With trial and error, I’ve lost a few files, and wondered if anyone had any idea why my code doesn’t work.
    I know I’m pretty close:

    Private Sub CmdArchiveJPG_Click()
    Dim SF As String ' Source Folder
    Dim AF As String ' Archive Folder
    Dim N As Integer ' Number
    Dim FN As String ' FileName
    Dim DP As String ' Default Path
    SF = "L:MMPDFImage"
    AF = "L:MMPDFArchive"
    For N = 0 To lstFiles.ListCount - 1
    If lstFiles.Selected(N) Then
    FN = lstFiles.List(N)
    DF = Left$(FN, Len(FN) - 3) & "IMA"
    X = Shell("c:pkwarepkzip.exe -a " & AF & DF & " " & SF & FN, vbMinimizedNoFocus)
    'Set fso = New FileSystemObject
    Debug.Print lstFiles.List(N)
    fso.DeleteFile SF & lstFiles.List(N)
    End If
    Next N
    MsgBox "Files Archived", , "Information"
    End Sub
    Viewing 3 reply threads
    Author
    Replies
    • #778417

      Could it be the problem with long file names you mentioned in an earlier thread? If not, which files do you lose, can you discover a pattern?

    • #778418

      Could it be the problem with long file names you mentioned in an earlier thread? If not, which files do you lose, can you discover a pattern?

    • #778537

      It appears you are defining a collection and then copying the first file to a zip file, deleting the file and then looking for the next item in the collection. This is problematic as the collection has just shrunk by one item.

      Can you reverse the loop direction so that the last file is attacked first?
      For N = lstFiles.ListCount – 1 To 0 Step -1

      Alternatively, I would have a look at the switches used in the shelled zip command. I would expect you can move the file rather than copy it if you found the right switch. I use Winzip and the command line component called wzzip which has a lot of flexibility in copying files to a zip archive.

      • #778676

        Thanks Andrew

        Hans
        I sorted the long file name issue and selectively chose the files to archive for testing purposes only.
        But what Andrew says kind of makes sense, the list decreases each time the next (N) has to be found.
        I think I will probably split the code to complete one operation first, then attempt to complete the second task after.

        Something like ( Forgive mistakes, on the fly ):::

        Dim SF As String ' Source Folder
        Dim AF As String ' Archive Folder
        Dim N As Integer ' Number
        Dim FN As String ' FileName
        Dim DP As String ' Default Path
        SF = "L:MMPDFImage"
        AF = "L:MMPDFArchive"
        For N = 0 To lstFiles.ListCount - 1
        If lstFiles.Selected(N) Then
        FN = lstFiles.List(N)
        DF = Left$(FN, Len(FN) - 3) & "IMA"
        X = Shell("c:pkwarepkzip.exe -a " & AF & DF & " " & SF & FN, vbMinimizedNoFocus)
        End If
        Next N
        End Sub
        For N = 0 To lstFiles.ListCount - 1 To 0 Step -1
        If lstFiles.Selected(N) Then
        FN = lstFiles.List(N)
        Set fso = New FileSystemObject
        fso.DeleteFile SF & lstFiles.List(N)
        End If
        Next N
        MsgBox "Files Archived", , "Information"
        End Sub

        I will have to test it first and see what happens, comments are welcome.

        • #778685

          Andrew has probably hit the nail on the head. I think your code will work, but I can’t test it because I don’t have pkzip (and have no need for it at the moment.)

          Note: don’t create a new FileSystemObject within the loop – you’ll end up creating many instances. Do it once before entering the loop, then set the object to Nothing after the loop.

          • #778699

            Like this ?

            Set fso = New FileSystemObject
            For N = 0 To lstFiles.ListCount – 1 To 0 Step -1
            If lstFiles.Selected(N)
            ThenFN = lstFiles.List(N)
            fso.DeleteFile SF & lstFiles.List(N)
            End If
            Next N
            MsgBox “Files Archived”, , “Information”
            Set fso = Nothing
            End Sub

            • #778701

              yep

            • #778709

              wink

            • #779217

              With the above code, is there a alternative switch to PKZIP which will zip all files selected into one file as opposed to individual as my does.
              I think I may have to move the shell statement out of the loop, but fear if I do this, only one of the files will be zipped ?

            • #779242

              You would have to assemble the command line string in the loop, then call it after the loop. Something like:


              Dim strCommand As String
              ‘ Change to the source drive and folder
              ‘ So that we don’t have to specify the path each time
              ChDrive “L”
              ChDir SF
              ‘ First part of command line
              strCommand = “c:pkwarepkzip.exe -a ” & AF & “Archive.zip”
              ‘ Loop through list box items
              For N = 0 To lstFiles.ListCount – 1
              If lstFiles.Selected(N) Then
              ‘ Append file name to command line
              strCommand = strCommand & ” ” & lstFiles.List(N)
              End If
              ‘ Run command line
              X = Shell(strCommand, vbMinimizedNoFocus)
              Next N

              Notes:
              1. Replace “Archive.zip” by the appropriate name (it shouldn’t be the same as the name of a .jpg, since you are zipping several into one file)
              2. If many .jpg’s have been selected, you may run into a problem, since a command line has a maximum length of 127 characters.

            • #779260

              Hans
              Since each Image has an ext of 01, 02 etc ie: 12345-01.jpg, 12345-02.jpg, I’m sure I can use the MkDir command to make a
              folder of the same first 5 Characters.

              I’m just adding code as we speak to create the named folder before running the shell command.
              That way I think there are sufficient switches with PKZIP to archive into the named folder.

              Using the following, I can extract the initial file name to create the folder :

              DF = Left$(FN, Len(FN) - 7)

              Then each file copied won’t be duplicated.

              I’ll let you know how I go on.

            • #779261

              Hans
              Since each Image has an ext of 01, 02 etc ie: 12345-01.jpg, 12345-02.jpg, I’m sure I can use the MkDir command to make a
              folder of the same first 5 Characters.

              I’m just adding code as we speak to create the named folder before running the shell command.
              That way I think there are sufficient switches with PKZIP to archive into the named folder.

              Using the following, I can extract the initial file name to create the folder :

              DF = Left$(FN, Len(FN) - 7)

              Then each file copied won’t be duplicated.

              I’ll let you know how I go on.

            • #779285

              Close mad We need a smiley for frustration !! (I’ll get it soon)

              Private Sub CmdArchiveJPG_Click()
              Dim SF As String ' Source Folder
              Dim AF As String ' Archive Folder
              Dim N As Integer ' Number
              Dim i As Integer ' Number
              Dim FN As String ' FileName
              Dim FNA As String 'FileName Archived
              Dim DP As String ' Default Path
              Dim strFolderName As String
              
              
              SF = "L:MMPDFImage"
              AF = "L:MMPDFArchive"
              
              Set fso = New FileSystemObject
              
              For N = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(N) Then
              FN = lstFiles.List(N)
              
              strFolderName = Left$(FN, Len(FN) - 7)
              If Dir(strFolderName, vbDirectory) = "" Then
              MsgBox "Creating Folder To Archive" & " " & FN & " Files"
              MkDir strFolderName
              End If
              
              DF = Left$(FN, Len(FN) - 3) & "IMA"
              x = Shell("c:pkwarepkzip.exe -a  " & strFolderName & DF & " " & SF & FN, vbHide)
              End If
              Next N
              
              For i = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(i) Then
              FNA = lstFiles.List(i)
              'fso.DeleteFile SF & FNA
              Debug.Print SF & FNA
              End If
              Next i
              MsgBox "Files Archived", , "Information"
              Set fso = Nothing
              End Sub
            • #779311

              What is the question? smile

              You have placed the creation of a new folder and the Shell to pkzip inside the loop again, so this will be done for each file separately. I thought you wanted to zip all selected images into one zip file?

            • #779325

              Yes you’re right again Hans, I also omitted the AF within the create folder section.

              strFolderName = AF & Left$(FN, Len(FN) – 7)

              I thought it would be correct to insert within the loop because FN isn’t created until items from the list had been selected ie:

              For N = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(N) Then
              FN = lstFiles.List(N)
              
              strFolderName = AF & Left$(FN, Len(FN) - 7)
              If Dir(strFolderName, vbDirectory) = "" Then
              MsgBox "Creating Folder To Archive" & " " & FN & " Files"
              MkDir strFolderName
              End If

              As it stands at the moment, it does create the folder but copies no files into it (thats something I suppose !)

              I did originally say to archive multiple files into one zip file, but thinking a little further, If I zip all the imagesinto one zip,
              I still have to zip the PDF file along with it from a different command.
              Putting them all in one folder makes better sense.

              Ideally from one command button, I would archive files from L:MMPDFImage & L:MMPDFEstPDF where both job No’s match.
              But for now, One step at a time.

            • #779333

              I think there is a backslash missing in strFolderName, try

              strFolderName = AF & Left$(FN, Len(FN) – 7) & “”

              This doesn’t matter when you create the folder with MkDir, but you need it when concatenating strFolderName & DF in the command line for pkzip.

            • #779339

              Ok That sorted that.
              I’ll look into comparing filenames now.

              Thanks again for the help, sorry to be a pain, but it’s all in the learning process.

            • #779340

              Ok That sorted that.
              I’ll look into comparing filenames now.

              Thanks again for the help, sorry to be a pain, but it’s all in the learning process.

            • #788105

              I’m still losing files and I can’t for the life of me find why !
              If I select 10 files to archive, I end up with 8 in the archive folder.
              I must have looked at this code umpteen times, debug.Print, step thru etc.

              Can anyone see why I am losing files ??

              I have remmed out the fso.DeleteFile and the Kill statement, it doesn’t matter which I use, I still lose files.
              Should I lose the end :
              For i = lstFiles.ListCount – 1 To 0 Step -1
              If lstFiles.Selected(i) Then

              and keep within the items already selected ?

              VB:
              --------------------------------------------------------------------------------
              Private Sub CmdArchiveJPG_Click()
                  Dim SF As String ' Source Folder
                  Dim AF As String ' Archive Folder
                  Dim N As Integer ' Number
                  Dim i As Integer ' Number
                  Dim FN As String ' FileName
                  Dim FNA As String 'FileName Archived
                  Dim DP As String ' Default Path
                  Dim strFolderName As String
                  
                  
                  SF = "L:MMPDFImage"
                  AF = "L:MMPDFArchive"
                  
                  Set fso = New FileSystemObject
                  Me.MousePointer = vbHourglass
                  For N = 0 To lstFiles.ListCount - 1
                      If lstFiles.Selected(N) Then
                          FN = lstFiles.List(N)
                      
                          strFolderName = AF & Left$(FN, 5) & ""
                      
                          If Dir(strFolderName, vbDirectory) = "" Then
                              MkDir strFolderName
                          End If
                      
                          DF = Left$(FN, Len(FN) - 3) & "IMA"
                          X = Shell("c:pkwarepkzip.exe -u  " & strFolderName & DF & " " & SF & FN, vbMaximizedFocus)
                      End If
                  Next N
                  
                  For i = lstFiles.ListCount - 1 To 0 Step -1
                      If lstFiles.Selected(i) Then
                          FNA = lstFiles.List(i)
                          'fso.DeleteFile SF & FNA
                          'Kill SF & FNA
                      End If
                  Next i
                  Me.MousePointer = vbDefault
                  
                  MsgBox "Files Archived", vbInformation, "Information"
                  Set fso = Nothing
              End Sub
            • #788113

              The last loop (with i as loop counter) is executed after the entire first loop (with N as loop counter) has finished. The first loop creates pkzip files in the archive folder, the second one deletes files from the source folder, whose path is different, so it is impossible that the second loop deletes files from the archive folder. The problem must be in the first loop.

              Try the following to get a log of what happens:

              After Set fso = New FileSystemObject, insert the following lines:

              Dim ts As Scripting.TextStream
              Set ts = fso.CreateTextFile(FileName:=”C:DWLog.txt”, OverWrite:=True)

              Temporarily comment out the line beginning with X = Shell(… and insert the following lines to replace it:

              Dim strCommand As String
              strCommand = “c:pkwarepkzip.exe -u ” & strFolderName & DF & ” ” & SF & FN
              X = Shell(strCommand, vbMaximizedFocus)
              ts.WriteLine strCommand

              Above the line Set fso = Nothing, insert the following lines:

              ts.Close
              Set ts = Nothing

              After performing an archive run, you should have a text file DWLog.txt in C: (of course, you can change the location and filename if you like), with one line for each call to PKZip. Check if there is a line for each selected file, and check the file names very carefully. Does anything catch your eye?

            • #788115

              Thanks Hans I will look at this over the weekend.

              One thing though, and I am completely to blame, I have not added “Option Explicit” in the form header, which has thrown up a
              couple of errors.

              Have a good weekend. cheers

            • #788119

              It may not have to do with the immediate problem, but in general, you should have Option Explicit in all modules, and try to find and repair the cause of the error messages. Not having Option Explicit can lead to unexpected bahavior of your code, and hard to trace errors.

              Have a good weekend too.

            • #788623

              Hans
              The log seems to be fine ?
              The folders are correct in the Archive folder ?

              is this what it should read like ?

              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1631916319.EST L:MMPDFTEMPEstPDF16319.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632016320.EST L:MMPDFTEMPEstPDF16320.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632016320a.EST L:MMPDFTEMPEstPDF16320a.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632216322.EST L:MMPDFTEMPEstPDF16322.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632416324.EST L:MMPDFTEMPEstPDF16324.pdf

              ( I changed teh folders to MMPDFTEMP for testing purposes)

              I’ve also read something about waiting for each shell execution to finish before proceeding to the next shell.
              Could this be the case here ?

            • #788655

              Dave,

              This is what the log file should look like: a copy of each command line executed. If this is the complete log, pkzip.exe was executed 5 times.
              The crucial questions are:
              – Were there exactly 5 selected items in the list box?
              – Are the 5 archive files (*.EST) actually created?

              The Shell function doesn’t wait until the command is finished. It might be that firing a series of pkzip command lines causes some of them to fail. I have attached a variation on Shell, shamelessly ripped off from http://www.mentalis.org/apilist/apilist.php%5B/url%5D. You can import the code into a standard module. You can then use the SuperShell function instead of Shell:

              X = SuperShell(“c:pkwarepkzip.exe -u ” & strFolderName & DF & ” ” & SF & FN, strFolderName, INFINITE, SW_NORMAL, HIGH_PRIORITY_CLASS)

              SuperShell will wait until pkzip has finished.

            • #788674

              Hans

              Something strange, using pkzip thru command line ? ( without updating to your API)

              Periodically pkzip hitches and cannot create an archive file and returns:

              PkZip [E01] L:/MMPDF/........................

              Notice the wrong direction of the directory slashes !!

            • #788676

              I wouldn’t worry about the direction of the slashes. It may be just the way PkZip is programmed to report errors. Can you detect any pattern in the failures to create the archive file, for example in the file names?

            • #788680

              No pattern errors at all.

              One I thought would throw up an error was 17362.1.pdf , saved by one of our estimators (notice two stops),
              but archived successfully ?

              Hans
              I’ll do some more tesing and see what happens, I’ve been writing down each file to be archived
              and checking after the archive, it just seems one file every now and again will cause the error, but no clear pattern with the
              file or filename.
              The files throwing the error still load ok with the pdf software ?

              Thanks

            • #788681

              No pattern errors at all.

              One I thought would throw up an error was 17362.1.pdf , saved by one of our estimators (notice two stops),
              but archived successfully ?

              Hans
              I’ll do some more tesing and see what happens, I’ve been writing down each file to be archived
              and checking after the archive, it just seems one file every now and again will cause the error, but no clear pattern with the
              file or filename.
              The files throwing the error still load ok with the pdf software ?

              Thanks

            • #788677

              I wouldn’t worry about the direction of the slashes. It may be just the way PkZip is programmed to report errors. Can you detect any pattern in the failures to create the archive file, for example in the file names?

            • #788675

              Hans

              Something strange, using pkzip thru command line ? ( without updating to your API)

              Periodically pkzip hitches and cannot create an archive file and returns:

              PkZip [E01] L:/MMPDF/........................

              Notice the wrong direction of the directory slashes !!

            • #788656

              Dave,

              This is what the log file should look like: a copy of each command line executed. If this is the complete log, pkzip.exe was executed 5 times.
              The crucial questions are:
              – Were there exactly 5 selected items in the list box?
              – Are the 5 archive files (*.EST) actually created?

              The Shell function doesn’t wait until the command is finished. It might be that firing a series of pkzip command lines causes some of them to fail. I have attached a variation on Shell, shamelessly ripped off from http://www.mentalis.org/apilist/apilist.php%5B/url%5D. You can import the code into a standard module. You can then use the SuperShell function instead of Shell:

              X = SuperShell(“c:pkwarepkzip.exe -u ” & strFolderName & DF & ” ” & SF & FN, strFolderName, INFINITE, SW_NORMAL, HIGH_PRIORITY_CLASS)

              SuperShell will wait until pkzip has finished.

            • #788624

              Hans
              The log seems to be fine ?
              The folders are correct in the Archive folder ?

              is this what it should read like ?

              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1631916319.EST L:MMPDFTEMPEstPDF16319.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632016320.EST L:MMPDFTEMPEstPDF16320.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632016320a.EST L:MMPDFTEMPEstPDF16320a.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632216322.EST L:MMPDFTEMPEstPDF16322.pdf
              c:pkwarepkzip.exe -u L:MMPDFTEMPArchive1632416324.EST L:MMPDFTEMPEstPDF16324.pdf

              ( I changed teh folders to MMPDFTEMP for testing purposes)

              I’ve also read something about waiting for each shell execution to finish before proceeding to the next shell.
              Could this be the case here ?

            • #788120

              It may not have to do with the immediate problem, but in general, you should have Option Explicit in all modules, and try to find and repair the cause of the error messages. Not having Option Explicit can lead to unexpected bahavior of your code, and hard to trace errors.

              Have a good weekend too.

            • #788116

              Thanks Hans I will look at this over the weekend.

              One thing though, and I am completely to blame, I have not added “Option Explicit” in the form header, which has thrown up a
              couple of errors.

              Have a good weekend. cheers

            • #788114

              The last loop (with i as loop counter) is executed after the entire first loop (with N as loop counter) has finished. The first loop creates pkzip files in the archive folder, the second one deletes files from the source folder, whose path is different, so it is impossible that the second loop deletes files from the archive folder. The problem must be in the first loop.

              Try the following to get a log of what happens:

              After Set fso = New FileSystemObject, insert the following lines:

              Dim ts As Scripting.TextStream
              Set ts = fso.CreateTextFile(FileName:=”C:DWLog.txt”, OverWrite:=True)

              Temporarily comment out the line beginning with X = Shell(… and insert the following lines to replace it:

              Dim strCommand As String
              strCommand = “c:pkwarepkzip.exe -u ” & strFolderName & DF & ” ” & SF & FN
              X = Shell(strCommand, vbMaximizedFocus)
              ts.WriteLine strCommand

              Above the line Set fso = Nothing, insert the following lines:

              ts.Close
              Set ts = Nothing

              After performing an archive run, you should have a text file DWLog.txt in C: (of course, you can change the location and filename if you like), with one line for each call to PKZip. Check if there is a line for each selected file, and check the file names very carefully. Does anything catch your eye?

            • #788106

              I’m still losing files and I can’t for the life of me find why !
              If I select 10 files to archive, I end up with 8 in the archive folder.
              I must have looked at this code umpteen times, debug.Print, step thru etc.

              Can anyone see why I am losing files ??

              I have remmed out the fso.DeleteFile and the Kill statement, it doesn’t matter which I use, I still lose files.
              Should I lose the end :
              For i = lstFiles.ListCount – 1 To 0 Step -1
              If lstFiles.Selected(i) Then

              and keep within the items already selected ?

              VB:
              --------------------------------------------------------------------------------
              Private Sub CmdArchiveJPG_Click()
                  Dim SF As String ' Source Folder
                  Dim AF As String ' Archive Folder
                  Dim N As Integer ' Number
                  Dim i As Integer ' Number
                  Dim FN As String ' FileName
                  Dim FNA As String 'FileName Archived
                  Dim DP As String ' Default Path
                  Dim strFolderName As String
                  
                  
                  SF = "L:MMPDFImage"
                  AF = "L:MMPDFArchive"
                  
                  Set fso = New FileSystemObject
                  Me.MousePointer = vbHourglass
                  For N = 0 To lstFiles.ListCount - 1
                      If lstFiles.Selected(N) Then
                          FN = lstFiles.List(N)
                      
                          strFolderName = AF & Left$(FN, 5) & ""
                      
                          If Dir(strFolderName, vbDirectory) = "" Then
                              MkDir strFolderName
                          End If
                      
                          DF = Left$(FN, Len(FN) - 3) & "IMA"
                          X = Shell("c:pkwarepkzip.exe -u  " & strFolderName & DF & " " & SF & FN, vbMaximizedFocus)
                      End If
                  Next N
                  
                  For i = lstFiles.ListCount - 1 To 0 Step -1
                      If lstFiles.Selected(i) Then
                          FNA = lstFiles.List(i)
                          'fso.DeleteFile SF & FNA
                          'Kill SF & FNA
                      End If
                  Next i
                  Me.MousePointer = vbDefault
                  
                  MsgBox "Files Archived", vbInformation, "Information"
                  Set fso = Nothing
              End Sub
            • #779334

              I think there is a backslash missing in strFolderName, try

              strFolderName = AF & Left$(FN, Len(FN) – 7) & “”

              This doesn’t matter when you create the folder with MkDir, but you need it when concatenating strFolderName & DF in the command line for pkzip.

            • #779326

              Yes you’re right again Hans, I also omitted the AF within the create folder section.

              strFolderName = AF & Left$(FN, Len(FN) – 7)

              I thought it would be correct to insert within the loop because FN isn’t created until items from the list had been selected ie:

              For N = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(N) Then
              FN = lstFiles.List(N)
              
              strFolderName = AF & Left$(FN, Len(FN) - 7)
              If Dir(strFolderName, vbDirectory) = "" Then
              MsgBox "Creating Folder To Archive" & " " & FN & " Files"
              MkDir strFolderName
              End If

              As it stands at the moment, it does create the folder but copies no files into it (thats something I suppose !)

              I did originally say to archive multiple files into one zip file, but thinking a little further, If I zip all the imagesinto one zip,
              I still have to zip the PDF file along with it from a different command.
              Putting them all in one folder makes better sense.

              Ideally from one command button, I would archive files from L:MMPDFImage & L:MMPDFEstPDF where both job No’s match.
              But for now, One step at a time.

            • #779312

              What is the question? smile

              You have placed the creation of a new folder and the Shell to pkzip inside the loop again, so this will be done for each file separately. I thought you wanted to zip all selected images into one zip file?

            • #779286

              Close mad We need a smiley for frustration !! (I’ll get it soon)

              Private Sub CmdArchiveJPG_Click()
              Dim SF As String ' Source Folder
              Dim AF As String ' Archive Folder
              Dim N As Integer ' Number
              Dim i As Integer ' Number
              Dim FN As String ' FileName
              Dim FNA As String 'FileName Archived
              Dim DP As String ' Default Path
              Dim strFolderName As String
              
              
              SF = "L:MMPDFImage"
              AF = "L:MMPDFArchive"
              
              Set fso = New FileSystemObject
              
              For N = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(N) Then
              FN = lstFiles.List(N)
              
              strFolderName = Left$(FN, Len(FN) - 7)
              If Dir(strFolderName, vbDirectory) = "" Then
              MsgBox "Creating Folder To Archive" & " " & FN & " Files"
              MkDir strFolderName
              End If
              
              DF = Left$(FN, Len(FN) - 3) & "IMA"
              x = Shell("c:pkwarepkzip.exe -a  " & strFolderName & DF & " " & SF & FN, vbHide)
              End If
              Next N
              
              For i = lstFiles.ListCount - 1 To 0 Step -1
              If lstFiles.Selected(i) Then
              FNA = lstFiles.List(i)
              'fso.DeleteFile SF & FNA
              Debug.Print SF & FNA
              End If
              Next i
              MsgBox "Files Archived", , "Information"
              Set fso = Nothing
              End Sub
            • #779243

              You would have to assemble the command line string in the loop, then call it after the loop. Something like:


              Dim strCommand As String
              ‘ Change to the source drive and folder
              ‘ So that we don’t have to specify the path each time
              ChDrive “L”
              ChDir SF
              ‘ First part of command line
              strCommand = “c:pkwarepkzip.exe -a ” & AF & “Archive.zip”
              ‘ Loop through list box items
              For N = 0 To lstFiles.ListCount – 1
              If lstFiles.Selected(N) Then
              ‘ Append file name to command line
              strCommand = strCommand & ” ” & lstFiles.List(N)
              End If
              ‘ Run command line
              X = Shell(strCommand, vbMinimizedNoFocus)
              Next N

              Notes:
              1. Replace “Archive.zip” by the appropriate name (it shouldn’t be the same as the name of a .jpg, since you are zipping several into one file)
              2. If many .jpg’s have been selected, you may run into a problem, since a command line has a maximum length of 127 characters.

            • #779218

              With the above code, is there a alternative switch to PKZIP which will zip all files selected into one file as opposed to individual as my does.
              I think I may have to move the shell statement out of the loop, but fear if I do this, only one of the files will be zipped ?

            • #778710

              wink

            • #778702

              yep

          • #778700

            Like this ?

            Set fso = New FileSystemObject
            For N = 0 To lstFiles.ListCount – 1 To 0 Step -1
            If lstFiles.Selected(N)
            ThenFN = lstFiles.List(N)
            fso.DeleteFile SF & lstFiles.List(N)
            End If
            Next N
            MsgBox “Files Archived”, , “Information”
            Set fso = Nothing
            End Sub

      • #778677

        Thanks Andrew

        Hans
        I sorted the long file name issue and selectively chose the files to archive for testing purposes only.
        But what Andrew says kind of makes sense, the list decreases each time the next (N) has to be found.
        I think I will probably split the code to complete one operation first, then attempt to complete the second task after.

        Something like ( Forgive mistakes, on the fly ):::

        Dim SF As String ' Source Folder
        Dim AF As String ' Archive Folder
        Dim N As Integer ' Number
        Dim FN As String ' FileName
        Dim DP As String ' Default Path
        SF = "L:MMPDFImage"
        AF = "L:MMPDFArchive"
        For N = 0 To lstFiles.ListCount - 1
        If lstFiles.Selected(N) Then
        FN = lstFiles.List(N)
        DF = Left$(FN, Len(FN) - 3) & "IMA"
        X = Shell("c:pkwarepkzip.exe -a " & AF & DF & " " & SF & FN, vbMinimizedNoFocus)
        End If
        Next N
        End Sub
        For N = 0 To lstFiles.ListCount - 1 To 0 Step -1
        If lstFiles.Selected(N) Then
        FN = lstFiles.List(N)
        Set fso = New FileSystemObject
        fso.DeleteFile SF & lstFiles.List(N)
        End If
        Next N
        MsgBox "Files Archived", , "Information"
        End Sub

        I will have to test it first and see what happens, comments are welcome.

    • #778538

      It appears you are defining a collection and then copying the first file to a zip file, deleting the file and then looking for the next item in the collection. This is problematic as the collection has just shrunk by one item.

      Can you reverse the loop direction so that the last file is attacked first?
      For N = lstFiles.ListCount – 1 To 0 Step -1

      Alternatively, I would have a look at the switches used in the shelled zip command. I would expect you can move the file rather than copy it if you found the right switch. I use Winzip and the command line component called wzzip which has a lot of flexibility in copying files to a zip archive.

    Viewing 3 reply threads
    Reply To: Archive Files (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: