Author Topic: Search routine  (Read 68563 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Search routine
« on: July 26, 2013, 19:11:46 »
Decided to attempt a script  -
I decided to try to script a button to search for todays jpgs

Code: [Select]
MC.FileSearch.Search SEARCHIN="${sourcepath}" SEARCHFOR="*.jpg"
This will get all jpgs in the active pane.  Date & tree-depth aren't pre-loaded.
What I wish to add is the search dialog parameters
a) for today from 00:00:00.
b) to search x subfolder levels.

If it is possible to send values to the search dialog I assume I will need something like
Code: [Select]
@var $t = GetTime();Then I need to import that & ignore hours/minutes values.
can I EXCLUDE= non $t values or StrIsEqual the 't' to select date matches?

Then I'd want to poke that into the search dialog settings....

That's as far as I have got & it shows how little I have of the language logic at the moment.

But if there was a saved/favorite search function I guess I wouldn't need this :)


Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #1 on: July 26, 2013, 19:51:19 »
Actually you are out of luck :)

The Custom Command for FileSearch do not support date/time matches to be added. (It should, I must have forgot to add it, so I will add it for the next version)

You can load/save searches in the FindFiles Dialog with Ctrl+F1 => F10 (save) and F1 => 10 (Load)
But it will not "reset" the now time for time/date filters so it might not help you completely.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #2 on: July 26, 2013, 23:22:34 »
Guess I'm not really surprised I decided to try the impossible, & so it's back to the drawing board for script ideas.... ;D

Good to know about the F1 - F10 feature I hadn't realized that was in there.  That will be useful. Thanks.

How many other features are there I don't know about, I wonder?  :P
« Last Edit: July 26, 2013, 23:25:13 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #3 on: July 28, 2013, 16:10:59 »
latest Beta Build 1442 has better TIME support for custom command MC.FileSearch.Search

Example

Code: [Select]
MC.FileSearch.Search SEARCHIN="${sourcepath}" SEARCHFOR="*.jpg" TIME="TODAY"
// OR
MC.FileSearch.Search SEARCHIN="${sourcepath}" SEARCHFOR="*.jpg" TIMEFROM="2013-01-01 10:00:00" TIMETO="2013-03-04 12:00:00"

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #4 on: July 29, 2013, 17:20:27 »
Thanks for the info, I will need to try this out!   ;)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #5 on: July 30, 2013, 19:20:25 »
Continuing my rename & SAR experiments, I'm wondering about regex to MC script transposition.
e.g.
Number & space to number & '.'
((?:[0-9]+)+)\
\1.

I'm stealing Jungles earlier code & using a . :-
$n = StrRegExpFind( $s, ".?\d+$" );

& I've just realized I appear to need to define a button to run this or is there a field I'm missing/misusing that allows me to run a script on a selected file/folder.  Is there a script input window & if not, can I have one to play with in a future build?  :D

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #6 on: July 30, 2013, 21:54:15 »
Continuing my rename & SAR experiments, I'm wondering about regex to MC script transposition.
e.g.
Number & space to number & '.'
((?:[0-9]+)+)\
\1.

I'm stealing Jungles earlier code & using a . :-
$n = StrRegExpFind( $s, ".?\d+$" );
?.. in regexp \d means any digit.. or what are you asking ?

& I've just realized I appear to need to define a button to run this or is there a field I'm missing/misusing that allows me to run a script on a selected file/folder.  Is there a script input window & if not, can I have one to play with in a future build?  :D
I don't understand..  You can assign a HotKey , Menu button . Button Panel Button and more for any script command you create.. there are 3 buttons to the right in the User command.  where you create the command.  you can not miss it . :)


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #7 on: July 31, 2013, 13:05:59 »
Thanks for responding Mathias.

I was thinking my regex would be replaced by the modified string by Jungle.  But I don't think it targets spaces.
ie
1 2 3 4
to
1.2.3.4

I wanted to test it & edit (as with a sandbox), before creating a button (that didn't work several times!) - short on time... 
Just wanted to select a file/folder, run the script & if good then assign it a button.  Maybe the debugger does this & I haven't figured out how?

I thought there was something like a script test window, (maybe I recall something like that from XYplorer which I tried years ago?)
But I have just looked at my toolbar - the User Commands item shown on MC homepage is not visible!  ???  See attached
So maybe I am missing it!!  :)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #8 on: July 31, 2013, 13:43:54 »
Thanks for responding Mathias.

I was thinking my regex would be replaced by the modified string by Jungle.  But I don't think it targets spaces.
ie
1 2 3 4
to
1.2.3.4
Not sure about regexp.  But if you want to replace all space with "." then you can do

