1012 Votes

Rename File to its Folder Name

Tutorial by Stefan Trost | Last update on 2023-02-15 | Created on 2016-04-23

Recently, someone asked me whether it is possible to use the FileRenamer for renaming a file in a way that the folder name of the file becomes part of the filename. At this time, it was not possible. However, in between, I have developed such a function for the FileRenamer and in this tutorial, I would like to show you how to use it.

The program File Renamer used here can be downloaded for free on this page for Windows, macOS and Linux. For renaming we are using placeholders in all examples which are explained here in detail but also in the following.

In each case, first, we have to add the files we would like to rename to the application. This can be done, for example with dragging the files onto the program. You can adjust the desired changes on the right of the program, you can see a preview directly in the file list in the column "Preview" and with clicking the button "Apply Changes", you can rename all files from the list.

In the following we would like to have a look at the following application examples for renamings with folder names:

File Name should become a Folder Name

In our first example, we would like to rename a file directly into one of its folder names. This means that the file should be completely renamed to the folder name and the new file name should not keep any components of the old name. So for example:

BEFORE:  C:\Vacations\Trip to Norway\Video.mp4
AFTER:   C:\Vacations\Trip to Norway\Trip to Norway.mp4

For this renaming, we are using the function "Mask" that you can find on the right side of the FileRenamer under "Changes" in the section "Name". In this section you will find all settings for changing the file name, which we will also use in the next examples. As mask, we are using "%folder-1%" with which we are directly getting what we want. The placeholder %folder-1% is standing for the first folder below the file ("Trip to Norway" in this case).

File name should be supplemented by a Folder Name

In our next example, we would like to supplement our origin filename with the name of the first direct folder with adding the name of the folder to the beginning of the file name. That is for example:

BEFORE:  C:\Vacations\Trip to Norway\Image 1.jpg
AFTER:   C:\Vacations\Trip to Norway\Trip to Norway Image 1.jpg

For getting this result, this time we use the function "Append" and enter the text "%folder-1% " into the field with the heading "Prepend this Text to the Front". As you can see, we have inserted a space behind the placeholder %folder-1% so that the letters of the folder name and the file name will not be written directly after each other.

Also for this renaming, we could alternatively use the "Mask" function that we have introduced in the first example. For this result, the mask "%folder-1% %name%" would have to be used. The placeholder %folder-1% again stands for the first folder below the file (here "Trip to Norway") and the placeholder %name% will be replaced with the old name of the file ("Image 1" here). Additionally, we are writing a space between the placeholders to ensure a distance between the two components.

File Name should contain multiple Folder Names

Beyond using the direct folder as we have done in the first two examples, it is also possible to reference deeper folders and to mix multiple folders. The placeholder %folder-1% always stands for the direct folder, the placeholder %folder-2% for the folder name one level deeper and so on. Here is an example:

BEFORE:  C:\Documents\Work\2022\Ready\01\Invoice.pdf
AFTER:   C:\Dokuments\Work\2022\Ready\01\Work Invoice 012345.pdf

In this example, accordingly, %folder-1% is standing for the folder "01", %folder-2% is standing for the folder "Ready", %folder-3% is standing for the folder "2022", %folder-4% is standing for the folder "Work" and %folder-5% is standing for the folder "Documents".

We want to rename our file to "Work Invoice 01-2022.pdf". For that, we are using the mask "%folder-4% %name% %folder-1%-%folder-3%". As you can see, we can arbitrarily order the placeholders, we can omit folder levels and we can mix them up with other characters.

Count Folders from below or above

With the previously discussed placeholders %folder-1%, %folder-2%, %folder-3% and so on, we have counted the folder in the path from above, that means from the direction of the file (%folder-1% is the name of the folder directly below the file, %folder-2% is the name of the folder one level below and so on).

However, if we use positive numbers instead of the negative numbers such as -1, -2, and -3, that is, using the wildcards %folder1%, %folder2%, %folder3% and so on, we can also count from the other direction respectively from below: %folder1% is accordingly the first folder in the path, %folder2% is the second folder in the path, and so on.

Let's look at an example in which we use the mask "%folder1%-%folder-1%":

MASK:   %folder1%-%folder-1%

BEFORE: C:\1\2\Text.txt
AFTER:  C:\1\2\1-2.txt

BEFORE: C:\1\2\3\Text.txt
AFTER:  C:\1\2\3\1-3.txt

With this mask we rename a file into the names of the first and the last folder of its path (the name of the first folder counted from below and the name of the first folder counted from above). From the perspective of both files, the first folder from below has the name "1" while the first folder counted from above respectively the last folder of the path differentiates between these files.

If we would use the placeholders %folder2% and %folder-2% instead, for example in the mask "%folder2%-%folder-2%", the result for the two files would look like this:

MASK:   %folder2%-%folder-2%

BEFORE: C:\1\2\Text.txt
AFTER:  C:\1\2\2-1.txt

BEFORE: C:\1\2\3\Text.txt
AFTER:  C:\1\2\3\2-2.txt

Regardless of whether we count from above or from below, the name of the second folder in the path for the second file in both cases is "2", so that this file would receive the file name "2-2" with this mask, even if both components are based on different placeholders. Since the path of the first file consists of only 2 folders, we receive, in contrast to the first example, an exchanged order for this case.

Delete, Insert, Append or Replace Folder Name in File Name

The placeholders standing for the folder names can also be used in the functions "Search and Replace", "Insert", "Append" and "Delete Text". So, you are able to replace parts of the old filename with folder names, you can insert the name of the folder at the beginning, the end or at an arbitrary position within the file name or you can selectively delete corresponding folder names from file names.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.