?
Solved

Create folder each Drive in Delphi

Posted on 2011-10-02
14
Medium Priority
?
549 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:prasiddutta
  • 8
  • 4
  • 2
14 Comments
 
LVL 3

Expert Comment

by:Morphor
ID: 36899118
// 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
 

Author Comment

by:prasiddutta
ID: 36899128
thanks but error:

see http://screensnapr.com/v/xNQTPX.png
0
 
LVL 3

Expert Comment

by:Morphor
ID: 36899145
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
Independent Software Vendors: 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!

 

Author Comment

by:prasiddutta
ID: 36899164
i change but error is same. any other methods?
0
 
LVL 3

Expert Comment

by:Morphor
ID: 36899173
How are you exactly using the method? (The way you have implemented it)
Your OS is Windows XP, right?
0
 

Author Comment

by:prasiddutta
ID: 36899178
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
 
LVL 3

Expert Comment

by:Morphor
ID: 36899179
What exactly is in your TButton1 OnClick method?
Which procedures have you added?
0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 36901920
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
 

Author Comment

by:prasiddutta
ID: 36904139
@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
 
LVL 25

Expert Comment

by:epasquier
ID: 36935700
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
 

Author Comment

by:prasiddutta
ID: 36935732
@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
 

Author Comment

by:prasiddutta
ID: 36935784
...
  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
 

Author Closing Comment

by:prasiddutta
ID: 36935786
Excellent answer.
0
 

Author Comment

by:prasiddutta
ID: 36935787
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

862 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