Link to home
Start Free TrialLog in
Avatar of helyonprime96
helyonprime96

asked on

working with dirs in delphi

hello experts.

Help me with one thing.
i have folderdialog component.

i need that application checks number if dirs in a folder.
Example folderdialog shows direction : C:\temp; and in temp folder are dirs and sudirs.
i need that app checks the number of dirs and its name.

Please help.
thanks
Avatar of RezaSadigh
RezaSadigh

Hi my friend,
You should count them. The below code can help you

function DirCount(Dir: string): integer;  // Dir Should be teminated with \
var
  sr: TSearchRec;
  n: integer;
begin
  n:= 0;;
  if FindFirst(Dir+'*.*', faDirectory, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory)=sr.Attr then
        Inc(n);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;
Avatar of helyonprime96

ASKER

ok.thanks a lot.

Can you suggest me hot to do this :
I need that app counts number of dirs (with you function) and creates it into ftp (idftp1.makedir)

thanks
Hi again,
 .... and if you want to Have its name try this one

function DirCount(Dir: string; var DirList: TStrings): integer;  // Dir Should be teminated with \
var
  sr: TSearchRec;
  n: integer;
begin
  n:= 0;;
  if FindFirst(Dir+'*.*', faDirectory, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory)=sr.Attr then
      begin
        Inc(n);
        DirList.Add(sr.Name);
     end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;
... Maybe this code heps you:

function DirCount(Dir, FTPTargetDir: string): integer;  // Dir Should be teminated with \
var
  sr: TSearchRec;
  n: integer;
begin
  n:= 0;;
  if FindFirst(Dir+'*.*', faDirectory, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory)=sr.Attr then
      begin
        Inc(n);
        idftp1.MakeDir(FTPTargetDir+sr.Name)
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;
thanks experts. i will test code
RezaSadigh:

thanks for your code.
it works, but it doesnt make subdirs of folder. and every created folder contains ftptargetdir and name of existing folder.thanks but can you correct it?
Hi,
I am very glad, I try to help you more ...  (if I can)
If You want to create the structure in the main FTP dir, you can pass the ftptargetdir by '' value (it can be used for create the structure in a subdir in main FTP folder)
and if you want to create subfolders you should use recursive function like below code:

function CopyDirStruct(Dir, FTPTargetDir: string): integer;  // Dir & FTPTargetDir Should be teminated with \
var
  sr: TSearchRec;
  n: integer;
begin
  n:= 0;
  if FindFirst(Dir+'*.*', faDirectory, sr) = 0 then
  begin
    repeat
      if ((sr.Attr and faDirectory)=sr.Attr) and (Pos('.', sr.Name)=0) then
     begin
        idftp1.MakeDir(FTPTargetDir+sr.Name);
        n:= n + CopyDirStruct(Dir+sr.Name+'\', FTPTargetDir+sr.Name+'\');
     end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;

Example of using:
  1- CopyDirStruct('C:\TestDir\', ''); // copy the Dir structure under C:\TestDir to root of FTP Dir
  2- CopyDirStruct('C:\TestDir\', 'MyFolder\'); // copy the Dir structure under C:\TestDir to MayFolder of FTP Dir
sorry, i tested it.
subfolders are not in primary folder.
subfolders by code will be located as primary folders.please help
ASKER CERTIFIED SOLUTION
Avatar of RezaSadigh
RezaSadigh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ephraim Wangoya

You can not create directory with ftp in the manner you are trying here
This will not work

For each subdirectory you are creating, you have to change the working directory (CWD) to the immediate parent.
Then simply call MakeDir(SubDirectory)

Example if you have the structure Home/Dir2/Dir3

You first have to change directory to Dir2 -  CWD(Dir2)
Once this is successful, call MakeDir(Dir3)

A simple implementation of FindFirst will not do either

Think objects, create an object that will hold the file names and the directories they belong to.
When transferring the files, start with the files in the Root of your object, then go to each subdirectory one level at a time, this way you call makedir only once for a group of files
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey thanks. now code works perfectly..Thanks

last : how can i upload files with this code??
Thanks
thanks again.

please respond to my last question..thanks a lot
I am very glad,
please open a new question  me and other Experts try to help you.
with best regards.
Reza