Author Topic: Rename window with filename only  (Read 469 times)

culturework

  • Newbie
  • *
  • Posts: 13
    • View Profile
Rename window with filename only
« on: June 12, 2026, 09:08:14 »
A fresh user here, greetings to all and big thanks to dev for this impressive soft.

Question:
I am using this User Command:
#options (RunSeparate)
MC.Filesystem.Rename ASKNAME

what can I do to have this window with filename only, without ext?
I am frequently adding strings to names, so current name in the box is needed, but don't need extension.
I am new to this and still struggling to understand scripting in MC.

culturework

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rename window with filename only
« Reply #1 on: Today at 19:48:11 »
for possible future seekers  :)

Multi-Script does the trick (box with filename only), ext stays the same:

function main()
{
    @var $arrfullpath = GetSelectedPaths();
    @var $arrnames = GetSourceSelectedFileNames();
    @var $count = arrayCount($arrnames);

    if ($count == 1)
    {
        @var $fullpath = $arrfullpath[0];
        @var $name = $arrnames[0];

        @var $dotpos = StrRegExpFind($name, "\.[^.]*$");
        @var $base;
        @var $ext;

        if ($dotpos == -1 || $dotpos == 0)
        {
            $base = $name;
            $ext = "";
        }
        else
        {
            $base = StrSub($name, 0, $dotpos);
            $ext  = StrSub($name, $dotpos, -1);
        }

        @var $newbase = AskText("Rename", $base, 0);
        @var $cancelled = StrIsEqual( StrFormat("{0}", $newbase), "0" );

        if ($cancelled && !StrIsEqual($base, "0"))
        {
            // user cancelled, do nothing
        }
        else if (StrIsEqual($newbase, ""))
        {
            MessageBox("Rename", "Name cannot be empty.", 0);
        }
        else
        {
            @var $newname = $newbase + $ext;
            @var $samename = StrIsEqual($newname, $name);

            if ($samename == 0)
            {
                @var $res = RenameFile( $fullpath, $newname, "" );
                if ($res == 0)
                {
                    MessageBox("Rename failed", "Could not rename to: " + $newname, 0);
                }
            }
        }
    }
    else if ($count == 0)
    {
        MessageBox("Rename", "No file selected.", 0);
    }
    else
    {
        MessageBox("Rename", "Please select only one file for this command.", 0);
    }
}
main();



If you want two sequential windows, first with filename only, second with extension only:

function main()
{
    @var $arrfullpath = GetSelectedPaths();
    @var $arrnames = GetSourceSelectedFileNames();
    @var $count = arrayCount($arrnames);

    if ($count == 1)
    {
        @var $fullpath = $arrfullpath[0];
        @var $name = $arrnames[0];

        @var $dotpos = StrRegExpFind($name, "\.[^.]*$");
        @var $base;
        @var $ext;

        if ($dotpos == -1 || $dotpos == 0)
        {
            $base = $name;
            $ext = "";
        }
        else
        {
            $base = StrSub($name, 0, $dotpos);
            $ext  = StrSub($name, $dotpos, -1);
        }

        // strip leading dot for display only
        @var $extIsEmpty = StrIsEqual($ext, "");
        @var $extNoDot = $ext;
        if ($extIsEmpty == 0)
        {
            $extNoDot = StrSub($ext, 1, -1);
        }
        Log(0, 10, "RenameNoExt: ext=[" + $ext + "] extNoDot=[" + $extNoDot + "]");

        // --- Dialog 1: filename ---
        @var $newbase = AskText("Rename - filename", $base, 0);
        @var $baseCancelled = StrIsEqual( StrFormat("{0}", $newbase), "0" );
        @var $baseIsZeroText = StrIsEqual($base, "0");

        if ($baseCancelled && $baseIsZeroText == 0)
        {
            // cancelled dialog 1
        }
        else if (StrIsEqual($newbase, ""))
        {
            MessageBox("Rename", "Filename cannot be empty.", 0);
        }
        else
        {
            // --- Dialog 2: extension, shown WITHOUT the leading dot ---
            @var $newextRaw = AskText("Rename - extension", $extNoDot, 0);
            Log(0, 10, "RenameNoExt: newextRaw=[" + $newextRaw + "]");

            @var $extCancelled = StrIsEqual( StrFormat("{0}", $newextRaw), "0" );
            @var $extNoDotIsZeroText = StrIsEqual($extNoDot, "0");

            if ($extCancelled && $extNoDotIsZeroText == 0)
            {
                // cancelled dialog 2
            }
            else
            {
                @var $newext = $newextRaw;
                @var $stillHasDot = 1;
                while ($stillHasDot == 1)
                {
                    @var $curEmpty = StrIsEqual($newext, "");
                    if ($curEmpty == 1)
                    {
                        $stillHasDot = 0;
                    }
                    else
                    {
                        @var $firstChar = StrSub($newext, 0, 1);
                        @var $firstIsDot = StrIsEqual($firstChar, ".");
                        if ($firstIsDot == 1)
                        {
                            $newext = StrSub($newext, 1, -1);
                        }
                        else
                        {
                            $stillHasDot = 0;
                        }
                    }
                }

                @var $newextEmpty = StrIsEqual($newext, "");
                if ($newextEmpty == 0)
                {
                    $newext = "." + $newext;
                }
                Log(0, 10, "RenameNoExt: final newext=[" + $newext + "]");

                @var $newname = $newbase + $newext;
                @var $samename = StrIsEqual($newname, $name);

                if ($samename == 0)
                {
                    @var $res = RenameFile( $fullpath, $newname, "" );
                    Log(0, 10, "RenameNoExt: RenameFile returned [" + $res + "]");
                    if ($res == 0)
                    {
                        MessageBox("Rename failed", "Could not rename to: " + $newname, 0);
                    }
                }
            }
        }
    }
    else if ($count == 0)
    {
        MessageBox("Rename", "No file selected.", 0);
    }
    else
    {
        MessageBox("Rename", "Please select only one file for this command.", 0);
    }
}
main();