Simplifying a File reference
I have a file reference containing multiple occurrences of .., like this File @ /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace How do I simplify this to show the simpler path without the .. ? There should be a method on FileReference, no? Something like #normalize ? Or maybe FileReference should do this automatically when it is created? Andrew
On 1 December 2017 at 17:32, Prof. Andrew P. Black <black@cs.pdx.edu> wrote:
I have a file reference containing multiple occurrences of .., like this
File @ /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace
How do I simplify this to show the simpler path without the .. ? There should be a method on FileReference, no? Something like #normalize ? Or maybe FileReference should do this automatically when it is created?
The answer will depend on which version of Pharo you are using, but for Pharo7: '/Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace' asFileReference canonicalize "File @ /Users/black/Development/mg/gracelang/albums_junk.grace" The reason the '..' segments are left in is because while the result is probably what you are looking for, if the original reference contains symbolic links you may end up with a different result to what the file system will return. Cheers, Alistair
On 2 December 2017 at 00:39, Alistair Grant <akgrant0710@gmail.com> wrote:
On 1 December 2017 at 17:32, Prof. Andrew P. Black <black@cs.pdx.edu> wrote:
I have a file reference containing multiple occurrences of .., like this
File @ /Users/black/graceProgs/lists/../../Development/mg/ gracelang/albums_junk.grace
How do I simplify this to show the simpler path without the .. ? There should be a method on FileReference, no? Something like #normalize ? Or maybe FileReference should do this automatically when it is created?
The answer will depend on which version of Pharo you are using, but for Pharo7:
'/Users/black/graceProgs/lists/../../Development/mg/ gracelang/albums_junk.grace' asFileReference canonicalize "File @ /Users/black/Development/mg/gracelang/albums_junk.grace"
While 'canonicalize' obviously fits, it seems a bit exotic and a tongue twist at five syllables. I wonder if 'flatten' might also be suitable. Is there some concept this wouldn't cover? (just a random first impression. happy to be shot down on it) cheers -ben
Am 01.12.17 um 17:32 schrieb Prof. Andrew P. Black:
I have a file reference containing multiple occurrences of .., like this
File @ /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace
How do I simplify this to show the simpler path without the .. ? There should be a method on FileReference, no? Something like #normalize ? Or maybe FileReference should do this automatically when it is created?
On Pharo 6 use "asAbsolute" on a FileReference, like this: '/Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace' asFileReference asAbsolute "File @ /Users/black/Development/mg/gracelang/albums_junk.grace" Cheers, Andreas -- Andreas Brodbeck www.mindclue.ch
participants (4)
-
Alistair Grant -
Andreas Brodbeck -
Ben Coman -
Prof. Andrew P. Black