Author Topic: MultiScript Name selection excluding file extension?  (Read 44062 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 511
    • View Profile
MultiScript Name selection excluding file extension?
« on: November 28, 2015, 16:22:19 »
I'm wondering about how to exclude the file extension with multiscript.  (Obviously MultiRename has a switch for this.)

If I use GetSourceSelectedFileNames or similar, I get a complete name.
I thought about generating a list of files to clipboard without extensions & I thought about renaming using a regex but excluding the extension.

Whilst I can imagine using something like (pdf$|txt$) in the regex to force to an end of name, this is quite restrictive.
It would be nice if there is a way to target the process to the different parts <file>.<ext> to avoid some clashes.
e.g.
I have a pdf which the user has labelled document(pdf).pdf as the creator has their Windows settings on exclude showing extensions & uses extra filetype identification signals.
If I run a normal SAR in MS for pdf I'd lose both elements.


Is there anything like e.g. GetSourceSelectedFileNamesExcludeExt script function for file name selections? 
Or have I not found the way this is done?  ::)

EDIT
I am going to try StrSplit & a $anchor to see if I can target it to the correct regex selected elements... & PathGetNamePart is one of the things I should be thinking about!
« Last Edit: November 28, 2015, 18:35:39 by Ulfhednar »

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 536
  • Old Skull
    • View Profile

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 511
    • View Profile
Re: MultiScript Name selection excluding file extension?
« Reply #2 on: November 28, 2015, 18:37:36 »
Thanks Jungle. 
I had finally got round to that when I saw your post!  ::)
I thought I recalled something of the sort but was not looking in the right section!

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 511
    • View Profile
Re: MultiScript Name selection excluding file extension?
« Reply #3 on: November 29, 2015, 14:16:34 »
I managed to do what I wanted by using several passes in the script.
The biggest question I had was trimming extraneous characters like a trailing whitespace or brackets.

I used
Code: [Select]
@var $re = "(docx$|pdf$|txt$)";
@var $reWS = "\s+\.";
to identify file exts to a regex & then ".$1" to ensure a dot was next to that ext.

Wondered if there was a better way. 
I know it is possible to exclude the ext from the rename pass outside MS, & that this way means I must specify the exts for the regex.

I had wondered about StrSplit, but having got the array I couldn't get it to re-assemble after the renaming pass on array element 0.
i.e.
file-name.ext
output to -
0 = file-name
1 = ext

Is there a function to rejoin 0 & 1 + add/keep a . delimiter?
Thanks in advance!

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 511
    • View Profile
Re: MultiScript Name selection excluding file extension?
« Reply #4 on: November 29, 2015, 16:05:09 »
 ::)  OK I think I should just have recalled the discussion on
PathGetFileExtPart
 :-[
 ;)

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 536
  • Old Skull
    • View Profile
Re: MultiScript Name selection excluding file extension?
« Reply #5 on: November 29, 2015, 19:28:57 »
Sorry, but I'm not sure I realize what is you task and expectable result. Please clarify your task and provide some examples.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4409
    • View Profile
    • Multi Commander
Re: MultiScript Name selection excluding file extension?
« Reply #6 on: November 30, 2015, 11:22:10 »
Im not sure what you want to do..

But to trim a string from white space there is a StrTrim function ( List of other string function : http://multicommander.com/docs/multiscript/functions/string )

And a jungle said..  PathGetNamePart(..) returns the filename part. and you also have PathGetFileExtPart(..) that return the file ext..
that you can store in a variable and then add it again when building the final filename



Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 511
    • View Profile
Re: MultiScript Name selection excluding file extension?
« Reply #7 on: November 30, 2015, 13:05:40 »
Thanks for replies guys.
I am (once more) playing with MultiScript & hoping I can learn it properly by giving myself problems :)
The breaks between using it meant I forgot stuff :(


I had several ideas about a renaming script.  So I had several problems ;)

The MultiRename feature does pretty much everything already & I can save/load the multi-step renames BUT I wanted to call a MR saved pattern into a button, rather than open the rename dialog etc each time I hit a folder that had items I needed to rename.

I couldn't see that it could be done directly so I wanted to try scripts for buttons that gave me my rename options.

In examining several types of rename I realized I could manipulate elements via the array & create more interesting name syntax.

