Multi Commander > Script

Rename & move/copy

(1/4) > >>

Ulfhednar:
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):
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:
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: ---@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" );
  }

--- End code ---




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: ---@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 );
}

--- End code ---

That also worked on it's own...




Mathias (Author):
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:
- 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: ---@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 );
}
--- End code ---

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.  ;)

Navigation

[0] Message Index

[#] Next page

Go to full version