#1 2016-04-02 10:43

Dirk
Member
Registered: 2013-04-05
Posts: 91

How to remove all folders of the 2. hierarchy which have only 1 subfol

A folder (1) contains an uncertain amount of sub folders (second hierarchy). Each folder in the 2. hierarchy can contain at least one item (folder or file) or more. When a folder of the second hierarchy contains only one subfolder (no more items) this subfolder shall be remove (without its content) so that the subfolder (third hierarchy) of the folder of the second hierarchy becomes a subfolder of the second hierarchy.

How could I do it?

Each subfolder of the third hierarchy contains at least one item (file or folder) and has an uncertain name, the name can be the same one as its superior folder.

For example:
I:\folder 1\folder 2\folder 3
folder 2 has only one subfolder (no more items), "folder 3", therefor folder 3 shall go to:
I:\folder 1\folder 3

For example
I:\folder 1\folder 2\folder 3
I:\folder 1\folder 2\file.xxx
folder 2 has one subfolder and one item, therefore it keeps where it is

Offline

#2 2016-04-02 14:59

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

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

A script below can accomplish a major part of this task.

Add all files (and only files) by adding the root folder. The script will check if subfolder of each file contains any files or any folders (except the parent folder of the current file), and it will insert "..\" into file name if its subfolder is effectively empty.

One thing that this script doesn't do is removing the empty folders. This could be accomplished by another script or some file system cleaning tool.

var
  SubFolderPath: WideString;
  Items: TStringsArray;

begin
  // Path to subfolder of current file
  SubFolderPath := WideExtractFileDir(WideExtractFileDir(FilePath));

  // Find all files in subfolder
  SetLength(Items, 0);
  WideScanDirForFiles(SubFolderPath, Items, False, False, False, '');

  // If subfolder has any files
  if Length(Items) > 0 then
    Exit;

  // Find all folders in subfolder
  SetLength(Items, 0);
  WideScanDirForFolders(SubFolderPath, Items, False, False, False);

  // If subfolder has any folders (excluding the parent folder of current file)
  if Length(Items) > 1 then
    Exit;

  // If we got here, then move current file
  FileName := '..\' + FileName;
end.

Offline

#3 2016-04-02 20:40

Dirk
Member
Registered: 2013-04-05
Posts: 91

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

Very great, thank you very much for the script.

Sorry, I cannot get it to work.

I added the script:
UxMAIhp.png

Added the files:
0KhdSuR.png
CreqDil.gif

The test folders:
PhnG7dm.png
T7645Pk.png

This (for folder 3) should be the end position (all of its files kept were there were):

qgipQzv.png

One thing that this script doesn't do is removing the empty folders. This could be accomplished by another script or some file system cleaning tool.

Yes, that doesn't matter, I have a program doing it.

Many thanks again

Last edited by Dirk (2016-04-02 20:41)

Offline

#4 2016-04-02 21:37

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

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

It may be a silly question, but did you click Preview and then Rename?

To help debug this, please provide an export of your files table:

  1. Enable "Folder", "Name" and "New Name" columns only

  2. Add all your files

  3. Add Pascal Script rule

  4. Click Preview

  5. Click Export -> Export all columns to clipboard

Offline

#5 2016-04-02 21:48

Dirk
Member
Registered: 2013-04-05
Posts: 91

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

It may be a silly question, but did you click Preview and then Rename?

No, no, not silly at all, yes, I did. Without doing it this message appears:

OUtN0LU.png

C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	3333.nfo	..\3333.nfo
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie (2) - Kopie.txt	..\Files - Kopie (2) - Kopie.txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie (2).txt	..\Files - Kopie (2).txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie (3).txt	..\Files - Kopie (3).txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie - Kopie (2).txt	..\Files - Kopie - Kopie (2).txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie - Kopie - Kopie.txt	..\Files - Kopie - Kopie - Kopie.txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie - Kopie.txt	..\Files - Kopie - Kopie.txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files - Kopie.txt	..\Files - Kopie.txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\	Files.txt	..\Files.txt
C:\Test - Folder 1\Folder 2 - only 1 subfolder\Folder 3\Folder 3 - Kopie (2)\	files.txt	files.txt
C:\Test - Folder 1\Folder 2a\	Help.Dat	Help.Dat
C:\Test - Folder 1\Folder 2a\	Registry.rw.tvr	Registry.rw.tvr
C:\Test - Folder 1\Folder 2a\	Registry.rw.tvr.lck	Registry.rw.tvr.lck
C:\Test - Folder 1\Folder 2a\	Registry.rw.tvr.transact	Registry.rw.tvr.transact
C:\Test - Folder 1\Folder 2a\	Registry.tlog	Registry.tlog
C:\Test - Folder 1\Folder 2a\	Registry.tlog.cache	Registry.tlog.cache

Offline

#6 2016-04-03 00:38

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

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

The export shows that the script is doing its job, just click Rename.

All files which have "..\" prefix will be moved to their subfolder, as you wanted.

Offline

#7 2016-04-03 09:59

Dirk
Member
Registered: 2013-04-05
Posts: 91

Re: How to remove all folders of the 2. hierarchy which have only 1 subfol

Thank you very much.

The export shows that the script is doing its job, just click Rename.

Yes, I had tried it a few times, but...I left ReNamer open like it was tonight, just pressed the rename button in the morning and suddenly it worked, very strange; do not have any idea what I could have done wrong, sorry.

All files which have "..\" prefix will be moved to their subfolder, as you wanted.

The entire content (so the subfolders - if available - and their subfolders -if available - and so on - if available - and its folder structure(s)) shall be kept in the folders (keeping their folder names) which become a subfolder in the second hierarchy

A folder of the 2nd hierarchy can also be empty, but I assume such a folder will be left like it is. Or I could remove all of the empty folders before starting the script.

Offline

Board footer

Powered by FluxBB