Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Making A Cd's Cataloges in delphi 6

Hi All

Iam trying to make a program in delphi6 and windows Xp Pro...  what that program will do is  read the Data Cd's and make a kind of catalog either in txt or html format
i prefere HTML format...  Any Help Please


any Free Components will do that or any source code to start with or any body have such a program before?

thanks... zaferms.

0
zaferms
Asked:
zaferms
  • 6
  • 5
1 Solution
 
CynnaCommented:
zaferms,

What data (columns) should this catalog contain?
If you want it to be automatically generated than I presume you want the names of all files (or just programs)? If this is true, you could just iterate through CD tree and extract appropriate file names.

Post more detailed explanation please...
0
 
zafermsAuthor Commented:

Hi Cynna

Yes I want it to be automatically generated
I want the names of all files+Folders & the Size

so i can make kind of grid to display them and to search
for any files in those cd's + i want to export it to html format if there is any delphi component will do that if not then i will save it in txt format. thanks
0
 
CynnaCommented:
zaferms,

I created set of functions which do what you want. You'll get html file with CD contens in it.

Create empty application and place Button on a Form.

Copy & Paste following functions:
----------------------------------------------

procedure GetCDInfo(var CDLetter, CDLabel: String);
var DriveLetters: String;
    i: Integer;
    Buf: array [0..MAX_PATH] of Char;
    NotUsed, VolFlags: DWORD;
begin
  DriveLetters:='DEFGHIJK';
  // Find CD Drive letter and volume label:
  for i:=1 to Length(DriveLetters) do begin
      CDLetter:=DriveLetters[i]+':\';
      if GetDriveType(PChar(CDLetter))=DRIVE_CDROM then begin
         if GetVolumeInformation(PChar(CDLetter)   , Buf,
                                 DWORD(sizeof(Buf)), nil,
                                 NotUsed, VolFlags , nil, 0) then
            SetString(CDLabel, Buf, StrLen(Buf))
        else CDLabel := '';
        Exit;
      end;
  end;
end;

procedure RecurseTree(BaseDir: String; FullFileNames: TStringList);
var SR: TSearchRec;
    DosError: integer;
begin
    // Find first file
    if BaseDir[Length(BaseDir)]<>'\' then BaseDir:= BaseDir + '\';
    DosError := FindFirst(BaseDir+'*.*', faAnyFile, SR);
    // Find all files in directory
    while DosError = 0 do begin
      if (SR.Attr <> faDirectory) then FullFileNames.Add(BaseDir+SR.Name+'?'+IntToStr(SR.Size));
      DosError := FindNext(SR);
    end;
    FindClose(SR);
    DosError := FindFirst(BaseDir+'*.*', faAnyFile, SR);
    // Find all dirs in directory
    while DosError = 0 do begin
      if ((SR.Attr = faDirectory) and (SR.Name<>'.')
         and (SR.Name<>'..')) then begin
         RecurseTree(BaseDir+SR.Name, FullFileNames);
      end;
      DosError := FindNext(SR);
    end;
    FindClose(SR);
end;

procedure CDRaport (var FileName, Title: String);
var i: Integer;
    s, CDLetter, CDLabel, shadow: String;
    F: TextFile;
    FileList: TStringList;
    ShortFileName, FilePath, FileSize: String;
    procedure w(fs: String);
    begin
      WriteLn(F, fs);
    end;
