Solved

working with dirs in delphi

Posted on 2011-03-13
14
604 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:helyonprime96
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35121520
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;
0
 

Author Comment

by:helyonprime96
ID: 35121534
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
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35121538
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;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35121547
... 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;
0
 

Author Comment

by:helyonprime96
ID: 35121565
thanks experts. i will test code
0
 

Author Comment

by:helyonprime96
ID: 35121646
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?
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35121799
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
0
 

Author Comment

by:helyonprime96
ID: 35122457
sorry, i tested it.
subfolders are not in primary folder.
subfolders by code will be located as primary folders.please help
0
 
LVL 2

Accepted Solution

by:
RezaSadigh earned 500 total points
ID: 35125319
Ok,
sorry for delay, if you want to put total structure in primary folder please check this: (attention there is two functions)

function CopySubDirStruct(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 + CopySubDirStruct(Dir+sr.Name+'\', FTPTargetDir+sr.Name+'\');
     end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;

function CopyDirStruct(Dir, FTPTargetDir: string): integer;
var
  PrimaryDir: string;
begin
  if (Copy(Dir, Length(Dir), 1)<>'\') then
    Dir:= Dir + '\';
  if (Copy(FTPTargetDir, Length(FTPTargetDir), 1)<>'\') then
    FTPTargetDir:= FTPTargetDir + '\';
  PrimaryDir:= ExtractFileName(ExtractFileDir(Dir));
  idftp1.MakeDir(FTPTargetDir+PrimaryDir);
  Result:= CopySubDirStruct(Dir, FTPTargetDir+PrimaryDir+'\');
end;

Example of using:
  1- CopyDirStruct('C:\TestDir\', ''); // copy the TestDir and below structure  to root of FTP Dir
  2- CopyDirStruct('C:\TestDir\', 'MyFolder\'); // copy the TestDir and below structure to MayFolder of FTP Dir
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35134184

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
0
 
LVL 2

Assisted Solution

by:RezaSadigh
RezaSadigh earned 500 total points
ID: 35144372
Ok, we should note to ewangoya comment, and correct the code like this

function CopyDirStruct(Dir, FTPTargetDir: string): integer;  // Dir & FTPTargetDir Should be teminated with \ and /
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
        if (FTPTargetDir='') then
          idftp1.ChangeDir('/')
        else idftp1.ChangeDir(FTPTargetDir);
        idftp1.MakeDir(sr.Name);
        n:= n + CopySubDirStruct(Dir+sr.Name+'\', FTPTargetDir+sr.Name+'/');
     end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  Result:= n;
end;

unfortunately i cant test this code please test and tell me again.
Best regards
Reza
0
 

Author Comment

by:helyonprime96
ID: 35159560
hey thanks. now code works perfectly..Thanks

last : how can i upload files with this code??
Thanks
0
 

Author Closing Comment

by:helyonprime96
ID: 35159580
thanks again.

please respond to my last question..thanks a lot
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35170099
I am very glad,
please open a new question  me and other Experts try to help you.
with best regards.
Reza
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse with various Java releases 7 91
programming a polycom voip phone 3 86
migrate this code to work on android 1 46
IP without any Dots 1 99
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question