if folder not exist, create a new

curecure Member Posts: 77
Hi

I'm looking for a function witch can see, if the folder all ready exist. If not, the function has to create this order.

Can you help me?

regards
curdin

Comments

  • AlbertvhAlbertvh Member Posts: 516
    Hi Cure

    set a variable as follows
    Name DataType Subtype Length
    FileSystem Automation 'Microsoft Scripting Runtime'.FileSystemObject

    code to check if exists

    IF ISCLEAR(FileSystem) THEN
    CREATE(FileSystem);

    IF NOT FileSystem.FolderExists(Foldername) THEN
    FileSystem.CreateFolder(Foldername);

    Hope this gets you going :P
  • curecure Member Posts: 77
    Hi Albertvh

    Everythings works fine, but the program has a problem to create the New Folder. Manually I've permission to create a folder.

    The ErrorMessage looks like that
    An exeption was raised in method CreateFolder. The OLE control or Automation server has returned error (HRESULT) -2147352567. The component did not provide the exception description.
    

    Whats wrong? The check, if the folder exists work well. Only create a new folder give me some problems. Is there any other way to create an folder??

    Thank you very much
  • flfl Member Posts: 184
    The mentioned code might give a problem if you want to create several levels at once. Also in Windows/Dos you can't do this.

    example:
    if is missing c:\level1\level2\level3

    then create c:\level1\level\level3 will give a problem if c:\level1\level does not exists.

    You need to do that check in a loop

    test if c:\ exists
    if so
    test if c:\level1 exists
    if not create
    test if c:\level1\level2 exists
    if not create
    test if c:\level1\level2\level3 exists
    if not create
    Then this will work (ALLWAYS) :)
    Francois
    Consultant-Developper

    http://www.CreaChain.com
  • Jascha_VinckeJascha_Vincke Member Posts: 12
    edited 2013-05-22
    Thank you for the information about the Automation Server.

    But I don't like to step through the folder structure to create several subfolders.

    Here is my solution:

    CheckLocalFolder(Folder : Text[250])
    IF ISCLEAR(FileSysObj) THEN
      CREATE(FileSysObj);
    IF NOT FileSysObj.FolderExists(Folder) THEN BEGIN
      BatchFileName := GeneralMgt.AppendBacksl(ENVIRON('tmp'))+'nav_mkdir_batch.cmd';
      IF EXISTS(BatchFileName) THEN BEGIN
        ERASE(BatchFileName);
        YIELD;
      END;
      BatchFile.CREATE(BatchFileName);
      BatchFile.WRITE('mkdir ' + Folder);
      BatchFile.CLOSE;
      YIELD;
      SHELL(BatchFileName);
      ERASE(BatchFileName);
    END;
    

    Works fine :-)


    For the sake of completeness here the used AppendBacksl procedure:
    AppendBacksl(_Input : Text[249]) : Text[250]
    // Check for ending backslash of folder name
    IF _Input = '' THEN
      EXIT('\');
    
    IF NOT (COPYSTR(_Input,STRLEN(_Input)) = '\') THEN
      EXIT(_Input + '\');
    

    hth
    X[1]:=126;X[2]:=67;X[3]:=47;X[4]:=65;X[5]:=76;X[6]:=32;X[7]:=114;X[8]:=111;X[9]:=120;X[10]:=120;X[11]:=120;X[12]:=126;
    FOR I:=1 TO 12 DO S+=X;MESSAGE(S);
  • shraddha_marodkarshraddha_marodkar Member Posts: 2
    Hi,

    Jascha Vincke

    i used ur code to create a folder but variable used
    GeneralMgt.AppendBacksl(ENVIRON('tmp'))+'nav_mkdir_batch.cmd';
    what is that?
    Any codeunit or what?
    Plz reply :)
  • Jascha_VinckeJascha_Vincke Member Posts: 12
    Hi shraddha,


    this is Codeunit 11501 and the method AppendBacksl() is a modified copy of the existing method CheckFolderName(). CheckFolderName() only display a Message. The new function appends a backslash if the string does not contain a backslash. This is more useful instead of a message i think ;-)


    hth

    Jascha
    //edit: We use a german NAV database. I don't know if u have this Codeunit (11501) in your local version. If you look at my previous post you'll see the code for the method AppendBacksl() to add it to an object you prefer.
    X[1]:=126;X[2]:=67;X[3]:=47;X[4]:=65;X[5]:=76;X[6]:=32;X[7]:=114;X[8]:=111;X[9]:=120;X[10]:=120;X[11]:=120;X[12]:=126;
    FOR I:=1 TO 12 DO S+=X;MESSAGE(S);
  • shraddha_marodkarshraddha_marodkar Member Posts: 2
    Hi,

    codeunit 11501 not exist in my DB



    #-o
  • Jascha_VinckeJascha_Vincke Member Posts: 12
    After years my little script stopped working some day because computers compute way more faster as in the days I created my CheckLocalFolder() method.

    So here comes the update:

    Add SLEEP(250); two times!
    Before SHELL(BatchFileName);
    And before ERASE(BatchFileName);.

    waaa *sleeeeep* \:D/
    X[1]:=126;X[2]:=67;X[3]:=47;X[4]:=65;X[5]:=76;X[6]:=32;X[7]:=114;X[8]:=111;X[9]:=120;X[10]:=120;X[11]:=120;X[12]:=126;
    FOR I:=1 TO 12 DO S+=X;MESSAGE(S);
Sign In or Register to comment.