Filtering the File Record

NaviDevNaviDev Member Posts: 365
Hello,

I am trying to retrieve the TXT files using the File record. In the directory I have this files :

Sales.txt
Purchase.txt
_Sales.txt
_Purchase.txt


But I want to exclude those TXT files with "_" in the begining of the filename. So I have put in my code this :
rFile.RESET;
rFile.SETRANGE(Path, 'c:\');
rFile.SETRANGE("Is a File", TRUE);
rFile.SETFILTER(Name, '%1|%2', '*.txt', '<>_*.txt');
IF rFile.FIND('-') THEN REPEAT
  MESSAGE(rFile.Name);
UNTIL rFile.NEXT = 0;

But somehow in the results, it still includes the files with "_" in the beginning of their filenames. ](*,)
Please hand me some suggestions. Thanks in advance.
Navision noob....

Comments

  • BeliasBelias Member Posts: 2,998
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • NaviDevNaviDev Member Posts: 365
    Thanks Belias for the response. Too bad, I am using non Sql db with Nav 5.0. Is there any I can do with this. ](*,)
    Navision noob....
  • reijermolenaarreijermolenaar Member Posts: 256
    Hi,

    Try this:
    rFile.SETFILTER(Name, '''*.txt''&<>''_*.txt''');
    
    Regards,
    Reijer
    Reijer Molenaar
    Object Manager
  • NaviDevNaviDev Member Posts: 365
    Hello reijermolenaar,

    I tried your inputs but still with the same results... Still includes the files with "_" in the begining of their filenames.
    Navision noob....
  • Timo_LässerTimo_Lässer Member Posts: 481
    Just try this:
    rFile.RESET;
    rFile.SETRANGE(Path, 'c:\');
    rFile.SETRANGE("Is a File", TRUE);
    rFile.SETFILTER(Name, '%1', '*.txt'); // <--
    IF rFile.FIND('-') THEN REPEAT
      IF rFile.Name[1] <> '_' THEN        // <--
        MESSAGE(rFile.Name);
    UNTIL rFile.NEXT = 0;
    
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • reijermolenaarreijermolenaar Member Posts: 256
    That is really strange. :-k
    It works ok in my client.
    Reijer Molenaar
    Object Manager
  • NaviDevNaviDev Member Posts: 365
    @reijermolenaar
    I think you are using Sql db.

    @Timo
    Many Thanks for the suggestion. I know I can do it that way with the help of function COPYSTR. But the scenario is much more complicated actually. I have a setup wherein user can put what are the filenames to include and exclude in the output. So I cannot put my code that way.

    My code is like this :
    ExcludeFile1 := '_*.txt';
    ExcludeFile2 := '*_*.txt';
    ExcludeFile3 := '*1234*.txt';
    ExcludeFile4 := '*8765*.txt';
    ExcludeFile5 := '*+_)('.txt';
    
    ExludeFiles := '<>' + ExcludeFile1 + '|<>' + ExcludeFile2 + '|<>' + ExcludeFile3 + '|<>' + ExcludeFile4 + '|<>' + ExcludeFile5;
    
    rFile.RESET;
    rFile.SETRANGE(Path, 'c:\');
    rFile.SETRANGE("Is a File", TRUE);
    rFile.SETFILTER(Name, '%1|%2', '*.txt', ExludeFiles);
    IF rFile.FIND('-') THEN REPEAT
      MESSAGE(rFile.Name);
    UNTIL rFile.NEXT = 0;
    

    Any suggestion will be greatly appreciated. Thanks again in advance.
    Navision noob....
  • reijermolenaarreijermolenaar Member Posts: 256
    Yep, you are right, I should have read you post better…
    I thought that the problem was lying in the fact that you were using a `|`-sign instead of a `&`-sign.
    But the problem is that the filter `<>_*` is not working with a native db.
    Reijer Molenaar
    Object Manager
  • NaviDevNaviDev Member Posts: 365
    @reijermolenaar
    I have test your codes in SQL db and its working.... but too bad I am using Native db :cry:.

    Anyways... I am trying to do it also in sql. But my code now is like the codes In my last post. How can I apply your code in that codes? Thanks again in advance.
    Navision noob....
  • BeliasBelias Member Posts: 2,998
    NaviDev wrote:
    @reijermolenaar
    I think you are using Sql db.

    @Timo
    Many Thanks for the suggestion. I know I can do it that way with the help of function COPYSTR. But the scenario is much more complicated actually. I have a setup wherein user can put what are the filenames to include and exclude in the output. So I cannot put my code that way.

    My code is like this :
    ExcludeFile1 := '_*.txt';
    ExcludeFile2 := '*_*.txt';
    ExcludeFile3 := '*1234*.txt';
    ExcludeFile4 := '*8765*.txt';
    ExcludeFile5 := '*+_)('.txt';
    
    ExludeFiles := '<>' + ExcludeFile1 + '|<>' + ExcludeFile2 + '|<>' + ExcludeFile3 + '|<>' + ExcludeFile4 + '|<>' + ExcludeFile5;
    
    rFile.RESET;
    rFile.SETRANGE(Path, 'c:\');
    rFile.SETRANGE("Is a File", TRUE);
    rFile.SETFILTER(Name, '%1|%2', '*.txt', ExludeFiles);
    IF rFile.FIND('-') THEN REPEAT
      MESSAGE(rFile.Name);
    UNTIL rFile.NEXT = 0;
    

    Any suggestion will be greatly appreciated. Thanks again in advance.

    I cannot understand what this code should do...
    Are you applying a filter like this?
    rFile.SETFILTER(Name, '%1|%2', '*.txt', '<>a.txt|<>b.txt');
    
    (this is an example). In this case i expect to retrieve every file in the folder...the name of a file will ever be different from a.txt or from b.txt... :-k
    if so, do you know that "|" is "OR"? and "&" is "AND"
    or maybe i'm missing something... :-k :-k :-k :-k :-k
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • NaviDevNaviDev Member Posts: 365
    Hmmm...
    Yeah I think my bad coding and analysis also... :oops:
    I didnt consider the operator..
    Belias and reijermolenaar are right. Operator should be '&' not '|'.

    Anyways what I want to do is to retrieve all the files '*.txt' in the directory but exclude those files that are in the variables ExcludeFile1 - 5 (from my code).
    Navision noob....
  • reijermolenaarreijermolenaar Member Posts: 256
    You can do something like this:
    IncludeFilter := '*.txt';
    ExcludeFilter := '_*.txt';
    
    rFile.SETRANGE(Path, 'c:\test\');
    rFile.SETRANGE("Is a file", TRUE);
    rFile.SETFILTER(Name, IncludeFilter);
    
    rFile2.SETRANGE(Path, 'c:\test\');
    rFile2.SETRANGE("Is a file", TRUE);
    rFile2.SETFILTER(Name, ExcludeFilter);
    
    IF rFile.FINDSET THEN
      REPEAT
        rFile2 := rFile;
        IF NOT rFile2.FIND('=') THEN
          MESSAGE(rFile.Name);
      UNTIL rFile.NEXT = 0;
    
    Reijer Molenaar
    Object Manager
  • NaviDevNaviDev Member Posts: 365
    Hello reijermolenaar,

    Thanks for the inputs. I already manage to do it, as the way you did it.
    Navision noob....
Sign In or Register to comment.