Solved

Folder Creating

Posted on 2003-11-03
4
175 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

20 Experts available now in Live!

Get 1:1 Help Now