Page 1 of 1

Rename files based on folder name

Posted: 03 Nov 2014 17:50
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?

Re: Rename files based on folder name

Posted: 03 Nov 2014 18:12
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.

Re: Rename files based on folder name

Posted: 03 Nov 2014 18:36
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.

Re: Rename files based on folder name

Posted: 03 Nov 2014 19:18
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...

Re: Rename files based on folder name

Posted: 04 Nov 2014 19:02
by tomuser
Better, thanks. Working only with selected folders would be too much to ask? :D

Re: Rename files based on folder name

Posted: 04 Nov 2014 19:17
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"; }

Re: Rename files based on folder name

Posted: 05 Mar 2015 10:13
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)

Re: Rename files based on folder name

Posted: 05 Mar 2015 10:51
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;

Re: Rename files based on folder name

Posted: 05 Mar 2015 11:34
by yusef88
very thanks highend

Re: Rename files based on folder name

Posted: 06 Mar 2015 04:45
by Papoulka
Yes, thank you highend. Sometimes I follow your threads just for the pleasure of being impressed.

Re: Rename files based on folder name

Posted: 05 Apr 2018 18:28
by EastBasin
Why do some file names begin with "~?"?

Re: Rename files based on folder name

Posted: 05 Apr 2018 18:45
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.