I hit several snags
  • - extraneous characters like whitespace (I found different ways of trimming these & making pattern changes with regex)
  • - correctly identifying name & ext in the script (resolved when I managed to find the correct functions!)
  • - this also raised questions about generating different name patterns & reassembling an array of strings to a single name string (regex works with the pattern structure but I couldn't figure out how to assemble the strings generated - StrLinesArray2String etc attracted my attention - but I failed to complete the idea.  I guess I have to think about Tokens)


The result was several different routines that approached my desired results.
I then began to think about optimization that would cover more variables & do more things.

This ultimately led me to the great 'name as folder' script provided waaaay back by jungle
http://forum.multicommander.com/forum/index.php/topic,945.msg2855.html#msg2855

I am now at a point where things are as I wanted but the possibilities I encountered via errors mean that I want to make more complex & interesting scripts.
I have been looking at Jungles script to try & evaluate how it works & thus how to emulate it.
So my spare time is fast disappearing.  :P




Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4409
    • View Profile
    • Multi Commander
Re: MultiScript Name selection excluding file extension?
« Reply #8 on: November 30, 2015, 14:38:34 »
    The MultiRename feature does pretty much everything already & I can save/load the multi-step renames BUT I wanted to call a MR saved pattern into a button, rather than open the rename dialog etc each time I hit a folder that had items I needed to rename.
    You can not load a saved MultiRename setup from script.
    I tis on my list. But MultiRename is kind of disconnected from MC so script can't really interact with MultiRename so easy.So not sure when I get the time for it.

    extraneous characters like whitespace (I found different ways of trimming these & making pattern changes with regex)
    I don't think you need regex for them, There are alot of string function for most thing so for normal operations regex is not needed.
    MultiScript is not pearl :) regex is not required. Most of the time

    this also raised questions about generating different name patterns & reassembling an array of strings to a single name string (regex works with the pattern structure but I couldn't figure out how to assemble the strings generated - StrLinesArray2String etc attracted my attention - but I failed to complete the idea.  I guess I have to think about Tokens)[/li][/list]
    I'm not sure why you do this. you got the file part in one variable the extension is one.. just combine them.?
    No need to flatten arrays or anything.. Not sure what you are doing.

    Might be easier to help you if you post the script and explain what you expect that script to do.

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #9 on: November 30, 2015, 19:28:52 »
    Thanks Mathias.
    I am now at a point of experimenting.  (& hoping to refine my MS knowledge.)

    If I ref my example above - document(pdf).pdf - where I wanted to remove the (pdf) name component, I then wondered if I could do the opposite - take the .ext of any selected file & drop it into the name pattern.
    So I have been playing around with ideas in the debugger to see what can happen & if I am getting syntax correct to make snippets to possibly use later.

    Mostly this is now me playing about, I am just trying different things.
    So other than needing a MS crash-course I am OK for now! :D :P

    Turns out MS can be fun for me to mess with (!) & gives some useful possibilities that I will expand upon when I have time.

    Now I have figured out how to script (some of) my renaming ideas I can wait for you to port the MultiRename functions at your leisure ;)
    Maybe using a MC.Command internal-type call to run MR (possibly silently) is a better way than a MS interface?

    OT
    this has made me wonder if I can call MDV to open files from outside MC - e.g. as a read-only notepad replacement in certain situations.

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #10 on: November 30, 2015, 21:50:26 »
    Maybe using a MC.Command internal-type call to run MR (possibly silently) is a better way than a MS interface?
    It is the same things. the MC.Command need to talk to something..

    OT
    this has made me wonder if I can call MDV to open files from outside MC - e.g. as a read-only notepad replacement in certain situations.
    Yes but also no it will not work since it depends on stuff inside MC


    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #11 on: December 05, 2015, 19:25:30 »
    Thanks for the info Mathias.
    Wasn't sure how deeply linked MC.Command & MS are.  I am interested in how you created the system & whether it is possible for someone to add to it.  I assume one needs to know a lot of your code though & that you are the only Expert.

    I wondered about camel case for renaming in scripts.  I can see it would be easy to identify in a filename e.g. \s[a-z]+ but would I need to manually add the 26 options a-A to z-Z as I don't see a command that is case changing?
    Although it is only 26 lines :P

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #12 on: December 06, 2015, 11:27:39 »
    You can create plugins to MC that extended the script language, There is a SDK for plugins,  But you need to know C++ to create the plugins.

    Not sure what you are trying to do with the regex for creating camel case. would it not be easier just to parse the string and change the case of the letters when needed ?
    Regex can be very powerfull but you often have very little control of what is done. sometimes it is easier to do the regex work by hand.

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #13 on: December 06, 2015, 13:25:46 »
    Thanks Mathias.  C++ is on my to do list :P
    I am trying to identify my best approach for best use of time.  Comparing MS with things like PHP & Java I can see some elements which seem to have a greater similarity than with C++. 
    I do have an interest in C++ as I perceive it as a more powerful tool.

    I haven't figured out how to use MS to parse for case!   
    I suppose I'd have to split the name to an array then tell MS to capitalize each segment....
    Or is there a command/routine I can call (as this feature exists in MR)? :)

    I just thought about what I know.  & I find the regex tool/concept interesting so it is a game for me running those.

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #14 on: December 06, 2015, 14:06:14 »
    Yes, C++ is powerful but it can also be very hard to get correct, There is nothing protecting you from doing bad things in C++
    Learn Php/Java well first before moving over to C++

    A string is already like an array of characters. like in Php and Java (and C/C++,JavaScript)
    you can access characters using the index operator.  [ ]

    Code: [Select]
    @var $text = "MyString";
    @var $pos = 1;
    $text[$pos] = StrToUpper($text[$pos]);

    // $text == "MYString"


    « Last Edit: December 06, 2015, 14:14:14 by Mathias (Author) »

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #15 on: December 06, 2015, 14:54:00 »
    Ahhh I hadn't got as far as thinking I could set a position qualifier on that command! 
    I assumed my option was either all lower or all upper when looking at the StrToUpper & StrToLower commands.

    Thanks for taking the time to show me that info.  ;)

    Seems I can do bad things in all types of coding! :D
    I will have a go at PHP/Java & see if that helps me see MS more clearly.

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #16 on: December 07, 2015, 09:28:55 »
    It seems I can give the script a specific command for a fixed position but not one for a variable. 

    I have been trying to get the StrToUpper to rename multiple matches, but I couldn't get the command to work correctly with this -
    Code: [Select]
    @var $text = "My string";
    @var $pos = StrIsRegExpMatch($text, "\s[a-z]";
    $text[$pos] = StrToUpper($text[$pos]);

    // $text == "MY string"
    Should this regex work?  I can't see why it chooses position 1, if the regex can be passed to it.

    I've ended up looking at an array of parts - haven't cracked it yet though.  ::)
    Apologies in advance if I'm being a muppet!   :)

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #17 on: December 07, 2015, 09:54:36 »
    The RegEx row fails.

    Try running the script in the debugger. Then you can step one line at the time

    Menu> Help> Multi-Script Debugger


    Also StrIsRegExpMatch return 1 the regEx and input is a match else 0.. Not the position
    http://multicommander.com/docs/multiscript/functions/string#strisregexpmatch

    Think you need to use StrRegExpFind if you want the position


    Jungle

    • Contributor
    • VIP Member
    • *****
    • Posts: 536
    • Old Skull
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #18 on: December 07, 2015, 10:03:26 »
    Ulfhednar
    1. StrIsRegExpMatch function compares string with RegEx pattern, it doesn't perform search. So it always returns 1 if success and 0 otherwise. It's a state, not position.

    2. $text[$pos] =  assignment modifies only one single character in the string.

    So if you want to find a position of RegExp in your string, you should use StrRegExpFind function. But unfortunately it returns only position, so you [probably] can't extract matching substring.

    IMHO StrRegExpFind function should be improved to return array of (pos; len) if possible.
    « Last Edit: December 07, 2015, 10:06:41 by Jungle »

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #19 on: December 07, 2015, 13:58:28 »
    OK thanks guys.
    Yes I can see my false expectations assumed a position value not just y/n (1/0) response.   ::)

    Jungle has seen my expectation/the correct idea in saying "StrRegExpFind function ...to return array of (pos; len)"
    (pos; len) was the value I was wanting to pass along, but couldn't see how.
    I will certainly try StrRegExpFind & see what happens.

    My approach had moved to breaking the string (StrSplit) into sections using " " <space> as the delimiter (I also think a regex with \W would be more useful in some cases)
    Having generated an array I just needed to figure out how to manipulate & 'reassemble' the modified string.  Maybe tokenize.

    I hope I am getting a better sense of things though!  ;) 

    OT
    Mathias
    - do you think you will have time to polish/extend the debugger functionality or is it a non-priority?  I don't know how many of your users use it, even little things like having right click menu functions would be helpful to me sometimes.


    Update
    Code: [Select]
    @var $text = "My string";
    @var $pos = StrRegExpFind($text, "\s[a-z]";
    $text[$pos] = StrToUpper($text[$pos]);

    gives error
    Script engine error => Failed to initialize variable "$pos" with "StrRegExpFind( $text
    Script engine error => Failed to define variable - "@var $pos = StrRegExpFind( $text , "\s[a-z]""
    Script engine error - Line : 1, Error : -1 => Code : "@var $pos = StrRegExpFind( $text , "\s[a-z]""


    Tried different regex eg \w also fails.  :-\
    « Last Edit: December 07, 2015, 14:18:10 by Ulfhednar »

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #20 on: December 07, 2015, 14:39:13 »
    You forgot end parentheses

    @var $pos = StrRegExpFind($text, "\s[a-z]" );

    Mathias - do you think you will have time to polish/extend the debugger functionality or is it a non-priority?  I don't know how many of your users use it, even little things like having right click menu functions would be helpful to me sometimes.
    Depends on what in the debugger that are missing need fixing. and what right click on it should do

    However.. What are you trying to do.  Make First letter after space uppercase ? So "My small string" =>  "My Small String" ?
    Then loop string if you find space then make the next character uppercase.. and continue..

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #21 on: December 07, 2015, 17:28:42 »
    You forgot end parentheses
    Nooooooooo!  ::)  How the hell did I manage that?!?!!?

    @var $pos = StrRegExpFind($text, "\s[a-z]" );

    Mathias - do you think you will have time to polish/extend the debugger functionality or is it a non-priority?  I don't know how many of your users use it, even little things like having right click menu functions would be helpful to me sometimes.
    Depends on what in the debugger that are missing need fixing. and what right click on it should do

    However.. What are you trying to do.  Make First letter after space uppercase ? So "My small string" =>  "My Small String" ?
    Then loop string if you find space then make the next character uppercase.. and continue..
    Yes that is what I am trying to do, & I will soon be trying to get the loop right...  Have only had about 10mins today tho as the missing ) shows!

    I didn't really have anything super-special in mind for right click, standard commands for when I forget I have to use <ctrl>+X C A etc.
    I would like to see the error codes defined. (That would be a cool tool-tip or right click option!!)

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #22 on: December 07, 2015, 20:22:21 »
    The editor is Windows standard editor component so assumed that the noraml copy/paste was there.  Butlook like it is not
    But Copy / paste using keyts work..  Ctrl+C / Ctrl+V

    Here a snippet for Making first characters and characters after space upper.
    Code: [Select]
    @var $text = "mY liTTle sTrIng";,

    // make everything lower case
    $text = StrToLower($text);

    @var $maxLen = StrLen($text);

    // set to 1 to begin with so the first char is made upper
    @var $makeNextUpper = 1;

    // Loop all characters, If Space is found make Next character upper
    for( $pos = 0; $pos < $maxLen; $pos++)
    {
       if($makeNextUpper == 1)
       {
         $text[$pos] = StrToUpper($text[$pos]);
         $makeNextUpper = 0;
       }
       
      if( StrIsEqual($text[$pos]," ") )
      {
        $makeNextUpper = 1;
      }
    }

    Ulfhednar

    • Contributor
    • VIP Member
    • *****
    • Posts: 511
      • View Profile
    Re: MultiScript Name selection excluding file extension?
    « Reply #23 on: December 08, 2015, 19:43:42 »
    Many Thanks for your time on this Mathias
    I can now see how MS loops should be made!  ;)
    Hopefully that will allow me to develop my MS a little more.

    makeNextUpper - is new?  Didn't see it in the docs when I searched.

    The RMB menu isn't serious, it's a niggle when I do dumb things, e.g. I occasionally forget the absence of the RMB menu, hi-lite then lose the selection because I forget to use <Ctrl>+ C or whatever ...  :P

    Mathias (Author)

    • Administrator
    • VIP Member
    • *****
    • Posts: 4409
      • View Profile
      • Multi Commander
    Re: MultiScript Name selection excluding file extension?
    « Reply #24 on: December 08, 2015, 20:14:45 »
    Many Thanks for your time on this Mathias
    I can now see how MS loops should be made!  ;)
    Hopefully that will allow me to develop my MS a little more.

    makeNextUpper - is new?  Didn't see it in the docs when I searched.

    The RMB menu isn't serious, it's a niggle when I do dumb things, e.g. I occasionally forget the absence of the RMB menu, hi-lite then lose the selection because I forget to use <Ctrl>+ C or whatever ...  :P

    makeNextUpper is a user declared variable.

    After it gets the lenght of the string in $text it create the variable $makeNextUpper  and sets it to 1
    Code: [Select]
    @var $makeNextUpper = 1;