Author Topic: Rename & move/copy  (Read 40841 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Rename & move/copy
« on: August 22, 2013, 17:55:59 »
Having got my regex script to work I wondered about scripting a rename instruction followed by a copy/move instruction to be attached to a button.
This is an attempt to combine my existing code with a few more operations.
Basically I want to select a file; rename according to a pattern; copy/move that file to a new location.
So my script 'should'
a - get selected files/folders
b - run the renaming operation(s)
c - open the copy or move dialog (default transfer to opposite pane).  The destination should be editable as per the 'Find' dialog copy/move operation.  Option for silent copy/move could be useful too.

Is this possible?
I assume I can invoke the copy or move dialog after the renaming & take the updated selected file forward into the copy/move operation.

I can see some possible issues - forcing the selection to update & then taking the 'new' item into the new operation seems like it could be a problem.  Can the script check the item has  1 - updated & 2 - then 'auto' select the new product of the rename operation?


Sorry if this sounds dumb, I'm new to this type of coding & I need to practice something with loops etc to get a better understanding.... ::)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #1 on: August 22, 2013, 18:15:25 »
You already know how to rename..

So after the rename create a new string that is the path to the source file then use

Copy File
or
Move File

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #2 on: August 24, 2013, 19:59:16 »
Thanks Mathias.
I had wondered if I needed to "apply" one op  before starting another?
 
I think I am still getting my tokens wrong. 
With multiple ops it doesn't update.

Do I need to instruct it to update & continue?  Is it automatic?

If you have time the following is what I've imagined so far!


My goal is 2 rename passes & a move.
I am doing it in steps.  i.e. create 3 working routines then combine them into a single script.

I made 2 (individually working) rename scripts, I combined them & added the resulting script to a button.
It needed to be clicked twice to perform the task.   ???
Am I missing a linking command between the ops to apply both operations consecutively?

This was something like it - (I think my $CurrentNameFullPath is a problem for part 2)
Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "([0-9]+)_([a-zA-Z]+)_([0-9]+)";
@var $result;
@var $n = 0;

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, "$3.$2-$1");
 
  RenameFile( $CurrentNameFullPath, $result, "SILENT" );

}
 


@var $NewName;
@var $NewName2;

{
  $CurrentNameFullPath = $arr[ $n ];
  $NewName = PathGetNamePart( $CurrentNameFullPath );
  $NewName2 = StrReplace( $NewName, "." , "-" );
 
  RenameFile( $arr, $NewName2, "SILENT" );
  }




My logic is:-
1. select item
2. rename item
3. rename item (2)
4. select result & move

I've tried just a move op:-

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

for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $targetPath = GetTargetPath();
  $sourceFile = GetSourceFocusPath();

  MoveFile( $targetPath, $sourceFile );
}

That also worked on it's own...




« Last Edit: August 24, 2013, 20:06:29 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #3 on: August 24, 2013, 21:10:55 »
You are renaming the same files twice ?

After the first rename operation the filenames in the array ( $arr ) is not correct anymore, The names in there are what they was the first time.
You need a second list. and for every file you rename at in the first loop you add the fullpath to what the new filename is and then use that list when doing the second rename.

(Why do you rename the same files twice. ?  )




Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #4 on: August 24, 2013, 23:02:18 »
- why twice -
I wanted to add complexity but keep script components I know work to minimize introducing new problems.
I can imagine a scenario where a regex rename + rename could be useful.
- A logical way to push myself to visualize the way the MC coding uses tokens etc.   :)


I got the 1x regex rename +move to work after a bit of tidying:-

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $OrgName;
@var $re = "([0-9]+)_([a-zA-Z]+)_([0-9]+)";
@var $result;
@var $n = 0;

