Solved

Folder Creating

Posted on 2003-11-03
4
177 Views
Last Modified: 2010-04-05
Hey,

I want to create a folder on runtime and if the folder exists then I want it to say "Safe Activated" or if it doesnt exist I want it to say "Creating Safe".

Heres my code but on runtime all it says is Creating Safe and when I run the program again it says Creating Safe and then when I go to find the folder its not there.

Heres my code ...

Code:
--------------------------------------------------------------------------------

procedure TForm1.FormCreate(Sender: TObject);beginif DirectoryExists('c:\Windows\datafile\25672567\safe')thenshowmessage (' Safe Activated ')elseshowmessage (' Creating safe ');CreateDir ('c:\Windows\datafile\25672567\safe')end;  

--------------------------------------------------------------------------------


Cheers,

Computer Programer
0
Comment
Question by:computerprogramer
  • 3
4 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9676865
computerprogramer,
  You need to create ForceDirectories instead of create dir.
"ForceDirectories creates a new directory as specified in Dir, which must be a fully-qualified path name. If the directories given in the path do not yet exist, ForceDirectories attempts to create them."

...Shu
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9676877
So,,y, I meant you need to use ForceDirectories instead of createdir.
Also, you could add a begin  end around the else part: create a directory only if it does not exist like:
procedure TForm1.FormCreate(Sender: TObject);
begin
  if DirectoryExists('c:\Windows\datafile\25672567\safe') then
    showmessage(' Safe Activated ')
  else
  begin//create directory only if it does not exist.
    showmessage(' Creating safe ');
    ForceDirectories('c:\Windows\datafile\25672567\safe');
  end;

end;

Cheers!
...Shu
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 20 total points
ID: 9676886
And, a final check for whether the directory was actually created:
procedure TForm1.FormCreate(Sender: TObject);
begin
  if DirectoryExists('c:\Windows\datafile\25672567\safe') then
    showmessage(' Safe Activated ')
  else
  begin//create directory only if it does not exist.
    If ForceDirectories('c:\Windows\datafile\25672567\safe') then
      showmessage(' Created safe ')
    else
      showmessage('!!! Create failed !!!');
  end;

end;

HTH
:-)
...Shu
0
 

Expert Comment

by:Kritsadapoj
ID: 9677492
Hi Above ....

Function Search  Directory (Copy For Use)

Function Find_File_and_Subdirectory( Dir : String );
var FindFileData : TWin32FindData;
SearchHandle : THandle;
begin
SearchHandle := FindFirstFile( PChar( Dir+'\*.*' ), FindFileData );
If SearchHandle <> INVALID_HANDLE_VALUE then
Repeat
If FindFileData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
Memo1.Lines.Add( '<'+FindFileData.cFileName+'>' )
else
Memo1.Lines.Add( FindFileData.cFileName );
Until Not FindNextFile(SearchHandle,FindFileData);
end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now