Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 03-21-2012, 12:40 PM
jerbear jerbear is offline
Junior Member
 
Join Date: Jun 2009
Location: Ireland
Posts: 17
Send a message via Skype™ to jerbear
Default FileListBox and Multi Select

Googling this like crazy but have not found a solution yet.
I have FileListBox which displays every file in a chosen folder.
I have Multi-Select ticked and I want to select some of these to zip up.
I'm using ZipForge from Component Ace to create the ZIP file.

Unfortunately I've not used a FileListBox before and it's proving a problem.
I need to pass each selected filename to the ZipForge FileMasks.Add function
and have tried 3 methods....only one of which works but not properly.


Code:
FileMasks.Clear;
    For x := 0 to ZipFileListBox.SelCount -1 do //  This gets the correct number of files selected...

           FileMasks.Add(ZipFileListBox.Selected[x]);  // This does not work - E2010 Incompatible types: 'WideString' and 'Boolean'
           
           FileMasks.Add(ZipFileListBox.ItemIndex[x]);  // nor does this  - E2016  Array Type required

           FileMasks.Add(ZipFileListBox.Filename); // and this just adds the last file in the selected list

AddFiles;
Not as simple as I thought it was going to be.....Using Delphi 2007.
But it should be this simple...should it not????

If anyone can help...great...if not then I'll keep googling.....
  #2  
Old 03-21-2012, 02:21 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

Isn't it just as simple as:
Code:
var
  i: Integer;
  files: TStrings;
begin
  files := TStringList.Create();
  try
    for i := 0 to FileListBox1.Count - 1 do
    begin
      if FileListBox1.Selected[i] then
        files.Add(FileListBox1.Items[i]);
    end;

    ShowMessage(files.Text);
  finally
    files.Free();
  end;
end;
  #3  
Old 03-21-2012, 02:28 PM
jerbear jerbear is offline
Junior Member
 
Join Date: Jun 2009
Location: Ireland
Posts: 17
Send a message via Skype™ to jerbear
Default

I'm afraid not.

As I said, this does not work, and I posted the errors I'm getting in my original posting.

I'm totally lost as to why it doesn't.

Any programmers out there shed some light on this please....
  #4  
Old 03-21-2012, 03:13 PM
major major is offline
Senior Member
 
Join Date: Jun 2011
Posts: 150
Default

Norrit's code should work except that rather than adding them to a separate stringlist, add it directly to the FileMasks stringlist structure

Code:
var
  i: Integer;
begin
    for i := 0 to ZipFileListBox.SelCount-1 do
    begin
      if ZipFileListBox.Selected[i] then
        FileMasks.Add(ZipFileListBox.Items[i]);
    end;
end;
  #5  
Old 03-21-2012, 04:03 PM
jerbear jerbear is offline
Junior Member
 
Join Date: Jun 2009
Location: Ireland
Posts: 17
Send a message via Skype™ to jerbear
Default

I've managed to get a bit further with this thanks to you guys, but still can't get the files into the archive.
The following code executes, goes through the motions, creates the zip file, but the files don't get added.
ZIP file is empty. I have verified that the selected file names are being passed to FileMasks.Add but..........

Code:
Var AllSelectedFiles : Integer;
 
    AllSelectedFiles := ZipFileListBox.SelCount;

    FileMasks.Clear;

    For x := 0 to AllSelectedFiles-1 do     
      If ZipFileListBox.Selected[x] then 
         FileMasks.Add(ZipFileListBox.Items[x]);
       
    AddFiles;

    CloseArchive();
  #6  
Old 03-21-2012, 06:52 PM
OldTurtle OldTurtle is offline
Junior Member
 
Join Date: Apr 2010
Posts: 25
Default

Perhaps is missing the full path of file (must be saved as C:\mydir\test.txt, not only test.txt).

Set also BaseDir property of zip component.
  #7  
Old 03-22-2012, 09:32 AM
jerbear jerbear is offline
Junior Member
 
Join Date: Jun 2009
Location: Ireland
Posts: 17
Send a message via Skype™ to jerbear
Default

Quote:
Originally Posted by OldTurtle View Post
Perhaps is missing the full path of file (must be saved as C:\mydir\test.txt, not only test.txt).

Set also BaseDir property of zip component.
I believe I have these set correctly... see modified code below...

Code:
Var AllSelectedFiles : Integer;
 
    FileName := 'c:\zipped files\mydata.zip';
    BaseDir := 'c:\zipped files';

    AllSelectedFiles := ZipFileListBox.SelCount;

    FileMasks.Clear;

    For x := 0 to AllSelectedFiles-1 do     
      If ZipFileListBox.Selected[x] then 
         FileMasks.Add(ZipFileListBox.Items[x]);
       
    AddFiles;

    CloseArchive();
As I said, the zip file is being created in the chosen directory, c:\zipped files, but it's an empty file. I have confirmed that the selected files are being passed to the FileMasks stringlist but are not being pushed into the zip file by the AddFiles command.