$s = StrReplace( $s, " " , "." );

I wanted to test it & edit (as with a sandbox), before creating a button (that didn't work several times!) - short on time... 
Just wanted to select a file/folder, run the script & if good then assign it a button.  Maybe the debugger does this & I haven't figured out how?
You have the MultiScript debugger under the help menu.

But I have just looked at my toolbar - the User Commands item shown on MC homepage is not visible!  ???  See attached
(Think you forgot to upload the attachment.)
The images on the webpage can be old. the UI might have updated.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #9 on: July 31, 2013, 18:53:19 »
Thanks Mathias
The screen of my tool bar is posted - it shows the 'user commands' menu element is absent on my UI.  Can you not see it?

How do I make the debugger run a script on a file/folder to test the script? 
I need a window that allows me to run a script 'live' if you see what I mean. 

If I understand correctly I must -
  • create a button,
  • test it,
  • then, if wrong, edit it.
 

That is a lot of clicking around the dialogs so I would like a sandbox. (If it doesn't already exist in some form).

If I understand your advice, my script should be

$s = StrReplace( $s, " " , "." ?\d);

But it doesn't seem to work..  :-[



Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 537
  • Old Skull
    • View Profile
Re: Search routine
« Reply #10 on: July 31, 2013, 19:31:37 »
Ulfhednar
Custom commands can be created via Configuration menu.
Then you may open multi-script debugger from the Help menu and choose your command in the dropdown list.

To perform some action on a file/folder you should get its path first by using appropriate functions. They are described in on-line doc.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #11 on: July 31, 2013, 21:26:20 »
Thanks Mathias
The screen of my tool bar is posted - it shows the 'user commands' menu element is absent on my UI.  Can you not see it?
My browser in my phone was acting up..

If I understand your advice, my script should be

$s = StrReplace( $s, " " , "." ?\d);
If you want to use Regular Expression then use StrRegExpFind, If you want to use a simple Replace substring/character you StrReplace.
You can not use regular expressing in StrReplace.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #12 on: July 31, 2013, 23:26:34 »
Thanks Jungle & Mathias, that helps.
I see I will have to pre-define my targets etc now.

It is going to take a while for me to grasp this & how the debugger interfaces, but I am now seeing how it will interact with the selected items & the custom commands list.  I hadn't understood the connection properly before. 

I will persevere & see how it goes.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #13 on: August 06, 2013, 18:15:37 »
For purposes of learning to write a script I have a folder called  -
abc v.1.2

I want
abc v1.2

The idea being that I keep a "." following a numeral, stripping any others.  A simple regex transposition I thought....
Code: [Select]
((?:[a-zA-Z]+)+)\.
-to-
\1<space>


I have made a couple of assumptions:-
  • I need to get the selected folder name;
  • I assign it a variable;
  • I then pass that variable to my command string.
  Guessing this is something like
Code: [Select]
$s = StrRegExpFind( $s, "v." , "v", ".?\d");
As I don't know PHP & I don't have examples of regex type formulae for MCS (I tried transposing elements of Jungles script but it's too advanced for me at this point) I have been messing for several hours unsuccessfully with various combinations & assignments & getting nowhere.


I began trying a simple rename -  1.2.3.4 to 1234 & failed. :'( :-[

I managed to grab the name-
Code: [Select]
@var $arr = GetSourceSelectedFileNames();It comes up as an array correctly, then I assumed I would hand the $arr to the rename function (even tho it's technically a string)
Code: [Select]
RenameFile( $arr = StrReplace( $arr, "." , "" ) );
The debugger runs but doesn't red-flag anything (BTW is there a list of error codes somewhere?)
& of course nothing happens.....
 :-[ ???

So I think I need a walkthru or something that helps me define what I need to do in terms of framework & assignments. 
I assume Jungle wasn't born fluent in MCS,  :P so how do I get started with this?  TiA

 

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #14 on: August 06, 2013, 18:47:29 »
RegExp are a pain. There are many reqex online testers/editor. try one of them they might be helpful when creating regex.

You can not send an entire array into a function that expect a string.. You need to send each item (string) from the array one by one from the array into the function.
Also do not put to much in one line inside each other. the debugger will stop after ever line, so if you got many things in a line then you will not see what went wrong. (You can shorten it afterwards)

Code: [Select]
@var $arr = GetSourceSelectedFileNames();
@var $items = arrayCount($arr);
@var $OrgName;
@var $NewName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $NewName = PathGetNamePart( $CurrentNameFullPath );
  $NewName = StrReplace( $NewName, "." , "" )
 
  RenameFile( $CurrentNameFullPath, $NewName, "SILENT" );
}
( Or something like that.. code above is NOT tested.. )

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #15 on: August 06, 2013, 19:04:22 »
RegExp are a pain. There are many reqex online testers/editor. try one of them they might be helpful when creating regex.

You can not send an entire array into a function that expect a string.. You need to send each item (string) from the array one by one from the array into the function.
Also do not put to much in one line inside each other. the debugger will stop after ever line, so if you got many things in a line then you will not see what went wrong. (You can shorten it afterwards)

Code: [Select]
@var $arr = GetSourceSelectedFileNames();
@var $items = arrayCount($arr);
@var $OrgName;
@var $NewName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  // The the Filename part only. (Since the TO name in RenameFile does not accept a path)
  $NewName = PathGetNamePart( $CurrentNameFullPath );
  // replace . with empty.
  $NewName = StrReplace( $NewName, "." , "" )
  // Rename current file to a new name. 
  RenameFile( $CurrentNameFullPath, $NewName, "SILENT" );
}
( Or something like that.. code above is NOT tested.. )

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #16 on: August 06, 2013, 19:51:18 »
Thanks for the pointers Mathias.
I will try again.  The regex I posted above works in its' native environment, but I don't mind creating something different for MCS.  Just a question of seeing what I'm doing wrong...in theory.


I note you didn't test the code but I find it interesting you got the same errors as I did: 23 & 1    ;)
I don't feel so bad I failed completely now ;D

Did you say you were going to add permanent line numbers to the debugger?  (That display when debugger is stopped)

Quote
Multi-Script debugger started.
Debugging started.
Debugger attched.  <<Typo you may wish to fix some time>>
Script engine error => Variable $CurrentNameFullPath not found
Script engine error => Failed to process token - "$NewName"
Script engine error - Line : 10, Error : -23 => Parameter wrong type, Code : "$NewName = StrReplace( $NewName, "." , "" )"
Script engine error - Line : 10, Error : -1 => Code : "$NewName = StrReplace( $NewName, "." , "" )"

Debugger detached.

I swapped $OrgName with $CurrentNameFullPath

Code: [Select]
@var $arr = GetSourceSelectedFileNames();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $NewName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath= $arr[ $n ];
  $NewName = PathGetNamePart( $CurrentNameFullPath);
  $NewName = StrReplace( $NewName, "." , "" )
 
  RenameFile( $CurrentNameFullPath, $NewName, "SILENT" );
}
 
It steps thru & shows the name would change without errors.  the script isn't executed (Normal for debugger or should it execute on the selected elements?)  So I'm not there yet.

[I don't want to eat up too much of your time reviewing this however!]
« Last Edit: August 06, 2013, 20:10:36 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #17 on: August 06, 2013, 21:48:48 »
Yes misstyped the definition of the variable name.. copy paste error.  Told you I did not run it.
But the error message is saying what when wrong.. "variable xxx not found.." so you worked it out..

Did you say you were going to add permanent line numbers to the debugger?  (That display when debugger is stopped)
No. it will not be added.


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #18 on: August 07, 2013, 09:36:59 »
...Told you I did not run it...

Yes just kidding around...  :)  I appreciate the help.


Is it necessary to include all variables in the { instruction} groups?
I wonder why it shows a change in the debugger but doesn't pass the change to the active selection.  Is it waiting for other vars to be handled?

Currently I think I will attempt to run 3 lines to emulate the regex.
1 strip "." & leave space
2 place "." following number.
3 remove space following 'v'


If \d is digit is \a letter?  Wondering about ?\a in this situation.

Better start reading a book on PHP if the forms are similar I guess.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #19 on: August 07, 2013, 11:05:24 »
Is it necessary to include all variables in the { instruction} groups?
Eh ?  { are for defining a scope. and loops and conditional expressions will execute its scope if the expression is true.
See how the script language works here http://multicommander.com/docs/multiscript/languagesyntax

I wonder why it shows a change in the debugger but doesn't pass the change to the active selection.  Is it waiting for other vars to be handled?
The value that the debugger show is what the script engine has for that variable.

If \d is digit is \a letter?  Wondering about ?\a in this situation.
\d and \a and all other strange combination is regex stuff. You have to google/bing how regex works. I'm not an expert at regular expressions.

Better start reading a book on PHP if the forms are similar I guess.
MultiScript is not PHP.. but it follows the same programming method as any program language like c/c++/javascrfipt/java/php.. men only in its syntax and structure.. not in built in functions.
So you only need the first chapter in any programming book for beginnings. :)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #20 on: August 07, 2013, 15:01:44 »
I'm OK for regex's (not expert but OK) I like sites like this
http://regexlib.com/
for some ideas.
Regex is just something for me to attempt as I know I could use MC scripts with regex. 

I have been browsing your online docs.  Cannot see anything which says outright what a perfect base script looks like.  Trying to step through the script Jungle made, I can (more or less) see why it does what it does but cannot extrapolate that yet as it has a lot of operations & variables for my untrained eye.

I'm wondering about vars & 'scope' (sorry, I wasn't using correct terminology)
As you can see from the screen shot in the post above, the debugger didn't red-flag anything but it didn't rename the selected folder either. 
It shows the result but doesn't execute the change.  ???
I therefore wondered if the script requires all predefined vars to be included in {} operations before it will execute.
Apart from that I cannot see why it didn't execute...  No obvious elements like RUN or END etc jump out at me

Slightly OT

I noticed the 'regex' box in the mass rename dialog is greyed out.  That would be useful for me to have in the future. 
(It wouldn't teach me the MCS however.) :P

The addition of TIME="TODAY" etc functions in my custom search works well btw
The updated in-line rename works well, hadn't noticed the <ctrl> + <e> or <ctrl> + <l> before.  Useful  ;)

Thanks Mathias.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #21 on: August 07, 2013, 23:36:14 »
I'm wondering about vars & 'scope' (sorry, I wasn't using correct terminology)
As you can see from the screen shot in the post above, the debugger didn't red-flag anything but it didn't rename the selected folder either. 
That is because there is no script error.  but I think you are sending the wrong data into RenameFile(...)
Look at the doc http://multicommander.com/docs/multiscript/functions/filesystem#renamefile and and the description of the parameters and what you send, and see if you can see it. :)
 
I therefore wondered if the script requires all predefined vars to be included in {} operations before it will execute.
Apart from that I cannot see why it didn't execute...  No obvious elements like RUN or END etc jump out at me
The { } are "scopes" for loops or other conditional expressions..  For example if the expression inside an if( ... ) is true the scope belonging to the if is executed. else it is not.
and variables that are defined inside a scope only exists inside that scope. if variable are define in the "global" space then they are accessible by all.
you can write BEGIN / END instead of { } for example
if( $b )
BEGIN
 ...
END

But only boring languages like basic is using that syntax.. { } is used by all modern languages like c/c++/java/js/php and so on.


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #22 on: August 08, 2013, 16:12:25 »
I have finally grasped which function/call & which token arrangement was needed to get my initial script to apply itself!

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $NewName;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $NewName = StrReplace( $OrgName, "." , "" );
 
  RenameFile( $arr, $NewName, "SILENT" );
  }

