Solved

Folder Creating

Posted on 2003-11-03
4
183 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
[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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

740 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