Create folder each Drive in Delphi

Hi friend,

i have few difficulties to make a same folder in all drive with few attibute.

Suppose i want to create "MyFolder" in each drive (Fixed and removal) with faArchive.

@epasquier thankful to you.
prasidduttaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MorphorCommented:
// Helper Methods
function IsLogicalDrive(Drive: char): boolean;
var
  cDrive: char;
begin
  cDrive := UpCase(Drive);
  if cDrive in ['A'..'Z']
  then
    Result := (GetLogicalDrives And (1 Shl (Ord(cDrive) - Ord('A')))) <> 0
  else
    Result := False;
end;

procedure createCustomDirs(folder: String; attribute: integer);
var i: Char;
    dir: String;
begin
  for I := 'A' to 'Z' do
    if IsLogicalDrive(i) then
    begin
      dir := i+':\'+folder;
      if CreateDir(dir)
        then FileSetAttr(dir, attribute);
    end;
end;
// Button Click / Implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
  createCustomDirs('MyFolder', faArchive);
end;

Open in new window

0
prasidduttaAuthor Commented:
thanks but error:

see http://screensnapr.com/v/xNQTPX.png
0
MorphorCommented:
Try modifying the createCustomDirs method like that:

procedure createCustomDirs(folder: String; attribute: integer);
var i: Char;
    dir: String;
begin
  for I := 'A' to 'Z' do
    if IsLogicalDrive(i) then
    begin
      dir := i+':\'+folder;
      try // This should not be needed but try it
        if ForceDirectories(dir)
          then FileSetAttr(dir, attribute);
      except
      end;
    end;
end;
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

prasidduttaAuthor Commented:
i change but error is same. any other methods?
0
MorphorCommented:
How are you exactly using the method? (The way you have implemented it)
Your OS is Windows XP, right?
0
prasidduttaAuthor Commented:
i use win 7 now.

i add two procedure and call "createCustomDirs" via TButton1

i need such code which will worl all os. (xp to win 8)
0
MorphorCommented:
What exactly is in your TButton1 OnClick method?
Which procedures have you added?
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
I gave you already how to do that here :
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27373543.html?cid=748#a36899068
and here :
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27362556.html?cid=1131#a36890430

your error is that you try to create your file on ALL LOGICAL DISKS without differentiation for removable medias, fixed drives, or even network drives.

Use the function GetDriveList with MountedOnly set to TRUE
function GetDriveList(DriveTypes:TDriveTypeSet;MountedOnly:Boolean=False):ANSIString;

procedure createCustomDirs(folder: String; attribute: integer);
var 
 i:integer;
 DriveList,Dir:ANSIString;
begin
 DriveList:=GetDriveList([dtRemovable,dtFixed],True); // Get only fixed or inserted removable, local disk
 Dir:='a:\'+folder;
 for i:=1 to Length(DriveList) do
  begin
   Dir[1]:=DriveList[i];
   if CreateDir(dir) then FileSetAttr(dir, attribute);
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prasidduttaAuthor Commented:
@epasquier nice to see you. But one difficulty.

Suppose i have same name file or folder inside drive then it can not replace or create new folder.


i use FileSetAttr(myfolder/file, FILE_ATTRIBUTE_NORMAL);

Open in new window


not success
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
ok, first time I read your last question I didn't understood what you where talking about and had no time to try. I see now, that you say that if the directory already exists then CreateDir will return false and your attributes will not be set
Also , use of ForceDirectories can be useful if the path you want to create has more than 1 level of folder. But it does not solve the problem of the path already existing.

so, I recommend changing the code to this :
...
 for i:=1 to Length(DriveList) do
  begin
   Dir[1]:=DriveList[i];
   if DirectoryExists(Dir) Or CreateDir(Dir) 
    Then FileSetAttr(Dir, attribute);
  end;
end;

Open in new window

0
prasidduttaAuthor Commented:
@epasquier thanks. and i always want something new from you.

i did this code 2 days ago. but difficulty is same.

i have a good example

Create a file in your drive (root) autorun.inf FILE and try to create a folder in same name autorun.inf as FOLDER. If autorun.inf file is exist then you can not create in same named folder. i want to craete a folder in same name forcefully. that mean it can delete that file a create new one.
0
prasidduttaAuthor Commented:
...
  for i:=1 to Length(DriveList) do
  begin
   Dir[1]:=DriveList[i];
   FileSetAttr(Dir , FILE_ATTRIBUTE_NORMAL);
   DeleteFile(Dir); // just add this
   if DirectoryExists(Dir) Or CreateDir(Dir)
    Then FileSetAttr(Dir, attribute);
  end;

Open in new window


@epasquier fixed. thanks alot.
0
prasidduttaAuthor Commented:
Excellent answer.
0
prasidduttaAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

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.