Author Topic: Name as Folder?  (Read 21027 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Name as Folder?
« 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.

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 556
  • Old Skull
    • View Profile
Re: Name as Folder?
« Reply #1 on: July 24, 2013, 12:10:28 »
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.

Code: [Select]
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." );
}
« Last Edit: July 24, 2013, 12:12:33 by Jungle »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4492
    • View Profile
    • Multi Commander
Re: Name as Folder?
« Reply #2 on: July 24, 2013, 18:30:57 »
Wow.. that was a nice script. +2 :) 

If something small is missing to make it more simple let me know.

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 556
  • Old Skull
    • View Profile
Re: Name as Folder?
« Reply #3 on: July 24, 2013, 18:44:51 »
However i have not tested this script with thousands of files, so i'm not sure about perfomance/resource consumption.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Name as Folder?
« Reply #4 on: July 24, 2013, 23:30:06 »
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
« Last Edit: July 24, 2013, 23:34:31 by Ulfhednar »

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 556
  • Old Skull
    • View Profile
Re: Name as Folder?
« Reply #5 on: July 25, 2013, 10:24:56 »
OK. Modified version.
Code: [Select]
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." );
}

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Name as Folder?
« Reply #6 on: July 25, 2013, 18:43:50 »
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 :
Code: [Select]
$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?)








Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 556
  • Old Skull
    • View Profile
Re: Name as Folder?
« Reply #7 on: July 26, 2013, 05:42:25 »
You may replace
Code: [Select]
$n = StrRegExpFind( $s, "\d+$" );
by
Code: [Select]
$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.


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Name as Folder?
« Reply #8 on: July 26, 2013, 09:42:38 »
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.  :)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4492
    • View Profile
    • Multi Commander
Re: Name as Folder?
« Reply #9 on: July 26, 2013, 10:24:17 »
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.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Name as Folder?
« Reply #10 on: July 26, 2013, 18:11:15 »
@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?
« Last Edit: July 26, 2013, 18:37:37 by Ulfhednar »