Rename files based on folder name

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
tomuser
Posts: 135
Joined: 04 Dec 2009 18:52

Rename files based on folder name

Post by tomuser »

Hi

Is there any quick method to rename files in folders based on folder name where they are? For example I "have" folder named DATA under My Documents. And under there are sub-folders "2012", "2013", "2014". In each of this folder there are several files with different extensions (info.txt, sample.jpg, test.avi, etc). Now how to get these files renamed to 2012.txt, 2012.jpg, 2012.avi; 2013.txt, 2013.jpg, 2013.avi; 2014.txt, 2014.jpg, 2014.avi? If I'm in 2012 folder then I can use <curfolder> pattern in Batch Rename and it does the job. But I have hundreds of such folders and doing it manually in each of these folders is time consuming. If I do branch view in DATA folder to see all files in sub-folders and select all these files then I end up with every file being renamed to DATA.txt; DATA.jpg, DATA.avi in each folder. And this what I do not want.

I tried program named Advanced Renamer and there is possible to use such variable as <DirName:1> which gives exactly what I want. Number in it marks folders starting counting from file and goes up. <DirName:1> is folder name where current file is (first folder). <DirName:2> there would go 1 folder up meaning second folder up from file. In my case it would give DATA. Using <DirName:3> would go again 1 folder up and give My Documents, etc. Very easy to handle and this would be exactly what I need.

And now if we go back to topic then question is is this achievable somehow in XYplorer too?

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename files based on folder name

Post by highend »

E.g. via a script (you must have the pro version ofc):

Code: Select all

    setting "BackgroundFileOps", 0;
    $list = "";
    foreach($file, folderreport("files", "r", , "r", , "<crlf>"), "<crlf>") {
        $newName = renameitem(getpathcomponent($file, "parent"), $file, , "-01");
        if ($newName) { $list = $list . "Src: $file<tab 2>Dst: $newName<crlf>"; }
    }
    text $list;
You start this script from inside a directory that contains all files (and subdirs with files) that should get the name of their parent directory.
I've added an additional output so you can see which file was renamed to what...

Try it with some test files / dirs first.
One of my scripts helped you out? Please donate via Paypal

tomuser
Posts: 135
Joined: 04 Dec 2009 18:52

Re: Rename files based on folder name

Post by tomuser »

Thanks. Will try it and investigate it.

I noticed that something is confusing the script. Some files were renamed nicely some not. May it be related that some folders have "." in their names? Files lost their extensions.

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename files based on folder name

Post by highend »

Yeah, folders with dots in them are evil xD

Try this:

Code: Select all

    setting "BackgroundFileOps", 0;
    $list = "";
    foreach($file, folderreport("files", "r", , "r", , "<crlf>"), "<crlf>") {
        $newName = replace(gettoken($file, -2, "\"), ":") . "." . getpathcomponent($file, "ext");
        $newName = renameitem($newName, $file, , "-01");
        if ($newName) { $list = $list . "Src: $file<tab 2>Dst: $newName<crlf>"; }
    }
    text $list;
This should (hopefully) work for all files (regardless of folder name)...

Again, test it first...
One of my scripts helped you out? Please donate via Paypal

tomuser
Posts: 135
Joined: 04 Dec 2009 18:52

Re: Rename files based on folder name

Post by tomuser »

Better, thanks. Working only with selected folders would be too much to ask? :D

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename files based on folder name

Post by highend »

Working only with selected folders...

Code: Select all

    setting "BackgroundFileOps", 0;
    $list = "";
    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) == 2) {
            foreach($file, folderreport("files", "r", $folder, "r", , "<crlf>"), "<crlf>") {
                $newName = replace(gettoken($file, -2, "\"), ":") . "." . getpathcomponent($file, "ext");
                $newName = renameitem($newName, $file, , "-01");
                if ($newName) { $list = $list . "Src: $file<tab 2>Dst: $newName<crlf>"; }
            }
        }
    }
    if ($list) { text $list; }
    else       { status "No files were renamed, no folder selected?", "8B4513", "stop"; }
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: Rename files based on folder name

Post by yusef88 »

highend wrote:Yeah, folders with dots in them are evil xD

Try this:

Code: Select all

    setting "BackgroundFileOps", 0;
    $list = "";
    foreach($file, folderreport("files", "r", , "r", , "<crlf>"), "<crlf>") {
        $newName = replace(gettoken($file, -2, "\"), ":") . "." . getpathcomponent($file, "ext");
        $newName = renameitem($newName, $file, , "-01");
        if ($newName) { $list = $list . "Src: $file<tab 2>Dst: $newName<crlf>"; }
    }
    text $list;
This should (hopefully) work for all files (regardless of folder name)...

Again, test it first...
good script :tup:
can it work for current folder only (don't rename files in sub folders)

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename files based on folder name

Post by highend »

can it work for current folder only (don't rename files in sub folders)

Code: Select all

    setting "BackgroundFileOps", 0;
    $list = "";
    $curBase = gettoken("<curpath>", -1, "\");
    foreach($file, listpane(, , 1)) {
        $newName = $curBase . "." . getpathcomponent($file, "ext");
        $newName = renameitem($newName, $file, , "-01");
        if ($newName) { $list = $list . "Src: $file<tab 2>Dst: $newName<crlf>"; }
    }
    text $list;
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: Rename files based on folder name

Post by yusef88 »

very thanks highend

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Rename files based on folder name

Post by Papoulka »

Yes, thank you highend. Sometimes I follow your threads just for the pleasure of being impressed.

EastBasin
Posts: 2
Joined: 02 Apr 2018 22:01

Re: Rename files based on folder name

Post by EastBasin »

Why do some file names begin with "~?"?

highend
Posts: 15004
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename files based on folder name

Post by highend »

Normal files can't have a question mark inside their name. If they really do have one, the file system rules have been circumvented.
One of my scripts helped you out? Please donate via Paypal

Post Reply