Finally got around to this, which is a finally a solid script (that I personally use frequently).
One use is quickly selecting folders part way down a hierarchy to remove intervening folders that are (no longer) desired. An example would be refiling music files if you've used a deep hierarchy system and want to change it - or simply copy several out to transcode and move to a player. Another would be if you have multiple files with useless zipped folder names, extract them all, then only want only the contents, leaving empty folders. If you remove the attribute=directory after the script has left the Find box filled out for you, it can be handy to see all the contents of all folders at a given level, folder and/or file. The thing this is closest to natively is, perhaps, doing a branch view, folders only. This, however, makes the folders you're actually interested in easy to select and move without intervening parents, or things like separate CD1 CD2 folders popping up underneath what you're interested in.
There's a few too many comments in the script, but they help keep me oriented after some time has gone by so I left them.
All hail highend for the main temporary search template script and great advice. I have a tendency to do this kind of thing interactively rather than with a script, which is neither elegant nor as quick. Thanks to Don, however, it is reasonably quick to load a search template and then tweak it, but this is certainly quicker. It also leaves the entry in the Find box afterwards for hand tweaking if desired, as I mentioned - which is particularly helpful if you use special characters in your folder names that conflict with a regular expression and need to be escaped. The main regular expression used, and the regular expression that escapes regex characters from a pathname, are both perhaps worth reusing in your own scripts, or at least are worth glancing at.
Code: Select all
//Version 1.2
//Usage: Find all folders a given distance down the heirarchy from <curfolder>, but no deeper, by entering range 1[- or ,#] using a regex using a self-removing Search Template.
/*
Examples, Notes, and FYIs:
If one had only this path: E:\Here I Am\These Are Garbage\This is What I Want\This is Too Deep\Etc\Etc.txt
If you're in the folder, "Here I Am" and select "2" in the pop-up dialogue, the default,
it will show the folder "This is What I Want" without subfolders or parents,
using the regex: "E:\\Here I Am(\\([^\\]+)){2}$" (FYI: "$" is required.)
Enter "3" and it will show "This is Too Low" instead.
Enter "2-3" or "2,3" and it will show "This is What I Want" along with "This is Too Low".
FYI: You can break out the group (\\([^\\]+)) for a given folder level(s) if you want to further restrict intervening folders using a regex,
FYI: From "Here I Am" with a normal search, using /maxdepth=1 would give you both "These Are Garbage" and "This is What I Want". There is no /mindepth= in XY.
FYI: One can accomplish almost the same thing without a regex by using the following with a Standard search:
"<curpath>\*\*" /maxdepth=1 <-Under "Name" in the Find Box, Quotes required.
which returns the folder "This is What I Want"
/maxdepth=2 would return "This is Too Deep" as well.
Also select: Path, Include subfolders, Auto sync, Attributes, and Attributes=Directory.
*/
$searchTemplate = <<<>>>
[Find]
Version=3
Mode=2
FullPath=1
Case=0
InclSubs=1
Inverted=0
Fuzzy=0
FollowFolderLinks=0
SelectedLocations=0
WholeWords=0
AutoSync=1
IgnoreDiacritics=0
FuzzIndex=0
TypeFilter=0
AttrFindCheck=16
AttrFindNotCheck=0
Mind0=1
Mind3=1
[Named]
Named=
[LookIn]
LookIn="<curpath>"
>>>;
//Input
$levels = input("Enter level...", "<x>[-<y>] subfolders from here", 2); //Depth=2 default.
$range = regexreplace($levels, "(\d+)(?:-|,)(\d+)?", "$1,$2"); //Lets x; x,y; or x-y work as input.
//Prepare Regex into "$named" for Name in Search Template Find Box.
$regexesc = regexreplace(<curpath>, "([][{}()*+?.\\^$|])", "\$1"); //Escape the path for regex use.
$named = $regexesc . "(\\([^\\]+)){$range}$"; //Append main regex for folder depth.
$named = """$named"""; //Put the whole thing in quotes.
//Search Template
$searchTemplate = replace($searchTemplate, "Named=", "Named=" . quote($named));
$templateFile = "<xydata>\FindTemplates\subfolderlevels.ini";
writefile($templateFile, $searchTemplate);
loadsearch "subfolderlevels", "rl";
delete 1, 0, $templateFile;
/*For Version 1.2:
-Commented.
-Escapes regex control characters in starting path by individually replacing
all members of a regex character class containing all regex control characters
with each match slash-escaped: \$1.
For reusability, the class includes characters that it will never find
(|, *, etc.) as they're illegal in a windows path anyway.
FYI: XY doesn't support $0.
-Puts the whole thing in quotes as some paths require it.
-Changed default depth to "2", as its primary use (for me at least) is
to bypass intervening garbage folders of varying names.
-Disposes of painful regex in v1.1 which needed ")" escaped anyway.
Note: It's useful to reuse the filled-in Find Box as needed,
such as changing attrib: folder or tweaking the regex to be more exclusive.
Started and finished by Dustydog.
The main search template script is by highend.
Most of the good advice is from highend.
*/