Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me refine this code.....

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Greetings to all of you programmer gurus…! =)

Can some one please help me refine this code??
Its intention is to search a folder (included sub directories) for files / folders and store it in a file saved to disk (i.e. C:\test.txt)
The parts I want to remove mostly are those dealing with TRichEdit… Need something else!! I'm tired of using TRichEdit for my programs…! =/
What else but TRichEdit can I use??

The code below are my own, and created entirely by me (with help from others, 2ffat for instance), however, you can copy it (freely) and use it for own purpose!!

Code:
#include <dos.h>
#include <dir.h>
void __fastcall TForm1::scan_it(TObject *Sender)
{
 /*frag*/
 struct ffblk ffblk; // Structure for file info..
 AnsiString curr_used_path; // currently scaned directory
 int antal_files=0,antal_folders=0; // total files / folders in scan area
 long total_size=0; // total size of all files in scan area
    RichEdit1->Lines->Clear(); // clear first TRichBox of lines
    RichEdit2->Lines->Clear(); // clear second TRichBox of lines
 int done; // done, no more files in directory
 RichEdit2->Lines->Add( scan_this ); // add first search directory to second TRichEdit
 while(lstUrls->Lines->Count>0) // You know this…
    {
     curr_used_path = RichEdit2->Lines->Strings[0]; // set curr_used_path to the first line in second TRichEdit
     done = findfirst((AnsiString(curr_used_path)+&quot;*.*&quot;).c_str(), &ffblk,FA_DIREC|FA_HIDDEN|FA_RDONLY); // find first file / folder (all)
     while(!done) // while something is found
        {
         int direct = strcmp(ffblk.ff_name, &quot;.&quot;); // exclude dir with name '.'
         int subdirect = strcmp(ffblk.ff_name, &quot;..&quot;); // exclude dir with name '..'
         if(direct != 0 && subdirect != 0) // if dir is not above
            {
             if( (ffblk.ff_attrib&FA_DIREC) == FA_DIREC ) // if found is dir. 
                {
                 antal_folders++; // count of folders foud
                 RichEdit2->Lines->Insert( 1, curr_used_path + ffblk.ff_name+&quot;\\&quot; ); // add folder path to second TRichEdit
                }
             else // if found not is a folder (is file)
                {
                 RichEdit1->Lines->Add( &quot;;&quot;+curr_used_path + ffblk.ff_name+&quot;?&quot;+IntToStr(ffblk.ff_fsize) ); // add file path+name+size to first TRichEdit
                 total_size += ffblk.ff_fsize; // No. of size of found files
                 antal_files++; // No. of found files
                }
            }
         done = findnext( &ffblk ); // find next file / folder
        }
     RichEdit2->Lines->Delete( 0 ); // delete first line in second TRichEdit (the path we recently searched)
    }
RichEdit1->Lines->SaveToFile( &quot;C:\\test.txt&quot; ); // save first TRichEdit lines to a file
}

Thanks for any idea…
Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
Just out of curiosity, why did you choose RichEdit is the first place? Was there some underlining reason for wanting to use Rich Text in your forms? If not, was there some reason why you didn't choose the Memo component? Answers to these questions can help us decide where to go to next.

James P. Cottingham
 
Yes, the reason I wanted the TRichEdit instead of TMemo is that TRichEdit is capable of dealing with an “unlimited” number of lines! Due to the fact that my program searches a folder / drive for files there will be many lines in the (first) TRichEdit!
I use the second TRichEdit instead of arrays! BTW, TRichEdit components property ‘PlainText’ is set to ‘true’ so it isn’t Rich Text I’m dealing with

One change I was thinking of was how to save each line directly to the file instead of adding it to the TRichEdit and then save it…

Hope it helped... Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
Interesting dilemma! I would definately save you data to a file FIRST. At this point, I guess we need to know more about what you are going for on your screen so we can advise as to what component to look for. Are you looking for something that works like the FileListBox? You may have to go online to find a custom component that is freeware. Let us know. James P. Cottingham
 
The actual code doesn't do anything on the screen! Both of the two TRichEdit components are hidden and read-only.... The user is incapable of altering any information in them!
Maybe I should put it something like this:
The user selects a folder from CDirectoryOutline (or the TDirectoryListBox), and then clicks the scan button. Here the scan starts (the code far above). The scan searches the first directory for files and folders. The folders (full path) are stored in one TRichEdit, files in the second! When finished searching a folder that, the path will be deleted from the first TRichEdit!

It is here I want to make the changes! I want to remove (at least) one of the TRichEdit components, (primarily) the one containing full paths! I want to use something else but TRichEdit for this! For instance arrays (if it’s possible)!

The second TRichEdit, containing files are to be saved to file, when the scan has finished!
During the scan when a file is found it is added to the TRichEdit. This I also want to remove, and do something like this:
When a file is found the program is to run a new function to ADD that line to a specific file!!
Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
Sounds like a job for. . . STL! (|:)>
Instead of using VCL components, you should be using something like vectors, list, or deque. These are containers, or collections, that hold other objects. Similar to arrays, these objects don't have the limitations that arrays have. For example:
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

main()
{
  vector<string> tokens; // declares a vector of strings named tokens
  string token;

  // Read tokens, fill vector
  while (cin >> token)
    tokens.push_back(token);

  int ntok = token.size();
  cout << &quot;There are &quot; << ntok  << &quot; tokens:\n&quot;;
  for (int i = 0; i < ntok; i++)
    cout << i << &quot;: &quot; << tokens[i] << endl;
}

// Input
how now brown cow

// Output
0: how
1: now
2: brown
3: cow

The above code came from C & C++ Code Capsule by Chuck Allison. You could create two vectors to replace your TRichEdit boxes.

James P. Cottingham
 
I'll look in to it...though it’ll demand some rewritten code! Just what I like =)
BTW, there isn't any need of deleting these 'vectors', is there?


Thanks...! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
I suppose that it depends on how you create them. Rule of thumb, if you create them using new then you must use delete.
James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top