Avatar of prasiddutta
prasidduttaFlag for India

asked on 

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.
Delphi

Avatar of undefined
Last Comment
prasiddutta
Avatar of Morphor
Morphor
Flag of Czechia image

// 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

Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

thanks but error:

see http://screensnapr.com/v/xNQTPX.png
Avatar of Morphor
Morphor
Flag of Czechia image

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;
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

i change but error is same. any other methods?
Avatar of Morphor
Morphor
Flag of Czechia image

How are you exactly using the method? (The way you have implemented it)
Your OS is Windows XP, right?
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

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)
Avatar of Morphor
Morphor
Flag of Czechia image

What exactly is in your TButton1 OnClick method?
Which procedures have you added?
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

@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
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

Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

@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.
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

...
  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.
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

Excellent answer.
Avatar of prasiddutta
prasiddutta
Flag of India image

ASKER

Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo