NAV 2013 find files in folder on client side

jevgjemjevgjem Member Posts: 17
edited 2014-03-18 in NAV Three Tier
Good day.

I'm sitting now with new challenge for me, fighting with this new client-server-db relations.

I need to find all "*.xml" files in folder. Before NAV2013 I have used code like this (FileRec is table "File"):

FileRec.RESET;
SELECTLATESTVERSION;
FileRec.SETFILTER(Path, 'C:\');
FileRec.SETRANGE("Is a file",TRUE);
IF FileRec.FINDFIRST THEN ;

FileRec.SETFILTER(Path, FileFolder);
FileRec.SETRANGE("Is a file",TRUE);
IF FileRec.FINDSET THEN ;
FileRec.SETFILTER(Name, '*.xml|*.XML');
IF FileRec.FINDSET THEN
REPEAT
//some code
UNTIL

But now - I can't make FileRec to look at local client folder? It is searching in NAV server folders... Any easy solutions?
Can't find this on forum. If there is post about this, I will be very grateful for the help to find it.

Comments

  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Try with DotNet assembly
    DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File"

    Dont forget to set RUNONCLIENT property of above variable..
  • jevgjemjevgjem Member Posts: 17
    Hello again.

    I have tried with DotNet, and failed. So I am using automations, but I'm sure that same result can be done with better and more optimized code then mine. If anyone can find time and provide example of code using DotNet, it will be very appreciated.

    Anycase, if someone need fast solution, my code behind:

    variables:
    XMLDoc Automation 'Microsoft XML, v6.0'.DOMDocument
    LautFileSystemObject Automation 'Windows Script Host Object Model'.FileSystemObject
    LautFolder Automation 'Windows Script Host Object Model'.Folder
    LautFiles Automation 'Windows Script Host Object Model'.Files
    LautFile Automation 'Windows Script Host Object Model'.File
    FoldersCol Automation 'Microsoft Scripting Runtime'.Dictionary
    scr Automation 'Microsoft Script Control 1.0'.ScriptControl
    LInt Integer
    txtCode Text 250
    txtCR Text 30

    Code:
    CREATE(LautFileSystemObject, FALSE, TRUE);
    LautFolder := LautFileSystemObject.GetFolder('some folder name');
    LautFiles := LautFolder.Files();

    CREATE(FoldersCol, FALSE, TRUE);
    CREATE(scr, FALSE, TRUE);
    scr.Language := 'VBScript';
    scr.AddObject('FilesByName', LautFiles);
    scr.AddObject('FilesById', FoldersCol);

    //VBScript to make collection of files
    txtCR := ' '; txtCR[1] := 13;
    txtCode :=
    'dim f1' + txtCR +
    'For Each f1 in FilesByName' + txtCR +
    ' FIlesById.Add (FilesById.Count+1), f1' + txtCR +
    'Next' + txtCR;
    scr.ExecuteStatement(txtCode);
    CLEAR(scr);

    //now we can use collection of files in Navision
    FOR LInt := 1 TO FoldersCol.Count() DO BEGIN
    CLEAR(LautFile);
    LautFile := FoldersCol.Item(LInt);

    IF LautFile.Type = 'XML Document' THEN BEGIN
    MESSAGE('%1 - %2',LautFile.Name,LautFile.Type);

    //some code

    END;
    END;
    CLEAR(LautFile);
    CLEAR(LautFiles);
    CLEAR(LautFolder);
    CLEAR(LautFileSystemObject);
  • jevgjemjevgjem Member Posts: 17
    Done. My college - real guru (God bless him) helped me to understand, how it is working.

    Again, task was 1)open folder and find all XML files, 2)do something with this file, 3) move it to another folder.

    Variables
    Name DataType Subtype Length
    DN_Obj DotNet System.Object.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Folder DotNet System.IO.Directory.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Lst DotNet System.Collections.Generic.List`1.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_File DotNet System.IO.File.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Path DotNet System.IO.Path.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    i Integer

    Code
    DN_Obj := DN_Folder.GetFiles('Source folder','*.xml');
    DN_Lst := DN_Lst.List;
    DN_Lst.AddRange(DN_Obj);
    FOR i := 0 TO DN_Lst.Count - 1 DO BEGIN

    //do someting with file

    DN_File.Move(DN_Lst.Item(i),'Destination folder'+'\'+DN_Path.GetFileName(DN_Lst.Item(i)));

    END; //for
  • ara3nara3n Member Posts: 9,256
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • MauddibMauddib Member Posts: 269
    I just tried the solution in post #3 from jevgjem.

    On the classic client it works perfectly. In the 2009 SP 1 RTC however it fails with that dreaded "Compilation of assembly" '....\Codeunit50040' failed. Message.

    The error (CS1503) is that the first argument can not be converted by the Nav.RunTime.NavAutomation.

    The error is coming from the line in code: LautFile := FoldersCol.Item(LInt);

    If I comment this line out and run all the rest of the test code, it works fine.

    What am I doing wrong here? Or is there a better way to get a list of files in a given LOCAL folder without using DotNet (Since its 2009 SP1) and without using the File table (since that only looks at files local to the server not the RTC).
  • suraj92291suraj92291 Member Posts: 49
    jevgjem wrote:
    Done. My college - real guru (God bless him) helped me to understand, how it is working.

    Again, task was 1)open folder and find all XML files, 2)do something with this file, 3) move it to another folder.

    Variables
    Name DataType Subtype Length
    DN_Obj DotNet System.Object.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Folder DotNet System.IO.Directory.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Lst DotNet System.Collections.Generic.List`1.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_File DotNet System.IO.File.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    DN_Path DotNet System.IO.Path.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    i Integer

    Code
    DN_Obj := DN_Folder.GetFiles('Source folder','*.xml');
    DN_Lst := DN_Lst.List;
    DN_Lst.AddRange(DN_Obj);
    FOR i := 0 TO DN_Lst.Count - 1 DO BEGIN

    //do someting with file

    DN_File.Move(DN_Lst.Item(i),'Destination folder'+'\'+DN_Path.GetFileName(DN_Lst.Item(i)));

    END; //for


    Hi,
    I try this in nav2013 but it give me this error "The give path former is not suppored"

    --> DN_File.Move(DN_Lst.Item(i),'D:\TestFolderxmlDestination'+'\'+DN_Path.GetDirectoryName(DN_Lst.Item(i)));
    any solution will be appreciated

    Thank you
    “Any fool can know. The point is to understand.”
    ― Albert Einstein
Sign In or Register to comment.