How To Handle Odd Folder Names?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

How To Handle Odd Folder Names?

Post by Jeff Bellune »

I'm trying to write a script that returns the path component of one or more folders that are in a list of folders. I get the original list of folders using the folderreport command. In some locations, the path contains folders with odd folder names that include dots. How do you handle that in a script? The getpathcomponent command seems to assume that any dot in a folder name means that the folder is a file with an extension.

For example, F:\Me\Data\Test.Folder is a folder containing irrelevant files. Using getpathcomponent on it returns F:\Me\Data, which won't let me include or exclude the original folder from any list of folders.

And if Test.Folder contains subfolders and I need to include or exclude any of them, getpathcomponent will only return F:\Me\Data\Test.Folder and not any of the subfolders.

I thought maybe using folderreport instead of getpathcomponent could help with this issue, but I don't see how to make it work.

Thanks,
Jeff

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How To Handle Odd Folder Names?

Post by bdeshi »

Add a slash after the folders.
If all items passed to getpathcomponent are always folders, you can use this bit of code getpathcomponent($variableWithDirName.'\', path)
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

SammaySarkar wrote:Add a slash after the folders.
If all items passed to getpathcomponent are always folders, you can use this bit of code getpathcomponent($variableWithDirName.'\', path)
Thanks. I'll try it and report back.

If they're *not* always folders, would you recommend a foreach loop to concatenate a backslash to the name of each folder returned by folderreport? Or is there a faster/more efficient way to handle the mixed folders and files case?

Cheers,
Jeff

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How To Handle Odd Folder Names?

Post by highend »

So your folderreport uses "items"?

Why not split it up in dirs and files and use a regexmatch to get only the path components of the folders?
One of my scripts helped you out? Please donate via Paypal

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

highend wrote:So your folderreport uses "items"?

Why not split it up in dirs and files and use a regexmatch to get only the path components of the folders?
No. My folderreport uses "dirs", which is why I asked about falling back to the report list to add the backslashes. I hadn't considered the case where a search or query would look for both files and folders at the same time until Sammay asked if my getpathcomponent would always get passed only folders. So I asked, indirectly, about how to handle the case where getpathcomponent might be passed a mix of files and folders. I don't have a use for the mixed case yet, but it may come up in the future.

Cheers,
Jeff

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: How To Handle Odd Folder Names?

Post by TheQwerty »

One nice aspect of FolderReport are the types which support Report's templates so if you wanted to get a listing of files and folders and append a backslash to the folders you can use Report's {Dir Dir Value|File Value|Drive Value} template like so:

Code: Select all

::Text FolderReport('items:{FullName}{Dir \||}', 'return');

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

SammaySarkar wrote:Add a slash after the folders.
If all items passed to getpathcomponent are always folders, you can use this bit of code getpathcomponent($variableWithDirName.'\', path)
I concatenated the backslash as described and it makes no difference in the result. If the path contains one or more dots and zero or more legal non-word characters, the getpathcomponent treats "path" like "parent". I checked the script by stepping through it and checking the variables, and the backslash gets added as expected. It's just that adding the backslash doesn't change the result.

I think maybe I don't understand how getpathcomponent is supposed to work...

Cheers,
Jeff

EDIT: For this particular project, explicitly folderreporting on the few folders that contain these characters and assigning the result to a variable lets me use the variable values as the source of the paths that I need to retrieve or exclude. But I'd like to find out why getpathcomponent isn't working as expected for me.

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How To Handle Odd Folder Names?

Post by highend »

What I'm not getting:

When you create a folderreport with dirs only which path components do you want to get afterwards...

E.g. to only get the full path of all entries of a folderreport you could use:

Code: Select all

text regexmatches(folderreport("items:{fullname}{dir \||}", "r"), "^.*(?=\\)", "<crlf>");
If that's what you're looking for...
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How To Handle Odd Folder Names?

Post by bdeshi »

Jeff Bellune wrote:If the path contains one or more dots and zero or more legal non-word characters, the getpathcomponent treats "path" like "parent".
getpathcomponent always returns the parent path when 'path' is used.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

highend wrote:What I'm not getting:

When you create a folderreport with dirs only which path components do you want to get afterwards...
Sometimes I want the path to the folder itself, sometimes I want the path to its parent, and sometimes I want the path(s) to its subfolder(s).

So for the following folder structure:

Master Music And Movie Folder
-Artist Folders
--Album Folders
--Singles Folders
--Music Video Folders
-Compilations Folder
--Album Folders
-Movie Folders
--Many Different Subfolders

I need to be able to include or exclude folders based on search results, album names, track format, ID3 tag, etc. For example, I know that the Movie folder and Music Video folders won't contain artwork (unless I've made a mistake). For now, I get a report for the Music Video and Movie folders by explicitly naming the paths, instead of by inclusion or exclusion of search parameters, which would be more dynamic and let me catch those mistakes. More importantly, I can't use the script on any top-level folder that doesn't contain Music Video and Movie subfolders.

Another example: many Album folders have similar names, based on whether they contain the multichannel version of an album or a stereo version. I need to exclude the multichannel folders in some cases.

Does that help?

Cheers,
Jeff
Last edited by Jeff Bellune on 09 Sep 2014 18:57, edited 2 times in total.

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

SammaySarkar wrote:
Jeff Bellune wrote:If the path contains one or more dots and zero or more legal non-word characters, the getpathcomponent treats "path" like "parent".
getpathcomponent always returns the parent path when 'path' is used.
So it doesn't return the parent only when the path includes a file? What's the difference, then, between "path" and "parent"?

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How To Handle Odd Folder Names?

Post by highend »

Does that help?
Not completely but in the end... Without knowing every detail, filtering things can be done via formatlist().

E.g.:
Let's assume

Code: Select all

$folders = folderreport("items:{fullname}{dir \||}", "r");
would deliver
D:\Music\Queen\
D:\Music\Queen\Greates Hits I\
D:\Music\Queen\Greates Hits II\
D:\Music\Queen\The works\
D:\Music\Freddy Mercury\Innuendo\
Then I'd do the filtering via:
$folders = replace($folders, "<crlf>", "|");

$excludeList = "hits|the";
$excludePattern = "!*" . replace($excludeList, "|", "*|*") . "*";
$result = formatlist($folders, "f", , $excludePattern);
The only thing I'd need to do is: add / remove items to the $excludeList...

Which would lead to (| replaced back to `n):
D:\Music\Queen\
D:\Music\Freddy Mercury\Innuendo\
Ofc it's also possible to just use regexreplace with a pattern that matches the exclude list items...

Code: Select all

^.*?($excludeList).*?\r?\n
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How To Handle Odd Folder Names?

Post by bdeshi »

Jeff Bellune wrote:
SammaySarkar wrote:
Jeff Bellune wrote:If the path contains one or more dots and zero or more legal non-word characters, the getpathcomponent treats "path" like "parent".
getpathcomponent always returns the parent path when 'path' is used.
So it doesn't return the parent only when the path includes a file? What's the difference, then, between "path" and "parent"?
Okay, let me explain.
getpathcomppnent with 'path' returns the path where an item is located. If the item is a folder, it's parent is returned.
If the item is a file, it's folder is returned. And the folder where a file is located is it's parent.
In both case, that parent is returned.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

@highend:
Once I get past why the folderreport you set up returns what it does, I'm lost. You've gone beyond my ability to interpret your logic as to why you set up the filters and replace commands as you did. I don't doubt that they work, I just don't understand why or how they should. For example, the double replace commands seem like circular logic to me, even as I'm sure it won't turn out to be circular. I see scripts in a very linear manner, and you are branching in multiple directions at once. Your experience and skill obviously produces better and faster results. I just can't catch up to your way of thinking yet. And I do want to understand why these things work the way they do.

The same thing goes for your optimized script in the thread where we discussed how to search for folders that *don't* contain a particular file or files. I'm marginally functional with regexp syntax and use, but you've used techniques that are obviously advanced well beyond my understanding at this point. I've spent over an hour in my regexp reference documents trying to decode what you've used there, and I can't do it. Again, I'm talking about learning to crawl without catching my hands under my knees, and you're discussing energy conservation techniques when running a marathon. :)

The good part is that I've learned a lot so far from our discussions. Thank you! But I have so much further to go that I have to re-evaluate the amount of time I'm willing to invest. If I had to write code for a living, I'd starve. :biggrin:

"A man's got to know his limitations."
- "Dirty Harry" Callahan


Cheers,
Jeff

Jeff Bellune
Posts: 284
Joined: 13 Dec 2007 12:55

Re: How To Handle Odd Folder Names?

Post by Jeff Bellune »

SammaySarkar wrote:getpathcomppnent with 'path' returns the path where an item is located. If the item is a folder, it's parent is returned.
If the item is a file, it's folder is returned. And the folder where a file is located is it's parent.
In both case, that parent is returned.
Thanks. I sort of expected that "parent" would return a path to the parent just like "path" does, but upon carefully examining the examples in the script reference, it doesn't. I'm going to have to use another method to get or retrieve the current folder's full path info.

Cheers,
Jeff

Post Reply