According to the guys at Component Ace the code below is how it's done and I have to assume that my code is more or less the same as what is shown here.
Code:
 with archiver do

  begin

    // Set the name of the archive file we want to create

    FileName := 'C:\test.zip';

    // Because we create a new archive,

    // we set Mode to fmCreate

    OpenArchive(fmCreate);

    // Set base (default) directory for all archive operations

    BaseDir := 'C:\';

    // Add files to the archive by list

  FileMasks.Clear;

  FileMasks.Add('c:\file1.txt'); 

  FileMasks.Add('c:\data\file2.txt'); 

    AddFiles;

    CloseArchive();
Totally baffled by this.. I have e-mailed the support team at component ace, 3 times yesterday at 11:12am, 15:00pm, and 15:55pm, but as yet have received no reply. The chap that sent me the above code said, and I quote, "Please feel free to write me if I can be of any further assistance.". Either he's very busy or he didn't get the e-mails. I don't know which.

I agreed to buy ZipForge if it works but until it does, that won't happen. I already purchased VCLZip but had similar problems with that and It seems the author of VCLZip no longer supports this product.

I also noticed when checking the contents of the FileMasks StringList that it was dropping the first character of every file passed to it. I reported this in my 3rd E-Mail to Component Ace at 3:55pm yesterday, UK time, but I'm still waiting on a response...

jerbear.
  #8  
Old 03-22-2012, 08:03 PM
OldTurtle OldTurtle is offline
Junior Member
 
Join Date: Apr 2010
Posts: 25
Default

FileListBox.item gives only the name of the file, without the complete path, so you should write:

PathToMyFolder:= "C:\mydirectory\";

For x := 0 to AllSelectedFiles-1 do
If ZipFileListBox.Selected[x] then
FileMasks.Add(PathToMyFolder + ZipFileListBox.Items[x]);

I use TurboPower Abbrevia (free) with Delphi 7.
  #9  
Old 03-23-2012, 09:17 AM
jerbear jerbear is offline
Junior Member
 
Join Date: Jun 2009
Location: Ireland
Posts: 17
Send a message via Skype™ to jerbear
Default

Quote:
Originally Posted by OldTurtle View Post
FileListBox.item gives only the name of the file, without the complete path, so you should write:

PathToMyFolder:= "C:\mydirectory\";

For x := 0 to AllSelectedFiles-1 do
If ZipFileListBox.Selected[x] then
FileMasks.Add(PathToMyFolder + ZipFileListBox.Items[x]);

I use TurboPower Abbrevia (free) with Delphi 7.
The "OldTurtle" is wise beyond his years. Well spotted. That indeed fixed the problem. Thank you. I'm posting the button's OnClick Event with the working code below. I've kept the ShowMessage commands in, especially the one within the For LOOP as it stills shows ZipForge's FileMasks dropping the first character of every file passed to it. It's not effecting the creation of the ZIP file but I'd love to know why it's doing this....

I also added a second variable called SelectedFiles and set it to the count of files selected and changed AllFiles to show the number of files within the chosen/current folder. I was looping through the wrong one which caused problems. It was fine if you selected the first x amount of files in order but if you selected random files it obviously crashed.

Big Thanks to everyone for their help, their input and their willingness to help a fellow Delphi Dabbler.

Code:
Procedure TForm1.ZipItUpNowClickClick(Sender: TObject);
Var
  Archiver : TZipForge;
  BakUpPath : string;
  AllFiles,
  SelectedFiles, x : Integer;

Begin
   BakUpPath := 'C:\Zipped Files\';
   If NOT DirectoryExists(BakUpPath) then
      Begin
        If ForceDirectories(BakUpPath) Then
           ShowMessage(BakUpPath+' Created OK')
        Else
           ShowMessage('Failed to Create '+BakUpPath+'! Reason = '+IntToStr(GetLastError));
      End;
  // Create an instance of the TZipForge class
  Archiver := TZipForge.Create(nil);
  Try
  With archiver do
  Begin
    // Set the name of the archive file we want to create
    FileName := BakUpPath+'MyData.ZIP';
    // Because we create a new archive,
    // we set Mode to fmCreate
    OpenArchive(fmCreate);
    // Set base (default) directory for all archive operations
    BaseDir := BakUpPath;
    // Set encryption algorithm and password
    EncryptionMethod := caAES_256;
    Password := 'Password';
    AllFiles := ZipFileListBox.Count;  //  Total number of files in selected folder
    SelectedFiles := ZipFileListBox.SelCount;  // total number of files selected by user
    ShowMessage(IntToStr(SelectedFiles)+' files selected from a total of '+IntToStr(AllFiles)+' files');
    FileMasks.Clear;
    For x := 0 to AllFiles - 1 do
     Begin
      If ZipFileListBox.Selected[x] then  //  If file has been selected add to FileMasks.
        Begin
         FileMasks.Add(DirListBox.Directory+'\'+ZipFileListBox.Items[x]);
         ShowMessage('Current Selected File :['+IntToStr(x)+']'+#10#13+FileMasks.Text);
         //  The above showmessage displays contents of current Item in FileMasks with 1st character missing.
        End
      Else
     End;
    AddFiles;
    CloseArchive();
  End;
    Except
  On E: Exception do
    Begin
      Writeln('Exception: ', E.Message);
      // Wait for the key to be pressed
      Readln;
    End;
  End;
End;
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 09:24 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.