Recent

Author Topic: [SOLVED] Quick bit of help please  (Read 2026 times)

SteveSS

  • New Member
  • *
  • Posts: 43
[SOLVED] Quick bit of help please
« on: December 21, 2018, 12:07:43 pm »
Hi All

I need to create a directory in my program.  The following code:

  if directoryexists('C:\LData\CSV\' + inttostr(year)) = false then       // already created new directory?
  begin
    if forcedirectory('C:\LData\CSV\' + inttostr(year)) = false then      // no - so create it
    begin
      showmessage('Cannot create new directory');
    end;
  end;

give an error "identifier not found : forcedirectory".  I have fileutil in my uses section and can't figure out what the problem is.   

Steve
         
« Last Edit: December 21, 2018, 12:27:45 pm by SteveSS »

fred

  • Full Member
  • ***
  • Posts: 202
Re: Quick bit of help please
« Reply #1 on: December 21, 2018, 12:14:58 pm »
I use ForceDirectories from sysutils

Edit:

It seems that ForceDirectory is in LazFileUtils
« Last Edit: December 21, 2018, 12:21:03 pm by fred »

SteveSS

  • New Member
  • *
  • Posts: 43
Re: Quick bit of help please
« Reply #2 on: December 21, 2018, 12:27:04 pm »
Thanks very much Fred.  Added LazFileUtils to uses and now compiles without problem.  A bit confusing because the FileUtil documentation says that ForceDirectory is included.

Steve

wp

  • Hero Member
  • *****
  • Posts: 13268
Re: Quick bit of help please
« Reply #3 on: December 21, 2018, 12:33:21 pm »
A few comments on your code: It is not required to check the existence of the new directory before ForceDirectory is called - this is what the "force" is all about. When ForceDirectory returns true you can be sure that the directory exists now, either it was created anew or it already had existed. Only when ForceDirectory returns false something must be very wrong: no disk in drive, write-protected, drive does not exist etc.

I would not use a statement like "if ForceDirectory(..) = false" Because "if" requires a boolean variable to decide how to continue, and ForceDirectory just returns that, and this is enough. Use the operator "not" to invert the boolean result. This improves readability of the code - it is one of the biggest advantages or Pascal that source code almost can be read like a book:

Code: Pascal  [Select][+][-]
  1.  if not ForceDirectory('C:\LData\CSV' + IntToStr(year)) then
  2.     ShowMessage('Cannot create new directory');

dsiders

  • Hero Member
  • *****
  • Posts: 1496
Re: Quick bit of help please
« Reply #4 on: December 21, 2018, 12:57:11 pm »
Added LazFileUtils to uses and now compiles without problem.  A bit confusing because the FileUtil documentation says that ForceDirectory is included.

Hello Steve.

I'm not sure what you're looking at, but the CHM Help files for 1.8.2 have it in the lazfileutils.pas file.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

 

TinyPortal © 2005-2018