Multi Commander Support Forum
Multi Commander => Feature Requests and Suggestions => Topic started by: Ulfhednar on July 23, 2013, 14:31:36
-
Is it possible for me to run a script that would name files as their parent folder? This would be a command I could allocate to a button ideally.
e.g.
\tortoise
file.avi
to
\tortoise
tortoise.avi
or (more tricky)
\Flowers
file01.jpg
file02.jpg
to
\Flowers
Flowers01.jpg
Flowers02.jpg
I can see that I would also need to run a numbering routine in this example unless I can exclude numerals somehow.
-
You can test the following code. But it doesn't check whether existing names have ending numerals.
Backup your files before testing and perform testing it in the temporary folder.
Log( 1, 10, "Renaming files to %%parent_folder_name%%[_N].*" );
@var $src_path = GetSourcePath();
@var $arr = PathGetParts( $src_path );
@var $name = $arr[0];
@var $prefix = StrReplace( PathGetPathPart( $src_path, 1 ), $name, "" );
@var $n = StrRFind( $prefix, "\\" );
if ( $n > 0 )
{
$prefix = StrSub( $prefix, $n + 1, StrLen( $prefix ) - $n - 1 );
@var $i;
@var $s;
@var $file;
@var $files;
$arr = GetSourceSelectedPaths();
for ( $i = 0; $i < arrayCount( $arr ); $i++ )
{
$file = $arr[$i];
$s = PathGetFileExtPart( $file );
$name = $prefix + "*" + $s;
$files = FindFiles( $src_path + $name );
$n = arrayCount( $files );
if ( $n == 0 )
{
$s = "";
}
else
{
if ( $n == 1 )
{
$s = $files[0];
RenameFile( $s, StrReplace( $name, "*", "_0" ), "RENAME_RO" );
}
$s = "_" + numtostr( $n );
}
RenameFile( $file, StrReplace( $name, "*", $s ), "RENAME_RO" );
}
}
else
{
Log( 1, 10, "It seems parent folder is root. Renaming impossible." );
}
-
Wow.. that was a nice script. +2 :)
If something small is missing to make it more simple let me know.
-
However i have not tested this script with thousands of files, so i'm not sure about perfomance/resource consumption.
-
Thanks Jungle for a nice script! :)
I see it contains more functions than just getting & implanting the dir name.
I like the numbering/counter for multiple files of the same type - especially as the numbering allows for pre-existing files of the same name.
The following is worth noting as it could cause issues:-
It isn't exclusive of folders, if I 'accidentally' click a sub-folder it will rename as parent dir also.
When I renamed a bunch of jpgs, it dropped the already present numbers & used it's own counter.
I mention it as something a scripting wizard might consider, an advisory to anyone else trying this & as something I should try & figure out...
For pre-existing numerals - how easy is it to exclude already present numbers? (e.g. as with a regex)
Maybe I'd need a 2nd script...
Not saying you should write me one, just wondering as I want to play around with this but need some pointers. ::) :P
-I must find time to learn this multi-scripting & this is an incentive!!
You have given me a good start to look more closely at the MC scripts & a great new functionality for my MC. ;D
-
OK. Modified version.
Log( 1, 10, "Renaming files to %%parent_folder_name%%[_N].*" );
@var $src_path = GetSourcePath();
@var $arr = PathGetParts( $src_path );
@var $name = $arr[0];
@var $prefix = StrReplace( PathGetPathPart( $src_path, 1 ), $name, "" );
@var $n = StrRFind( $prefix, "\\" );
if ( $n > 0 )
{
$prefix = StrSub( $prefix, $n + 1, StrLen( $prefix ) - $n - 1 );
@var $i;
@var $c;
@var $s;
@var $file;
@var $old_file;
@var $files;
@var $mask = "*";
$arr = GetSourceSelectedPaths();
for ( $i = 0; $i < arrayCount( $arr ); $i++ )
{
$file = $arr[$i];
if ( IsFolder( $file ) )
{
continue;
}
$s = PathGetNamePart( $file, 1 );
$n = StrRegExpFind( $s, "\d+$" );
if ( $n < 0 )
{
$s = "";
}
else
{
$s = StrSub( $s, $n, StrLen( $s ) - $n );
}
$name = $prefix + $s + $mask;
$s = PathGetFileExtPart( $file );
$name = $name + $s;
$files = FindFiles( $src_path + $name );
$n = arrayCount( $files );
$s = StrReplace( $name, $mask, "" );
$n = 0;
$c = 0;
while( $n < arrayCount( $files ) )
{
while( $n < arrayCount( $files ) )
{
$old_file = $files[$n];
$old_file = PathGetNamePart( $old_file );
if ( StrIsEqual( $s, $old_file ) )
{
$c++;
$s = StrReplace( $name, $mask, "_" + numtostr( $c ) );
$n = 0;
break;
}
$n++;
}
}
RenameFile( $file, $s, "RENAME_RO" );
}
}
else
{
Log( 1, 10, "It seems parent folder is root. Renaming impossible." );
}
-
Thanks Jungle :)
I tried it & used a date named folder & some "img_###.jpg" files.
I found I got
img_0040.jpg
to become
17-04-20130040.jpg
Which is 'almost' what I would like. I saw you were able to use "_" as a separator in your 1st script.
Obviously I do not know how to write MC script (!!!) but I thought adding it as you have in #66 :
$s = StrReplace( $name, $mask, "_" + numtostr( $c ) );
should work.
Looking through it I guessed entering the _ with the $prefix or the $name might deploy the _ correctly.
e.g "_" $prefix,
but it didn't work.
Having looked at the currently posted docs I am unclear what I might do to alter my output string with added fixed characters. (In this case _).
I can see that being able to script patterns of characters added to folder names could be really useful for things like image files.
The scripting of buttons offers me a lot of possibilities & is an interesting tool. But logically, I probably should use the batch rename instead.
I would appreciate any advice if you have the opportunity. I'm not seeing what I personally need in the current script documentation. I'd like a few walk-thrus. (Maybe that needs to go on the request list for the next release?)
-
You may replace
$n = StrRegExpFind( $s, "\d+$" );
by
$n = StrRegExpFind( $s, "(_|-|~)?\d+$" );
where (_|-|~) is a list of characters that precedes numbers.
But using a special rename tool may indeed be a better idea.
-
Thanks for the explanation Jungle.
I'd looked at that line but don't understand enough MCS syntax to recognize the correct elements yet. :-[
I will run a few tests later - I am pretty sure that for me, the inclusions/adjustments you have made will be faster for my normal routine than the rename tools, so again thanks a lot. :)
-
If you run the script from the debugger.. ( Menu > Help > Multi-Script Debugger ) then you can run the script one line at the time and you can see where stuff goes wrong.
-
@Mathias -
I will keep playing about. Hopefully I will figure it out ;)
@Jungle
That has worked perfectly.
I think I will use your example to try & learn this scripting a little. :)
Maybe this thread should go to the Scripts section now?