Solved

Folder Creating

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 100
Dev Express grid collapse 2 45
Unique identifier on a terminal server (rdp) 4 57
Dev express lookupcombo 3 30
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

825 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