Author Topic: Recursive file search  (Read 23523 times)

Suncatcher

  • Active Member
  • ***
  • Posts: 104
    • View Profile
Recursive file search
« on: February 17, 2016, 14:46:50 »
I have need for script which would find all *dsl and *.lsd files in directory including subdirectories and select those folders which contain them. I wrote this script which, however, doesn't work.
Code: [Select]
@var $file = FindFiles("D:\downloads\Dicts_unpacked\\*");

@var $n;
for( $n = 0; $n < arrayCount($file); $n++)
{
  @var $is = IsFolder($file[n]);
  if($is == 1)
  {

    @var $infiles = FindFiles($file[n] ^ "*.lsd");
if(arrayCount($infiles) > 0)
       {
         @var $num = SetSourceSelected( $file[n] , 1);
        }

    @var $infiles = FindFiles($file[n] ^ "*.dsl");
if(arrayCount($infiles) > 0)
       {
         @var $num = SetSourceSelected( $file[n] , 1);
        }
  }
}


I get error on the 4th line: Script engine error - Line : 4, Error : -1 => Code : "$n = IsFolder($file[n])"
However, if I hard code this line, e.g IsFolder($file[0])) the IsFolder function works smoothly.
What am I doing wrong? Maybe, I misunderstand some fundamental concept?

P.S. Is there any function for recursive file search?

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4484
    • View Profile
    • Multi Commander
Re: Recursive file search
« Reply #1 on: February 17, 2016, 16:36:55 »
Code: [Select]
  @var $is = IsFolder($file[n]);

you are missing a $ before the "n"
try
  @var $is = IsFolder($file[$n]);

same error in the other places where $n is accessed

P.S. Is there any function for recursive file search?
No, you have to do it your self in the script.
« Last Edit: February 17, 2016, 16:39:52 by Mathias (Author) »

Suncatcher

  • Active Member
  • ***
  • Posts: 104
    • View Profile
Re: Recursive file search
« Reply #2 on: February 17, 2016, 21:26:24 »
Thank you, it was a dumb mistake, and finally I was able to fulfill my task.
I don't know whether it will be useful for anybody (selection of folders based on filetypes they contain), but I will just put the script here.
Code: [Select]
@var $file = FindFiles("D:\some\path\\*");

@var $n;
for( $n = 0; $n < arrayCount($file); $n++)
{
  @var $is = IsFolder($file[$n]);
  if($is == 1)
  {

    @var $inlsdfiles = FindFiles($file[$n] ^ "*.lsd");
    @var $indslfiles = FindFiles($file[$n] ^ "*.dsl");

if(arrayCount($inlsdfiles) > 0)
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 0);

        }

       if(arrayCount($indslfiles) > 0)
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 0);

       }
  }
}

Maybe, it could be done in a more beautiful way, but the clumsiness of this script language reads off the scale. I had a choice between Batch script, Bash script and Your script during evaluating this task, and I assume, I made the wrong choice  :)
« Last Edit: February 17, 2016, 21:48:42 by Suncatcher »

Suncatcher

  • Active Member
  • ***
  • Posts: 104
    • View Profile
Re: Recursive file search
« Reply #3 on: February 17, 2016, 21:44:28 »
Several other critical points that should be emphasized:

1. No complex conditions can be built in IF clause.
Code: [Select]
if(A == B OR (B == C AND D == E) ) {}Or I am mistaken? I didn't find anything in documentation.

2. The behavior of script interpreter with IF clause is undefined. One time it threw the exception with totally correct piece which is on the picture 1.
Another time it behaved itself totally unexpectedly with this piece of code.

Code: [Select]
if(arrayCount($inlsdfiles) == 0)
       {
        }
       else if(arrayCount($indslfiles) == 0)
       {
        }
       else
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 1);

       }

During rendering this piece, having found false condition in main IF it skipped all other ELSE clauses and proceeded to the next loop iteration.

3. The collation of the real directories differs from the collation of array that FindFiles() function returns as result (see picture 2). And that's awkward.

4. The arrays cannot be initialized as empty ones. E.g.
Code: [Select]
@var $arr = {};Even if you write like this it inserts empty array member.

5. The arrays cannot be initialized with variables. E.g.
Code: [Select]
@var $arr = {$ini[$n], $ini[$n+1], $ini[$n+2], ...};Only with literals and it's poor.

