Options

Check for, Create Directory

mfabianmfabian Member Posts: 187
edited 2005-12-12 in NAV Tips & Tricks
The folowing routine solves the problem:
Does a directory <SearchDirectory> exist? If not, create it.


Var
SearchDirectory : Text200
rtFileSystem : Automation 'Microsoft Scripting Runtime'.FileSystemObject

Begin
SearchDirectory := 'C:\Temp\testdirectory';
CREATE(rtFileSystem);
IF NOT rtFileSystem.FolderExists(SearchDirectory) THEN
rtFileSystem.CreateFolder(SearchDirectory);
end;

Marcus




Marcus Fabian
m.fabian@thenet.ch
+41 79 439 78 72
With best regards from Switzerland

Marcus Fabian

Comments

  • Options
    rwabelrwabel Member Posts: 32
    very useful code, thanks. there is just one problem. if the directory Temp doesn't exists you will get an error. It seems that navision can only handle one directory at the time.
  • Options
    krikikriki Member, Moderator Posts: 9,096
    rwabel wrote:
    very useful code, thanks. there is just one problem. if the directory Temp doesn't exists you will get an error. It seems that navision can only handle one directory at the time.
    Actually, this is not Navision, but Windows.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    krikikriki Member, Moderator Posts: 9,096
    rwabel wrote:
    very useful code, thanks. there is just one problem. if the directory Temp doesn't exists you will get an error. It seems that navision can only handle one directory at the time.
    I changed the code of fabian a little.
    This function recursively creates the whole structure if necessary.
    FUNCTION CreateSubdir(ItxtSubdir : Text[1024])
    // CreateSubdir
    // Creates a subdir if it doesn't exist, it creates the whole path if necessary
    // PARAMETERS:
    //   ItxtSubdir : Full path of directory to create
    
    IF STRLEN(ItxtSubdir) <= 2 THEN // ('X:')
      EXIT;
    
    Lint := STRLEN(ItxtSubdir);
    WHILE (Lint > 0) AND (COPYSTR(ItxtSubdir,Lint,1) <> '\') DO BEGIN
      Lint -= 1;
    END;
    IF Lint = 0 THEN
      EXIT; // no 'X:\' found
    
    CreateSubdir(COPYSTR(ItxtSubdir,1,Lint - 1));
    
    CREATE(LAutFileSystemObject);
    IF NOT LAutFileSystemObject.FolderExists(ItxtSubdir) THEN
      LAutFileSystemObject.CreateFolder(ItxtSubdir);
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    SavatageSavatage Member Posts: 7,142
    You're a code machine \:D/
  • Options
    krikikriki Member, Moderator Posts: 9,096
    Savatage wrote:
    You're a code machine \:D/
    I just have a very good processor with good software where there should be a brain. :mrgreen:
    Shame my parents forgot to order some memory... :(

    PS I send to Luc "How to use recursion in Navision" with some theory, remarks and some basic and Navision examples.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    rwabelrwabel Member Posts: 32
    kriki wrote:
    I changed the code of fabian a little.
    This function recursively creates the whole structure if necessary.
    FUNCTION CreateSubdir(ItxtSubdir : Text[1024])
    // CreateSubdir
    // Creates a subdir if it doesn't exist, it creates the whole path if necessary
    // PARAMETERS:
    //   ItxtSubdir : Full path of directory to create
    
    IF STRLEN(ItxtSubdir) <= 2 THEN // ('X:')
      EXIT;
    
    Lint := STRLEN(ItxtSubdir);
    WHILE (Lint > 0) AND (COPYSTR(ItxtSubdir,Lint,1) <> '\') DO BEGIN
      Lint -= 1;
    END;
    IF Lint = 0 THEN
      EXIT; // no 'X:\' found
    
    CreateSubdir(COPYSTR(ItxtSubdir,1,Lint - 1));
    
    CREATE(LAutFileSystemObject);
    IF NOT LAutFileSystemObject.FolderExists(ItxtSubdir) THEN
      LAutFileSystemObject.CreateFolder(ItxtSubdir);
    

    thanks a lot for the recursive function. But when I use it I get the following error message: "This message is for C/AL programmers: Variable already created."
    When you add
    IF ISCLEAR(LAutFileSystemObject) THEN
    
    before the CREATE it should work fine
  • Options
    krikikriki Member, Moderator Posts: 9,096
    rwabel wrote:
    CREATE(LAutFileSystemObject);
    IF NOT LAutFileSystemObject.FolderExists(ItxtSubdir) THEN
    LAutFileSystemObject.CreateFolder(ItxtSubdir);[/code]

    thanks a lot for the recursive function. But when I use it I get the following error message: "This message is for C/AL programmers: Variable already created."

    Forgot to write this: the variable LAutFileSystemObject must be a local one, not a global.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    rwabelrwabel Member Posts: 32
    kriki wrote:
    rwabel wrote:
    CREATE(LAutFileSystemObject);
    IF NOT LAutFileSystemObject.FolderExists(ItxtSubdir) THEN
    LAutFileSystemObject.CreateFolder(ItxtSubdir);[/code]

    thanks a lot for the recursive function. But when I use it I get the following error message: "This message is for C/AL programmers: Variable already created."

    Forgot to write this: the variable LAutFileSystemObject must be a local one, not a global.

    much better than doing the ISCLEAR stuff :-) thanks for the hint
Sign In or Register to comment.