Difference between revisions of "TFile.Exists"

From Soldat Community Wiki
Jump to: navigation, search
(Created page with " '''''function Exists(const Source: string): Boolean''''' Source: a path to a file to check access for Result: Whenever file exists or not ==Description== This function ch...")
 
(Example)
Line 9: Line 9:
 
procedure OnPlayerCommand(Player: TActivePlayer; Text: string);
 
procedure OnPlayerCommand(Player: TActivePlayer; Text: string);
 
begin
 
begin
   // Assuming config Sandboxed=1 or 0
+
   // Assuming config Sandboxed=2 (default)
 
   Delete(Text, 1, 1); // remove command's slash
 
   Delete(Text, 1, 1); // remove command's slash
 
   try
 
   try
     if FileExists('maps/' + Text) then
+
     if FileExists('~/' + Text + '.ini') then
       Player.WriteConsole('Yup, that map exists', $FFFFFFFF);
+
       Player.WriteConsole('Yup, config for that map exists', $FFFFFFFF);
 
     else
 
     else
       Player.WriteConsole('Sorry, this map doesn''t exist', $FFFFFFFF);
+
       Player.WriteConsole('Sorry, this config for this map doesn''t exist', $FFFFFFFF);
 
   except     
 
   except     
 
     Player.WriteConsole('Wrong path (you added ../ a few times, didn''t you?)', $FFFFFFFF);
 
     Player.WriteConsole('Wrong path (you added ../ a few times, didn''t you?)', $FFFFFFFF);

Revision as of 15:36, 26 July 2013

function Exists(const Source: string): Boolean
 Source: a path to a file to check access for
 Result: Whenever file exists or not

Description

This function checks whenever file pointed by Source exists on hard drive. If the path is out of script's sandbox, EAccessViolation exception is thrown.

Example

procedure OnPlayerCommand(Player: TActivePlayer; Text: string);
begin
  // Assuming config Sandboxed=2 (default)
  Delete(Text, 1, 1); // remove command's slash
  try
    if FileExists('~/' + Text + '.ini') then
      Player.WriteConsole('Yup, config for that map exists', $FFFFFFFF);
    else
      Player.WriteConsole('Sorry, this config for this map doesn''t exist', $FFFFFFFF);
  except    
    Player.WriteConsole('Wrong path (you added ../ a few times, didn''t you?)', $FFFFFFFF);
  end;
end;