// regex rename
for( $n = 0; $n < $items; $n++ )
{
  $CurrentNameFullPath = $arr[ $n ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $result = StrRegExpReplace($OrgName, $re, "$3.$2-$1");
 
  RenameFile( $CurrentNameFullPath, $result, "SILENT" );

}

// move

@var $targetPath;
@var $sourceFile;

{
  $CurrentNameFullPath = $arr[ $n ];
  $targetPath = GetTargetPath();
  $sourceFile = $CurrentNameFullPath;
 
  MoveFile( $targetPath, $sourceFile );
}

I am gradually seeing why things operate as they do.  Hopefully I can fix this 2x rename script now & start working on more complex multi-process scripts.

Thanks for the assistance Mathias.  ;)
« Last Edit: August 25, 2013, 10:02:36 by Ulfhednar »

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #5 on: August 25, 2013, 01:35:52 »
Updated
I've replicated the 3 variables sets for each operation & now can have multi-step renaming ops +copy/move.

Had a problem with <str> vs <array> in my Move op when selecting multiple folders but I've figured it out...!

This is the code I've appended for move:-
Code: [Select]
@var $arr3 = GetSourceSelectedPaths();
@var $items3 = arrayCount($arr3);
@var $MoveNameFullPath;
@var $targetPath;
@var $sourceFile;
@var $n3 = 0;

for( $n3 = 0; $n3 < $items3; $n3++ )
{
  $MoveNameFullPath = $arr3[ $n3 ];
  $targetPath = GetTargetPath();
  $sourceFile = $MoveNameFullPath;
 
  MoveFile( $targetPath, $sourceFile );
}

Hopefully my bungling will inspire others to use the scripting tools. 

Now I'm wondering if I can rename files within a folder to match the updated folder name before I move the folder..... :P
« Last Edit: August 25, 2013, 10:11:57 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #6 on: August 25, 2013, 12:41:56 »
Now I'm wondering if I can rename files within a folder to match the updated folder name before I move the folder..... :P

Eh ?
You want to rename a folder and then you also want to rename the subitems in that folder before you move them ? Or ?

Use FileFiles to get an array with paths to all the files inside a folder.
Then you do that before or after you renamed the main folder.
and then move it.



Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #7 on: August 25, 2013, 13:50:09 »
Quote
You want to rename a folder and then you also want to rename the subitems in that folder before you move them ?

Yes. 
I want to - 
  • select & update folder(s) name
  • find & select files within the folder(s) matching *.xxx, *.zzz, *.ddd (i.e. match different file types in various folders)
  • rename to match their parent folder.
  • move the parent with updated contents
I still have some hair that hasn't been pulled out  :D

OK, I will attempt to incorporate the findfiles function.

More rename +copy/move
This is an idea for another script for a button. 
This would require the calling of hotkeys & waiting for the correct state of the item.
Specifically -
Can I invoke F2 on a selected item then auto copy/move it to target after the in-line edit has completed?

Best to ask if it is possible before I try to do it!
« Last Edit: August 25, 2013, 14:29:39 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #8 on: August 25, 2013, 14:41:02 »
Can I invoke F2 on a selected item then auto copy/move it to target after the in-line edit has completed?

Not sure why you want to do that.. Just get the information you need from the UI at the beginning of the script.

Also the script is run in the same thread as the UI. So the UI will not update it self until the script has run. However it is possible to run the script in a background thread. But not all script functions works then. And there are many strange things that can happen so it is not officially supported.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #9 on: August 25, 2013, 17:31:27 »
I thought it might be useful/interesting to call a hotkey via script. 
In this case my idea is probably not sensible.   ::)
No problem.   I'll think about different approaches. 

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #10 on: August 25, 2013, 17:35:58 »
You can call a command, internal or user created.

MC.RunCmd ID=<cmd id>

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #11 on: August 25, 2013, 18:06:28 »
OK thanks Mathias, that will be something I can play with.  :)


I am finding using findfiles for files inside a selected folder difficult.  I can't figure out how to make it search & select inside the selected folder.   That said, it is easier to use the built in search function in this case.  Copying features you already have might be counter-productive to learning something...  :-\

In which case the following is probably irrelevant.

I'm not sure about my syntax - (<arr>,"xxxxx")
Just trying to get the debugger to return the txt match in the folder at this point.  It shows the folder name not the txt inside the selected folder. 

But perhaps this not within the scope of the find command?