begin
  FileList:=TStringList.Create;
  FileList.Sorted:=FALSE;
  // Grab CD info:
  GetCDInfo(CDLetter, CDLabel);
  // If no FileName, generate it automatically:
  if FileName='' then begin
     FileName:=CDLabel+'.html';
     if FileName='' then FileName:='untitledCD.html';
     FileName:=ExtractFilePath(Application.ExeName)+FileName;
  end;
  // If no Title generate it automatically:
  if Title='' then Title:=CDLabel;
  // Write raport file header:
  ThousandSeparator:=',';
  Assign(F, FileName); Rewrite(F);
  w('<HTML> ');
  w('<HEAD><TITLE>CD contens - '+Title+'</TITLE></HEAD>');
  w('<BODY bgcolor="#000000">');
  w('<H1><CENTER><U><I><FONT color=red>'+Title+'</FONT></I></U></CENTER></H1>');
  w('   <TABLE border=1 cellPadding=5 cellSpacing=2 width="100%">');
  w('   <TBODY>');
  w('   <TR>');
  w('      <TD align=left bgColor=blue width="30%"><FONT color=Yellow><B>');
  w('          File Name');
  w('      </B></TD>');
  w('      <TD align=left bgColor=blue width="55%"><FONT color=Yellow><B>');
  w('          File Path');
  w('      </B></TD>');
  w('      <TD align=left bgColor=blue width="15%"><FONT color=Yellow><B>');
  w('          File Size');
  w('      </B></TD>');
  w('   </TR>');
  // Grab files:
  RecurseTree(CDLetter, FileList);
  for i:=0 to FileList.Count-1 do begin
      s:=FileList.Strings[i];
      FileSize:=Copy(s, Pos('?',s)+1, Length(s));
      s:=Copy(s, 1, Pos('?',s)-1);
      ShortFileName:=ExtractFileName(s);
      FilePath:=ExtractFilePath(s);
      if i mod 2=1 then shadow:='bgColor="#222222"' else shadow:='';
      w('   <TR>');
      w('     <TD align=left '+shadow+'> <FONT color=white>');
      w(ShortFileName);
      w('     </TD>');
      w('     <TD align=left '+shadow+'> <FONT color=white>');
      w(FilePath);
      w('     </TD>');
      w('     <TD align=right '+shadow+'> <FONT color=white>');
      w(FloatToStrF(StrToFloat(FileSize), ffNumber, 10,0));
      w('     </TD>');
      w('   </TR>');
  end;
  // Finish html document:
  w('   </TBODY>');
  w('   </TABLE>');
  w('</BODY>');
  w('</HTML> ');
  CloseFile(F);
  FileList.Free;
end;



Demonstration:
-------------------------

procedure TForm1.Button1Click(Sender: TObject);
var FullFileName, RaportTitle: String;
begin
  FullFileName:=''; RaportTitle:='';
  if MessageDlg('Insert CD, please...', mtConfirmation,
                [mbOK, mbCancel], 0) = mrOK then begin
     CDRaport(FullFileName, RaportTitle);
     ShowMessage('Raport generated and saved in'+#13+#10+FullFileName);
  end;
end;




Notes:
----------------

1. CDRaport is the procedure that generates html file. Just call it and you'll get list of all files on CD.
   Name and path of html file can be set by FullFileName.
   Generated document title is set by RaportTitle.
   If you set any of these vars to '', it will be  automatically set to CD label (look at the CDRaport() code)

2. You can easily modify CDRaport() procedure to set different file filters, change html formatting (color, fonts...) etc.

3. You can copy/paste table in raport to Excell sheet, and additionally  modify it as you see fit.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zafermsAuthor Commented:
Thanks Cynna
this is a very good code But!!!

It Still not what i need

1)-it will see the folders on the cd as a files
    means it will write them on the report as a files
    it should at least say (DIR) in front of them.

can we scan each dir content in sperate html file?
in one go i mean it should go to each dir and read it's  contents and create sperate html file  by dir name is this possible i hope ,, that will be great.

2)-it will read drive E:\ only  i have 2 cd drives
      E:\  ,  F:\  how can i read F:\?

3)- i noticed the folders size is not right
     Example ( i have folder on the cdrom called  
     earth plant the size on the disk is  123MB
     But the size on the report  51,412 ??.

     i want to know the size is in KB or Bytes?.

4)- the final report will not sort the files    
    alphabatically   A..Z


Thanks any way but if you can modify your code so it will meet my needs ... i will grade you,,, thanks again,,, zaferms

0
 
