READ MULTIPLE TXT FILES FROM A DIRECTORY

leugimleugim Member Posts: 93
hi all, some help is needed

one of our customers has a software which creates a file in a predefined path periodically (i. e. every 5 mins)
the name of the files follows the rule: txtFileName_yymmdd_hhmmss
we thougth to create a new directory for "processed files", to copy there all txt files processed in Nav. the processed files will be deleted from original path, in order to have here as less files as possible
we are looking for some code to read the name of the files in a directory from within Navision with some report or loop code from a codeunit, but we don't know how to return the name of the files, because never two files will be named equally due to date & time..

hope you understand what we want to explain...
regards
_______________
so far, so good

Answers

  • Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
    Hi

    In NAV exists a virtual table "File"


    WITH recFile DO BEGIN
    RESET;
    SETRANGE(Path,strPath);
    SETRANGE("Is a file",TRUE);
    IF FIND('-') THEN
    REPEAT
    MESSAGE(Name);
    UNTIL NEXT = 0;
    END;
  • leugimleugim Member Posts: 93
    wow... perfect!! that's what we were looking for

    THAAAAAAAAAAANKS A LOT!!! \:D/

    regards
    _______________
    so far, so good
  • maanmuslehmaanmusleh Member Posts: 7
    Does the virtual Table File exist in NAV 4.0???
  • ara3nara3n Member Posts: 9,256
    yes, it existed for a while now. Probably from the beginning.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • JPHSCJPHSC Member Posts: 67
    Another way to do this is the next :
    Since Navision can't loop trough all the files in a folder the filenames are stored in a 'Microsoft Scripting Runtime'.Dictionary ( the FilesCol object), using the VBScript.
    This vbScript gives the dictionary an integer and text value. The integer to loop, the text is the filename.
    I found the vbScript here on Mibuso to loop trough folders : http://www.mibuso.com/forum/viewtopic.php?f=5&t=7855&hilit=folders

    Just to know there are others ways ...

    Variables are : ( all of type automation )
    FilesCol : 'Microsoft Scripting Runtime'.Dictionary
    Script : 'Microsoft Script Control 1.0'.ScriptControl
    WHSFileSystemObject : 'Windows Script Host Object Model'.FileSystemObject
    WHSFolder : 'Windows Script Host Object Model'.Folder
    WHSFile : 'Windows Script Host Object Model'.File
    IF ISCLEAR(WHSFileSystemObject) THEN
      CREATE(WHSFileSystemObject);
    
    // If the folder does not exist, exit
    IF NOT WHSFileSystemObject.FolderExists(ltxtFolder) THEN BEGIN
       CLEAR(WHSFileSystemObject);
       EXIT;
    END;
    
    WHSFolder := WHSFileSystemObject.GetFolder(ltxtFolder);
    WHSFiles := WHSFolder.Files;
    
    IF ISCLEAR(FilesCol) THEN
      CREATE(FilesCol);
    
    // ========================================================================
    // Use a VBScript to fill a 'Microsoft Scripting Runtime'.Dictionary
      IF ISCLEAR(Script) THEN
        CREATE(Script);
    
      Script.Language := 'VBScript';
      //variable LautFolders is visible in script as FoldersByName 
      Script.AddObject('FilesByName', WHSFiles);
      //variable FoldersCol is visible in script as FoldersById 
      Script.AddObject('FilesById', FilesCol);
    
      //copy objects using VBScript 
      txtCR := ''; 
      txtCR[1] := 13;
      txtCode := 
         'dim f1' + txtCR +
         'For Each f1 in FilesByName' + txtCR +
           ' FilesById.Add (FilesById.Count+1), f1' + txtCR +
         'Next' + txtCR;
      Script.ExecuteStatement(txtCode);
      CLEAR(Script);
    // ========================================================================
    
    
    FOR i := 1 TO FilesCol.Count() DO BEGIN
      WHSFile := FilesCol.Item(i);
      ltxtFileName := WHSFolder.Path + '\' + WHSFile.Name;
      // ==========================
      // Here you do what you want to do with the file... 
      // ==========================
    END;
    
    CLEAR(FilesCol);
    CLEAR(WHSFileSystemObject);
    
Sign In or Register to comment.