RenAncestry

Title RenAncestry
File Name RenAncestry.txt
Description Rename files using names of ancestors of any level
Author Vochomurka
Parameters ("%L", Template, Delimiter)
Plugins Called file
Icon
Version 1.1
Updated on 18.04.2019
args List, Template, Delim
local fh = file.open(List, "r")
static Slash = "\"
local LT = length(Template)
local Path, Name, i, Dir, Str, Depth, NewName, Ext, Char

if(fh > 0) do
	for(not(file.eof(fh))) 
		Str = file.readstring(fh)
		if(Str) do
			Path = file.folder(Str) ++ Slash
			Name = file.name(Str)
			Ext = "." ++ file.type(Str)
			Depth = word(Path, 0, Slash)
			NewName = Name

			for(i = LT; i > 0; i--)
				Char = Template[i - 1]
				Dir = word(Path, Depth - Char + 1, Slash)
				NewName = Dir ++ Delim ++ NewName
			endfor
		endif
		file.rename(Path ++ Name ++ Ext, Path ++ NewName ++ Ext)
	endfor
else
	messagebox("ok error", "File not opened", "FILE plugin error")
endif

file.close(fh)

Comments:

Suppose there is file c:\One\Two\Three\Four\name.ext. It is required to rename it using names of parent directories of any level.

For the first level counting from the file to the root the Multi-Rename Tool (MRT) of Total Commander can provide, for example, the [P]-[N] mask that yields the Four-name.ext name. Here the minus character is used as a delimiter.

To insert the next (grand-parent) level directory name (Threename.ext) the [G][N] mask can be applied.

Unfortunately, MRT can't use names of grand-grand-parent and higher, though the filex plugin and its function GtGrandPaDir can make up this deficiency: mask [=filex.GtGrandPaDir]$[N] renames file to Two$name.ext.

But even the filex plugin is not able to move upwards in the directory hierarchy. For these purposes the present script can be easily used.

Its first parameter is always "%L". Second one is a template. It contains numbers where each number corresponds to the level of ancestor. As it was said above, the current directory is considered to be level 1, parent - level 2, etc. Third parameter is an optional delimiter.

Script with the ("%L", "13", "%%") parameters renames the file to "Four%Two%name.ext" in the same directory (the percent sign must be specified twice). Another example: ("%L", "243", "-") parameters correspond to "Three-One-Two-name.ext".


Main Page Total Commander PowerPro