CynnaCommented:
zaferms,


> 1)-it will see the folders on the cd as a files
>   means it will write them on the report as a files
>   it should at least say (DIR) in front of them.

This should not happen. Line:

 if (SR.Attr <> faDirectory) then FullFileNames.Add(....)

explicitly ensures this doesn't happen.
If you look at the code you'll see that only items from FullFileNames list are added to raport. These items are *NOT* directories, because only line that adds items to list is the upper one which states:
        "add this item only if its not directory"

I just tested the code on 2 CD's and I see only files, their folders and sizes...

Did you make a test with several different CDs?
How do you know that folders on CD are *not* actually files?
Did you browsed to this CD with some other file manager other then Windows Explorer?
Try looking at the file tree with, for example, Windows Commander. It will give you actuall file/folder structure.



> 2)-it will read drive E:\ only  i have 2 cd drives
>     E:\  ,  F:\  how can i read F:\?

Good point. I didn't anticipated this (rather usual these days..) scenario. This is very easy to correct. For example, you could modify GetCDInfo() function so that it doesn't automatically check for first CD drive letter, but instead uses the one you provided. You can do this easily yourself now, but I'll send you corrected version, along with other corrections, later.



> 3)- i noticed the folders size is not right
>    Example ( i have folder on the cdrom called  
>    earth plant the size on the disk is  123MB
>    But the size on the report  51,412 ??.

This seems to be related to point 1). My code *doesn't* list any folders, you should see only files and their sizes.


> i want to know the size is in KB or Bytes?.

It's in bytes. Code could easily be modified to automatically adjust postfix to Kb or Mb, but this is not good for a quick comparation between sizes. Tell me what would you like, and I'll modifiy it.


> 4)- the final report will not sort the files    
>   alphabatically   A..Z

This is a bit more work, but can be done.

> Thanks any way but if you can modify your code so it will meet my needs ... i will grade you,,,
> thanks  again,,, zaferms
 
I can modify it, but we have to be clear on points 1) and 3). Read my remarks on point 1), and then get back to me with final suggestions.


C.


P.S. I won't be able to access my machine with Delphi for 2 days, so I'll post then, please be patient.


0
 
zafermsAuthor Commented:

Hi Cynna

>This seems to be related to point 1). My code *doesn't* >list any folders, you should see only files
>and their sizes


**too bad** how can i know  folders from files.

how can I Read each Folder content in sperate html file?
in one go i mean it should go to each Folder and read it's  contents and create sperate html file  by Folder
name is this possible ??

I do not think so(**My code *doesn't*list any folders**).

but anyway go ahead and make the modifications

1)-it will read drive E:\ only  i have 2 cd drives
     E:\  ,  F:\  how can i read F:\?

> modify GetCDInfo() function so that it doesn't automatically check for first CD drive letter, but instead uses the one you provided.

2)- the final report will not sort the files    
   alphabatically   A..Z
> This is a bit more work, but can be done.


3)-If you Can Please Do Any Procedure to read Each Folder sperately  Please do if you can't .. then it's no big deal.

go ahead and finish the modifications and send it to me i will grade you 200 points  you deserve it real Expert.
 thanks .... Cu .
 zaferms

0
 
zafermsAuthor Commented:

Hi Cynna

>This seems to be related to point 1). My code *doesn't* >list any folders, you should see only files
>and their sizes


**too bad** how can i know  folders from files.

how can I Read each Folder content in sperate html file?
in one go i mean it should go to each Folder and read it's  contents and create sperate html file  by Folder
name is this possible ??

I do not think so(**My code *doesn't*list any folders**).

but anyway go ahead and make the modifications

1)-it will read drive E:\ only  i have 2 cd drives
     E:\  ,  F:\  how can i read F:\?

> modify GetCDInfo() function so that it doesn't automatically check for first CD drive letter, but instead uses the one you provided.

2)- the final report will not sort the files    
   alphabatically   A..Z
> This is a bit more work, but can be done.


