#1 2018-04-02 22:46

nokkia
New Member
Registered: 2018-04-01
Posts: 5

Insert specific level parent folder name

Hi
I have a similar problem as in Insert parent directory names into the file name,

A list of folders that contains directly files and some that contains a subfolder and then the files.
I want to rename all files with the name of the folder.

I try to use the Pascal script show in the previous message.

I replace the last line: FileName := ParentDir2 + '#' + ParentDir1 + '#' + FileName;
with: FileName := ParentDir2
It work with folder containing a subfolder, but not for folder containing only files.
Using FileName := ParentDir1, do the inverse.
I add a second rule to get back the extension  insert; .:File_Extension:

Is it possible to have both result with one script.

Thank you

Offline

#2 2018-04-04 17:50

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 2,604

Re: Insert specific level parent folder name

If I understood your request correctly, the following script should achieve what you need.

It will replace file names with the name of 2nd level parent folder name, if defined, otherwise it will use 1st level parent folder name. The original extension will be maintained.

var
  ParentDir1, ParentDir2, TargetParentDir: WideString;
begin
  ParentDir1 := WideExtractFileDir(FilePath);
  ParentDir2 := WideExtractFileDir(ParentDir1);
  ParentDir1 := WideExtractFileName(ParentDir1);
  ParentDir2 := WideExtractFileName(ParentDir2);
  if Length(ParentDir2) > 0
    then TargetParentDir := ParentDir2
    else TargetParentDir := ParentDir1;
  FileName := TargetParentDir + WideExtractFileExt(FilePath);
end.

Offline

#3 2018-04-05 02:15

nokkia
New Member
Registered: 2018-04-01
Posts: 5

Re: Insert specific level parent folder name

Hi

It's not working
Probably a bad explanation from me.

The files must always be rename with the name of the 1st level parent folders.
The 1st level parent folders contain only files or a 2nd level parent folder wich contains files.
The 2nd level parent folders name must never be a choice for renaming files

1st level parent folders name A-----> files name A
1st level parent folders name A-----> 2nd level parent folders name X-----> files name A



I try your example and it does rename the files in the 2nd level parent folders to the name of 1st level parent folders but not
the files in the 1st level parent folders.

if I inverse the order in the script to

if Length(ParentDir2) > 0
    then TargetParentDir := ParentDir1
    else TargetParentDir := ParentDir2;

the files directly under the 1st level parent folders are rename ok, but not the files under 2nd level.

I hope my explanation is clear, my english is not the best you could find.

Offline

#4 2018-04-06 10:59

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 2,604

Re: Insert specific level parent folder name

Let's say you have the following paths:

X:\file.txt
X:\A\file.txt
X:\A\B\file.txt
X:\A\B\C\file.txt
X:\A\B\C\D\file.txt

What do you expect to get after renaming?

For example:

X:\file.txt
X:\A\A.txt
X:\A\B\B.txt
X:\A\B\C\C.txt
X:\A\B\C\D\D.txt

Or:

X:\file.txt
X:\A\A.txt
X:\A\B\A.txt
X:\A\B\C\A.txt
X:\A\B\C\D\A.txt

Offline

#5 2018-04-06 14:00

nokkia
New Member
Registered: 2018-04-01
Posts: 5

Re: Insert specific level parent folder name

Hi
Thank you for the time you take.
I  expect the second case
and only 2 level of folders


X:\A\A.txt
X:\A\B\A.txt

Offline

#6 2018-04-06 15:10

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 2,604

Re: Insert specific level parent folder name

nokkia wrote:

X:\A\A.txt
X:\A\B\A.txt

Well, the script posted previously does exactly that...

X:\A\file.txt    ->  X:\A\A.txt
X:\A\B\file.txt  ->  X:\A\B\A.txt

However, you are still missing an explanation about how should the script handle the rest of cases?

X:\file.txt          => ?
X:\A\file.txt        => X:\A\A.txt
X:\A\B\file.txt      => X:\A\B\A.txt
X:\A\B\C\file.txt    => ?
X:\A\B\C\D\file.txt  => ?

Offline

#7 2018-04-06 16:43

nokkia
New Member
Registered: 2018-04-01
Posts: 5

Re: Insert specific level parent folder name

I find out that the script work if I put all the folders directly at the root of the hard disk,
drive D in my case.
But it's not practical, I have a working folder call to be rename

Example of what I have

D:\to be rename\Dumas, Alexandre-La boule de neige\La_boule_de_neige.epub
D:\to be rename\Dumas, Alexandre-La colombe\colombe.epub
D:\to be rename\Dumas, Alexandre-Le capitaine Arena\capitaine\capitaine Arena.epub
D:\to be rename\Dumas, Alexandre-Le corricolo\Le corricolo\corricolo.epub

The renaming should do this

D:\to be rename\Dumas, Alexandre-La boule de neige\Dumas, Alexandre-La boule de neige.epub
D:\to be rename\Dumas, Alexandre-La colombe\Dumas, Alexandre-La colombe.epub
D:\to be rename\Dumas, Alexandre-Le capitaine Arena\capitaine\Dumas, Alexandre-Le capitaine Arena.epub
D:\to be rename\Dumas, Alexandre-Le corricolo\Le corricolo\Dumas, Alexandre-Le corricolo.epub

The first two don't have a second level folder.
The two others have a second level folder.

Most of the folders contains only files
but a few have a second level folder and sometimes a third level folder.

Last edited by nokkia (2018-04-06 16:58)

Offline

#8 2018-04-06 21:02

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 886

Re: Insert specific level parent folder name

So you want to rename always with name from a top folder at a special hierarchy level,

here the third level (number 2 in array speak, as we start count from zero) in your case

D:\to be rename\Dumas, Alexandre-La boule de neige\La_boule_de_neige.epub
0..\.......1.........\......2...........................................\...file
D:\to be rename\Dumas, Alexandre-Le corricolo\Le corricolo\Dumas, Alexandre-Le corricolo.epub
0..\.......1.........\......2...................................\.......3......\...file


the fifth level (4) on my test folder:
d:\temp\ReNamer Test\file name like folder\Artist\Sub folder\file
0.\...1...\.........2........\........3..................\...4...\....5........\..



any file no matter how many sub folders are there, in your case none or one.


So I want to provide another approach

//Script to get the name of a folder at a specific hierarchy level
var
  FolderArray: TStringsArray;
  ArtistFolderLevel: Integer;
  ArtistFolderName: WideString;

begin
      FolderArray := WideSplitString(FilePath, '\');

      //d:\temp\ReNamer Test\file name like folder\Artist\Sub folder\file
      //0 \  1 \     2      \     3               \  4   \  5       \
      //ArtistFolderLevel:=4;
      //D:\to be rename\Dumas, Alexandre-La boule de neige\La_boule_de_neige.epub
      //0 \    1       \               2                  \
      ArtistFolderLevel:=2;

      ArtistFolderName := FolderArray[ArtistFolderLevel];
      //showmessage(ArtistFolderName);

  FileName := ArtistFolderName + WideExtractFileExt(FilePath);
end.

For a how to use a script see >>> http://www.den4b.com/wiki/ReNamer:Rules:PascalScript




HTH? big_smile


For more help and how-to's  read our fine Wiki.
If ReNamer had helped you, please donate a coin to Denis or buy a Pro license for commercial use. (Lite vs Pro)

Offline

#9 2018-04-06 21:48

nokkia
New Member
Registered: 2018-04-01
Posts: 5

Re: Insert specific level parent folder name

Great It work

Many thanks to den4b and Stefan.
Fantastic piece of software this renamer

Offline

Board footer

Powered by FluxBB