#1 2023-04-20 03:02

adiosdonut
Member
Registered: 2021-11-09
Posts: 8

How to add name of folder 2 levels up as prefix?

This has probably been asked before. I did a search and found similar posts with solutions, but none that matched exactly what I need.
And unfortunately, due to my lack of coding knowledge, I don't even know how to adapt those solutions to what I'm looking for.

Anyways, here's what I'm trying to do--

I have about 50 folders (with unique names), each containing a subfolder (with unique name) and in each subfolder a bunch of files.
I know there's a way to add the name of the folder containing the file to the file name.
But what I want to do is add the name of the folder two levels up, followed by an underscore, as a prefix to each of the files.

For example:

parent/subfolder-level-2/subfolder-level1/file.txt ==> subfolder-level-2_file.txt
parent/1/a/apple.png ==> 1_apple.png
parent/1/a/banana.txt ==> 1_banana.txt
parent/2/b/apple.jpg ==> 2_apple.txt
parent/2/b/kiwi.pdf ==> 2_kiwi.pdf
parent/3/c/cherry.png ==> 3_cherry.png

Thanks!

Last edited by adiosdonut (2023-04-20 03:03)

Offline

#2 2023-04-20 10:48

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: How to add name of folder 2 levels up as prefix?

This script will insert the 2nd level parent folder name followed by an underscore and the original file name.

var
  ParentDir1, ParentDir2: WideString;
begin
  ParentDir1 := WideExtractFileDir(FilePath);
  ParentDir2 := WideExtractFileDir(ParentDir1);
  ParentDir2 := WideExtractFileName(ParentDir2);
  FileName := ParentDir2 + '_' + FileName;
end.

It is almost identical (functionally) to your older topic: How to insert name of folder one level up?.

Also, a similar task has been discussed in topics #2158 and #2405.

Offline

#3 2023-04-20 14:16

adiosdonut
Member
Registered: 2021-11-09
Posts: 8

Re: How to add name of folder 2 levels up as prefix?

Beautiful! Does exactly what I needed. smile
Thank you!

Offline

Board footer

Powered by FluxBB