How To Handle Odd Folder Names?
-
Jeff Bellune
- Posts: 284
- Joined: 13 Dec 2007 12:55
How To Handle Odd Folder Names?
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
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?
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)
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 ]
[ this user is asleep ]
-
Jeff Bellune
- Posts: 284
- Joined: 13 Dec 2007 12:55
Re: How To Handle Odd Folder Names?
Thanks. I'll try it and report back.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)
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?
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?
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?
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.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?
Cheers,
Jeff
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: How To Handle Odd Folder Names?
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?
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.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 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?
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:
If that's what you're looking for...
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>");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?
getpathcomponent always returns the parent path when 'path' is used.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".
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
Jeff Bellune
- Posts: 284
- Joined: 13 Dec 2007 12:55
Re: How To Handle Odd Folder Names?
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).highend wrote:What I'm not getting:
When you create a folderreport with dirs only which path components do you want to get afterwards...
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?
So it doesn't return the parent only when the path includes a file? What's the difference, then, between "path" and "parent"?SammaySarkar wrote:getpathcomponent always returns the parent path when 'path' is used.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".
-
highend
- Posts: 14953
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: How To Handle Odd Folder Names?
Not completely but in the end... Without knowing every detail, filtering things can be done via formatlist().Does that help?
E.g.:
Let's assume
Code: Select all
$folders = folderreport("items:{fullname}{dir \||}", "r");Then I'd do the filtering via: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\
The only thing I'd need to do is: add / remove items to the $excludeList...$folders = replace($folders, "<crlf>", "|");
$excludeList = "hits|the";
$excludePattern = "!*" . replace($excludeList, "|", "*|*") . "*";
$result = formatlist($folders, "f", , $excludePattern);
Which would lead to (| replaced back to `n):
Ofc it's also possible to just use regexreplace with a pattern that matches the exclude list items...D:\Music\Queen\
D:\Music\Freddy Mercury\Innuendo\
Code: Select all
^.*?($excludeList).*?\r?\nOne 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?
Okay, let me explain.Jeff Bellune wrote:So it doesn't return the parent only when the path includes a file? What's the difference, then, between "path" and "parent"?SammaySarkar wrote:getpathcomponent always returns the parent path when 'path' is used.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".
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 ]
[ this user is asleep ]
-
Jeff Bellune
- Posts: 284
- Joined: 13 Dec 2007 12:55
Re: How To Handle Odd Folder Names?
@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.
"A man's got to know his limitations."
- "Dirty Harry" Callahan
Cheers,
Jeff
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.
"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?
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.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.
Cheers,
Jeff
XYplorer Beta Club