So, the script language, as well as debugger, is still very raw for real work. IMHO.
« Last Edit: February 17, 2016, 21:51:44 by Suncatcher »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4484
    • View Profile
    • Multi Commander
Re: Recursive file search
« Reply #4 on: February 17, 2016, 22:06:21 »
Thank you, it was a dumb mistake, and finally I was able to fulfill my task.
I don't know whether it will be useful for anybody (selection of folders based on filetypes they contain), but I will just put the script here.
Code: [Select]
@var $file = FindFiles("D:\some\path\\*");

@var $n;
for( $n = 0; $n < arrayCount($file); $n++)
{
  @var $is = IsFolder($file[$n]);
  if($is == 1)
  {

    @var $inlsdfiles = FindFiles($file[$n] ^ "*.lsd");
    @var $indslfiles = FindFiles($file[$n] ^ "*.dsl");

if(arrayCount($inlsdfiles) > 0)
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 0);

        }

       if(arrayCount($indslfiles) > 0)
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 0);

       }
  }
}

Maybe, it could be done in a more beautiful way, but the clumsiness of this script language reads off the scale. I had a choice between Batch script, Bash script and Your script during evaluating this task, and I assume, I made the wrong choice  :)

Batch would be messier and you can't do selection from that..
But you can split it up into functions making it cleaner..

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4484
    • View Profile
    • Multi Commander
Re: Recursive file search
« Reply #5 on: February 17, 2016, 22:17:20 »
Several other critical points that should be emphasized:

1. No complex conditions can be built in IF clause.
Code: [Select]
if(A == B OR (B == C AND D == E) ) {}Or I am mistaken? I didn't find anything in documentation.
No that is not included yet. I have not had a need for that yet. So it has not been added.
But you can simulate it with script functions. ( like ADD(..)  and OR(..) that return 0/1 )

2. The behavior of script interpreter with IF clause is undefined. One time it threw the exception with totally correct piece which is on the picture 1.
Another time it behaved itself totally unexpectedly with this piece of code.

Code: [Select]
if(arrayCount($inlsdfiles) == 0)
       {
        }
       else if(arrayCount($indslfiles) == 0)
       {
        }
       else
       {
         @var $arr[] = PathGetParts($file[$n]);
         @var $path[] = {};
         arrayAdd($path, $arr[2]);
         @var $num = SetSourceSelected($path, 1);

       }

During rendering this piece, having found false condition in main IF it skipped all other ELSE clauses and proceeded to the next loop iteration.
The script engine simple.. it is line based..  it is explained somewhere..
So do not compress rows
doing
Code: [Select]
{  $var = 33;might fail. ( I think , did not test)
instead do
Code: [Select]
{
 $var = 3;



3. The collation of the real directories differs from the collation of array that FindFiles() function returns as result (see picture 2). And that's awkward.
the script call do not sort it, It returns as Windows returns it.  The view is sorted depending on your settings


4. The arrays cannot be initialized as empty ones. E.g.
Code: [Select]
@var $arr = {};Even if you write like this it inserts empty array member.
If empty there is no need to init it. But will check.

5. The arrays cannot be initialized with variables. E.g.
Code: [Select]
@var $arr = {$ini[$n], $ini[$n+1], $ini[$n+2], ...};Only with literals and it's poor.
Now you are just trying to break it.

Avoid exotic syntaxes, Don't do to much at once.. MultiScript engine support a lot but it is still a very simple script engine.


So, the script language, as well as debugger, is still very raw for real work. IMHO.
It is meant for small script. Not for large applications/tools.  There are a real API for that..
I got some large advanced script my self.. So I use it for real work. So it is good enough for that.  could it be better ? Ofcouse. And it will be .. If time allows..

« Last Edit: February 17, 2016, 22:40:02 by Mathias (Author) »

Suncatcher

  • Active Member
  • ***
  • Posts: 104
    • View Profile
Re: Recursive file search
« Reply #6 on: February 18, 2016, 07:54:40 »
Nevertheless, thank you for your job! :) I just wanted to highlight ways of improvement, not to abuse.
The Commander itself is above all praise.
« Last Edit: February 18, 2016, 07:56:25 by Suncatcher »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4484
    • View Profile
    • Multi Commander
Re: Recursive file search
« Reply #7 on: February 18, 2016, 17:59:42 »
I do not like to add stuff just because it would be nice to have.
There must be a real need. I have limited time so I need to prioritize things that are really needed.

So if you find something that you really need, like a script function that does X, Then I might add it.