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.0
Updated on 01.03.2009
args List, Template, Delim
local fh = file.open(List, "r")
local Slash = "\"
local Path, Name, i, Dir, Str, Depth, NewName, Ext

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) - 1
			NewName = Name
			for(i = 1; i <= Depth; i++)
				if(index(Template, i)) do
					Dir = word(Path, Depth - i + 2, Slash)
					NewName = Dir ++ Delim ++ NewName
				endif
			endfor
			file.rename(Path ++ Name ++ Ext, Path ++ NewName ++ Ext)
		endif
	endfor
else
	messagebox("ok error", "File not opened", "FILE plugin error")
	quit
endif

file.close(fh)

Comments:

Suppose there is file c:\One\Two\Three\Four\name.txt. 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.txt name. Here the minus character is used as a delimiter.

To insert the next (grand-parent) level directory name (Threename.txt) 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.txt.

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 (in any order) where each number corresponds to the level of ancestor. As it was said above, the parent directory is considered to be level 1, grand-parent - level 2, etc. Third parameter is an optional delimiter.

Script with the ("%L", "13", "%") parameters renames the file to "Two%Four%name.txt" in the same directory. Another example: ("%L", "243", "-") parameters correspond to "One-Two-Three-name.txt".


Main Page Total Commander PowerPro