Now all I have to do is build my regex! :P
I am beginning to see how the different instructions need to be addressed.   

I wonder if there is already a good way to hi-lite the text for ease of editing (I've found with notepad++ I can save as a codepage & get coloured results, eg:- as .pl or .php), which helps me at the moment.

Thanks for your time & tuition Mathias.   :)

*
For regex you mention StrRegExpFind is there also StrRegExpReplace? (If so I haven't seen it in the docs) I am currently assuming that the the find command will then invoke the regex to do any necessary editing.
« Last Edit: August 08, 2013, 16:59:46 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4416
    • View Profile
    • Multi Commander
Re: Search routine
« Reply #23 on: August 08, 2013, 16:29:55 »
I wonder if there is already a good way to hi-lite the text for ease of editing (I've found with notepad++ I can save as a codepage & get coloured results, eg:- as .pl or .php), which helps me at the moment.
Well use an external editor is the option for that.  The built in editor in MC will not support syntax coloring anytime soon. cost so much (in time) to include an edit that support that.

For regex you mention StrRegExpFind is there also StrRegExpReplace? (If so I haven't seen it in the docs) I am currently assuming that the the find command will then invoke the regex to do any necessary editing.
Only functions you see in doc is there. or in release note for latest beta version if it is a new function.. after final release doc is updated. 

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Search routine
« Reply #24 on: August 08, 2013, 17:03:36 »
Quote
syntax coloring
OK no problem.  I can use my current workaround.  ;)

Quote
new function
OK, well I am currently hoping something like the following would work, but it doesn't seem to...

Code: [Select]
$NewName = StrReplace( $OrgName, "{((?:[0-9]+)+)\ }", "{((?:[0-9]+)+)\.}");
Is it just my lack of skill or StrReplace won't accept a regex at all?