This is what I have so far: -

Code: [Select]
@var $arr = GetSourceSelectedPaths();
@var $items = arrayCount($arr);
@var $CurrentNameFullPath;
@var $file
@var $files;
@var $files1;
@var $n = 0;

//find txt

for( $n = 0; $n < $items; $n++ )
{
  $file = $arr[ $n ];
  $files = PathGetFileExtPart( $file );
  $files1 = FindFiles( $files, "*.txt");
}

The upside is I now have several small routines to automate some of my regular tasks. 
Been very interesting so far.   :)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #12 on: August 25, 2013, 18:34:55 »
I am finding using findfiles for files inside a selected folder difficult.  I can't figure out how to make it search & select inside the selected folder.   That said, it
Not sure what you mean with select. I think that you might try to use script for controlling the UI?
The script engine is separate. It is not connected to the UI. It can fetch some information from UI, But that all.
Think more of it like .bat files in windows command line.

FindFiles will return an array with path to files it finds when scanning that path you tell it to scan.

$files = PathGetFileExtPart( $file );
Get get the file extension part here  Like ".txt" and use that as a path into FindFiles.
You need to send a path WITH filter like  "C:\Temp\*.txt" into FindFiles
If you look at the doc you see that the second parameter is if <num> (number) and if it is 1 then it will only match files,

$files = FindFiles( "C:\\Temp\\*.txt" );

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #13 on: August 26, 2013, 14:46:59 »
Thanks for looking at this Mathias. 

I had looked at the docs then at Jungles script (Name as Folder)
Code: [Select]
    $file = $arr[$i];
    $s = PathGetFileExtPart( $file );
    $name = $prefix + "*" + $s;

    $files = FindFiles( $src_path + $name );
    $n = arrayCount( $files );

& thought that I could also give a string variable.  I looked at my code today & realized I'd omitted that bit!  (& probably other things...)

I think you're right about the UI. 
My idea will be best handled by MCs own tools, so I will need to study the MC.<command> features more closely.  That could give me some great automation.
e.g. I like the ability to load the explorer panel filter via a button - "MC.Explorer.SetViewFilter" - is very useful. 
So I need to play with the other commands.

I think that the ideas I had clash between using MC script & taking over parts of the MC UI environment.
I am not 100% sure of boundaries yet. 

I can see DOS .bat type script is cumbersome/weak compared to the MC script, & that the script is out classed by C++ MFC. 
Now I'm learning more about using the right tool for my job.

I think a button script calling MC commands could allow me to -
activate in-line edit
paste in clipboard contents
copy/move

Which might make a useful button. 
Using my new MCscript based buttons has improved my work environment a great deal.


I noticed on the 1481 update today that array processing by functions has changed.  If I understand this correctly it looks like scripting will be quicker & easier.  This will help me move forward in my experiments.

OT - I found a handy free regex tester that anyone wanting to use the MCS regex functions might find useful - http://antix.co.uk/Projects/Regex-Tester  The variables it shows seem fully compatible but I haven't tried them all yet!

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #14 on: August 26, 2013, 15:32:12 »
I think you're right about the UI. 
My idea will be best handled by MCs own tools, so I will need to study the MC.<command> features more closely.  That could give me some great automation.
e.g. I like the ability to load the explorer panel filter via a button - "MC.Explorer.SetViewFilter" - is very useful. 
So I need to play with the other commands.

I think that the ideas I had clash between using MC script & taking over parts of the MC UI environment.
I am not 100% sure of boundaries yet. 
Using the "CustomCommands" you can set some part of the UI.  Even if you used them in MultiScript.
But if you go crazy I think you can break things.. 
hehe If you learn C++ program you can create plugins instead. From there you have a lot more control

I can see DOS .bat type script is cumbersome/weak compared to the MC script, & that the script is out classed by C++ MFC. 
I was more thinking that you can write stuff in .bat script but you do not change the UI of Window Explorer or other programs from .bat script
They work against the same filesystem but there are separate layers of viewing it.