3)-If you Can Please Do Any Procedure to read Each Folder sperately  Please do if you can't .. then it's no big deal.

go ahead and finish the modifications and send it to me i will grade you 200 points  you deserve it real Expert.
 thanks .... Cu .
 zaferms

0
 
CynnaCommented:
zaferms,

It seems I misunderstood you:

> I want the names of all files+Folders & the Size

I understood this as: "I want files, along with their folders and sizes."
So, only files were listed, with folders and sizes as their "properties".



> 1)-it will see the folders on the cd as a files
                  ....
> 3)- i noticed the folders size is not right

I was puzzled with these statements, because there should be no folders at all.
This is the reason of my overstating that only files are shown.

But, after you latest statement:

> **too bad** how can i know  folders from files

I geather you do want folders as well.



> ...is this possible ??
> I do not think so(**My code *doesn't*list any folders**).

It is possible, I just didn't understood what you wanted.

------------------------------

OK, I hope I got you now. Bellow is totally revamped version whitch does this:

1. Go to CD
2. List all folders and files with their sizes
3. Sort it alpabetically, folders first
4. Save this list to html document, with name
   generated based on current path
5. Repeat 1-5 for every subfolder




Copy/Paste:
--------------------

procedure GetCDInfo(var CDLetter, CDLabel: String);
var DriveLetters: String;
    i: Integer;
    Buf: array [0..MAX_PATH] of Char;
    NotUsed, VolFlags: DWORD;
begin
  // If CDLetter is given, don't try to find CD ROM, just grab volume label:
  if CDLetter<>'' then begin
     if GetVolumeInformation(PChar(CDLetter)   , Buf,
                             DWORD(sizeof(Buf)), nil,
                             NotUsed, VolFlags , nil, 0) then
        SetString(CDLabel, Buf, StrLen(Buf))
     else CDLabel := 'untitled CD';
     Exit;
  end;
  DriveLetters:='DEFGHIJK';
  // Find CD Drive letter and volume label:
  for i:=1 to Length(DriveLetters) do begin
      CDLetter:=DriveLetters[i]+':\';
      if GetDriveType(PChar(CDLetter))=DRIVE_CDROM then begin
         if GetVolumeInformation(PChar(CDLetter)   , Buf,
                                 DWORD(sizeof(Buf)), nil,
                                 NotUsed, VolFlags , nil, 0) then
            SetString(CDLabel, Buf, StrLen(Buf))
        else CDLabel := 'untitled CD';
        Exit;
      end;
  end;
end;

function RecurseTree(BaseDir: String; FullFileNames, DirNames: TStringList): Integer;
var SR: TSearchRec;
    DosError, SubSize, DirSize, Index: Integer;
    tmp: String;
begin
    // Find first file
    if BaseDir[Length(BaseDir)]<>'\' then BaseDir:= BaseDir + '\';
    DosError := FindFirst(BaseDir+'*.*', faAnyFile, SR);
    DirSize:=0;
    DirNames.Add(BaseDir); Index:=DirNames.Count-1;
    // Find all files in directory
    while DosError = 0 do begin
      if (SR.Attr <> faDirectory) then begin
          FullFileNames.Add(IntToStr(Index)+'?'+SR.Name+'?'+IntToStr(SR.Size));
          DirSize:=DirSize+SR.Size;
      end
      else begin
          tmp:=BaseDir+SR.Name+'\';
          if (SR.Name<>'.') and (SR.Name<>'..') then begin
             SubSize:=RecurseTree(tmp, FullFileNames, DirNames);
             FullFileNames.Add(IntToStr(Index)+'?!'+SR.Name+'\?'+IntToStr(SubSize));
             DirSize:=DirSize+SubSize;
          end;
      end;
      DosError := FindNext(SR);
    end;
    FindClose(SR);
    Result:=DirSize;
end;

procedure BuildHtml(Base, Path: String; List:TStringList);
var i,tmp, Total: Integer;
    s, shadow, FileName, Title, Home: String;
    F: TextFile;
    Item, ItemSize, BaseName: String;
    IsDir: Boolean;
    procedure w(fs: String);
    begin
      WriteLn(F, fs);
    end;
begin
  if (Path[1]='!') and (Path[Length(Path)]='\') then Path:=Copy(Path, 2, Length(Path));
  ThousandSeparator:=',';
  List.Sort;
  BaseName:=StringReplace(Path, '\','.', [rfReplaceAll]);
  FileName:=Base+BaseName+'html';
  Home:=Copy(Path, 1, Pos('.',Path)-1)+'.html';
  Title:=StringReplace(Path, '.',':\', [rfReplaceAll]);
  Title:=StringReplace(Title, '\',' \ ', [rfReplaceAll]);
  Assign(F, FileName); Rewrite(F);
  // Write raport file header:
  w('<HTML> ');
  w('<HEAD><TITLE>CD Dir contens - '+Title+'</TITLE></HEAD>');
  w('<BODY bgcolor=black text=white link=#00FFEE vlink=yellow alink=green>');
  w('<H1><CENTER><I><FONT color=red>');
  w('   <A HREF='+Home+'>'+Title+'</A>');
  w('</FONT></I></CENTER></H1>');
  w('   <TABLE border=0 cellPadding=5 cellSpacing=2 width="100%">');
  w('   <TBODY>');
  w('   <TR>');
  w('      <TD align=left bgColor=blue width="80%"><FONT size=5><B>');
  w('          File Name');
  w('      </B></TD>');
  w('      <TD align=right bgColor=blue width="20%"><FONT size=5><B>');
  w('          Size (bytes)');
  w('      </B></TD>');
  w('   </TR>');
  Total:=0;
  for i:=0 to List.Count-1 do begin
      s:=List.Strings[i];
      tmp:=Pos('?',s);
      Item:=Copy(s, 1, tmp-1);
      if (Item[1]='!') and (Item[Length(Item)]='\') then
          Item:=Copy(Item, 2, Length(Item));
      s:=Copy(s, tmp+1, Length(s));
      ItemSize:=s;
      Total:=Total+StrToInt(ItemSize);
      if i mod 2=1 then shadow:='bgColor="#222222"' else shadow:='';
      if Item[Length(Item)]='\' then IsDir:=TRUE else IsDir:=FALSE;
      // if IsDir then FontColor='Yellow'
      w('   <TR>');
      w('     <TD align=left '+shadow+'>');
      if IsDir then w('     <A HREF='+BaseName+
                      StringReplace(Item, '\','.', [rfReplaceAll])+'html>');
      w('           '+Item);
      if IsDir then w('     </A>');
      w('     </TD>');
      w('     <TD align=right '+shadow+'>');
      if IsDir then w('       <FONT color=yellow>');
      w('           '+FloatToStrF(StrToFloat(ItemSize), ffNumber, 10,0));
      if IsDir then w('       </FONT>');
      w('     </TD>');
      w('   </TR>');
  end;
  // Finish html document:
     w('     <TR><TD align=right bgColor=#DDEE44>');
     w('       <FONT color=red><B>Total dir size:</B></FONT></TD>');
     w('       <TD align=right bgColor=#DDEE44> <FONT color=red> <B>');
     w('           '+FloatToStrF(Total, ffNumber, 10,0));
     w('       </B></FONT></TD>');
     w('   </TR>');
  w('   </TBODY>');
  w('   </TABLE>');
  w('</BODY>');
  w('</HTML> ');
  CloseFile(F);
end;

procedure MakeCDRaport (var BasePath, CDLetter: String);
var i, j, tmp: Integer;
    s, CDLabel: String;
    FileList, DirList, SingleDir: TStringList;
begin
  FileList:=TStringList.Create;
   DirList:=TStringList.Create;
 SingleDir:=TStringList.Create;
  FileList.Sorted:=FALSE;
   DirList.Sorted:=FALSE;
 SingleDir.Sorted:=FALSE;
  // Grab CD info:
  GetCDInfo(CDLetter, CDLabel);
  // If no BasePath, generate it automatically:
  if BasePath='' then BasePath:=ExtractFilePath(Application.ExeName)+CDLabel+'\'
                 else if BasePath[Length(BasePath)]<>'\' then BasePath:= BasePath + '\';
  ForceDirectories(Copy(Basepath, 1, Length(Basepath)-1));
  // Grab files and dirs:
  RecurseTree(CDLetter, FileList, DirList);
  // Generate raports:
  for i:=0 to DirList.Count-1 do begin
      SingleDir.Clear;
      for j:=0 to FileList.Count-1 do begin
          s:=FileList.Strings[j];
          tmp:=Pos('?',s);
          if StrToInt(Copy(s, 1, tmp-1))=i then SingleDir.Add(Copy(s, tmp+1, Length(s)));
      end;
      // Save each dir to separate file under BasePath:
      s:=StringReplace(DirList.Strings[i],CDLetter,CDLabel+'.', [rfIgnoreCase]);
      BuildHtml(BasePath, s, SingleDir);
  end;
  FileList.Free;
   DirList.Free;
  SingleDir.Free;
end;


procedure TForm1.Button3Click(Sender: TObject);
var BasePath, CDLetter: String;
begin
  // If empty, will be set to [Application path]/[CD label]:
  BasePath:='C:\My CD contens\';
  // If empty, will be set to first available CD-ROM drive:
  CDLetter:='F:\';
  if MessageDlg('Insert CD, please...', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then begin
     MakeCDRaport(BasePath, CDLetter);
     ShowMessage('CD list contens saved in:'+#13+#10+BasePath);
  end;
end;






NOTES:
------------

MakeCDRaport() is called with two arguments:

1. BasePath - is the local folder, where generated html files are saved
   If you leave it empty, it will be automatically set to [Application path]/[CD label]

2. CDLetter - for your case, you could put 'E:\' or 'F:\' directly.
   If you leave it empty, it will be automatically set to first available CD ROM drive.
0
 
zafermsAuthor Commented:

 Cynna  Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks,,, It Is Very Very Good Job.

If you don't mind I want lettel help
to choose  Cd Drive Letter that will be read ( E:\  ,, F:\ ) and to choose the directory that it will save the html files at.

i want to make a startup form to be displayed when i start the program and give me chance to choose the drive letter @ choose the directory that it will save the html files at.
 thanks again.. and keep the good job.


zaferms
0
 
CynnaCommented:
zeferms,

> It Is Very Very Good Job.
Thanks.


>i want to make a startup form to be displayed when i >start the program and give me chance to choose
>the drive letter @ choose the directory that it will save >the html files at.

There are about a thousand ways to do this. I will give you detailed explanation of one of the easiest ones.

1. Start new application.
2. Copy my functions in Unit1.
3. Go to tab "Win 3.1" Delphi palette (you know, you have Standard, Additional, .... - scroll to Win 3.1)
4. Place component DirectoryListBox on the form
5. Above it, place component DriveComboBox
6. Return to Standard palette and place Button on the form
7. Double-click Button to make event handler.
8. Copy this inside event handler:

procedure TForm1.Button1Click(Sender: TObject);
var BasePath, CDLetter: String;
begin
  CDLetter:=UpperCase(DriveComboBox1.Drive+':\');
  BasePath:=DirectoryListBox1.Directory;
  if MessageDlg('Make report of '+CDLetter+' and save it to '+BasePath+'?', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then begin
     MakeCDRaport(BasePath, CDLetter);
     ShowMessage('CD list contens saved in:'+#13+#10+BasePath);
  end;
end;


Now start your app.
1. From the list of drives select E:\ or F:\
2. From the list of the folders select folder where html
   raport files are to be saved.
3. Click Button1
0
 
zafermsAuthor Commented:
thank you very much..... and cu. zaferms
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now