I noticed on the 1481 update today that array processing by functions has changed.  If I understand this correctly it looks like scripting will be quicker & easier. 
That was only a bug fix when inilizing array using single quote characters instead of double.
instead of
@var $arr[] = { "string1" , "string2" }
one can write
@var $arr[] = { 'string1' , 'string2' }  <<= did not work before, works is 1481+

Why single quote? if you need to have " inside the string. 
Instead of writing @var $text = "\"1\" is good" you can write @var $text = 'string "1" is good'

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #15 on: August 27, 2013, 13:38:36 »
Thanks for the detailed response Mathias.

I did a tiny bit of C++ years ago.  I expect a lot has changed.  I didn't do enough to remember much. 
I would quite like to try it again - time permitting.
What kind of things would you describe as plug-ins or extensions?  If I want to attempt that I would need some guidelines to avoid major disasters!  :D

The bug fix may well help me, I am using "" quite a lot. 

Going OT -
When you designed MC script what did you use?  Did you take an existing code library or did you write it directly to hardware code?  (Maybe this is a big question?)
To me, a scripting language seems very ambitious, but also a great way to make something you can tailor to your needs.
I think that dynamic is why I am starting to get more from MC - I can tailor things instead of looking for a tool that may do what I want.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #16 on: August 27, 2013, 14:01:31 »
I did a tiny bit of C++ years ago.  I expect a lot has changed.  I didn't do enough to remember much. 
I would quite like to try it again - time permitting.
What kind of things would you describe as plug-ins or extensions?  If I want to attempt that I would need some guidelines to avoid major disasters!  :D
MC is built around a plugin/extension architecture. Everything in MC is almost an plugin. (MultiRename, Checksum, Zip, Rar
Even the "Explorer Panel" in MC is a plugin. (Built in.. and some minor cheats. ) Look in Configuration => Manage Extensions and plugins.

So the API to MC is extensive and some parts is a bit advanced. So you will need some C++ experience to be able to do it..

Going OT -
When you designed MC script what did you use?  Did you take an existing code library or did you write it directly to hardware code?  (Maybe this is a big question?)
To me, a scripting language seems very ambitious, but also a great way to make something you can tailor to your needs.
I think that dynamic is why I am starting to get more from MC - I can tailor things instead of looking for a tool that may do what I want.
I build the script engine my self from scratch.. From the beginning it was only meant to be something simple to execute multiple program and with very limited features. Like a simple .bat script engine but that could connect to MC.
But it grow with more and more features and become way way more complex then first intended.
I been thinking about replacing it with a javascript engine instead. But I have not found one that is small enough and flexible enough to use.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #17 on: August 28, 2013, 13:31:47 »
I see.  That all makes sense.  I can see why the complete 'explorer replacement' path could be a big step code-wise in this case.
That said, at the moment even without bypassing/removing the clutter of Win Explorer you have achieved a great deal. 
I will have to spend more time reading your documentation & see if anything clicks for me.

I guess using Java would increase functionality, & might increase your script user base if it has a common basis of commands & syntax. 
I personally find java apps don't run well on my system.  Not sure why.  Java is present & says 100% but...
I can put the same app in a VM & it will run fine.   
Most common answer I hear is that it is AV related - if so how would the AV react to a java-based MC scripting engine? 





Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Rename & move/copy
« Reply #18 on: August 28, 2013, 13:52:55 »
I guess using Java would increase functionality, & might increase your script user base if it has a common basis of commands & syntax. 
I personally find java apps don't run well on my system.  Not sure why.  Java is present & says 100% but...
I can put the same app in a VM & it will run fine.   
Most common answer I hear is that it is AV related - if so how would the AV react to a java-based MC scripting engine? 

No no no..
JavaScript is not the same as Java.  Java that runs using VM is something completely different.
http://kb.mozillazine.org/JavaScript_is_not_Java

Javascript is used a lot in webpages. so a lot of people know how it works. And there are good references about it on the net.


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Rename & move/copy
« Reply #19 on: August 28, 2013, 19:37:23 »
Hmm well I might be forgiven for thinking you might write a Java based script from scratch using the Java language....!   :D