- NSIS Discussion
- Compare Paths?
Archive: Compare Paths?
AaronLS
9th March 2008 12:13 UTC
Compare Paths?
Any suggestions on the best way to determine that any two of these strings are equivalent paths? I could just loop one char at a time removing redundant and trailing \'s if no one knows of anything better. I'm also afraid there are other nuances that I might not have anticipated. Like, I just thought while typing, what if shortened 8.3 compatible file names were in the path. So maybe some method that relies on the OS a little more would be more "aware" of those nuances.
"C:\\Program Files\\blah blah"
"C:\Program Files\blah blah\"
"C:\\Program Files\\blah blah\"
"C:\Program Files\blah blah"
Anders
9th March 2008 12:27 UTC
GetFullPathName might be able to help out
Animaether
10th March 2008 14:59 UTC
you could cheat and write a temporary file to the path. If that file is present in the second path, then the two paths are the same.
On the up side, that bypasses any manual handling of short vs long filenames, symbolic paths that may be the same, double backslashes, 'up' modifiers ( \..\ ), etc.
On the down side, it's not a clean method and if you can't write to the path it won't work either.
Daniel James
10th March 2008 18:11 UTC
Originally posted by Anders
GetFullPathName might be able to help out
That's a good idea ... depending on how GetFullPathName is actually implemented in NSIS.
If it's just a wrapper around the GetFullPathNameA function in kernel32 (as I should hope) then it should do the job.
Just call GetFullPathName on each of the two strings in question and then compare them with StrCmp, and they should compare as equal if the strings represent the same path.
Not quite what I need ... I want to compare a string to see whether it contains the CLSID_PROGRAM_FILES directory
or any subdirectory thereof, but I think this and some calls to GetParent will do it ;)
Afrow UK
10th March 2008 18:59 UTC
For that Daniel, can you not get the length of $PROGRAMFILES, copy that same number of characters from your path to be compared and then compare the result with $PROGRAMFILES?
Stu
Animaether
10th March 2008 20:49 UTC
(for the more generic case, there's also...)
http://nsis.sourceforge.net/Repair_path_names_with_../
Daniel James
12th March 2008 13:49 UTC
Originally posted by Afrow UK
For that Daniel, can you not get the length of $PROGRAMFILES, copy that same number of characters from your path to be compared and then compare the result with $PROGRAMFILES?
Yes. Much neater. Thanks.
Daniel James
12th March 2008 13:52 UTC
Originally posted by Animaether
(for the more generic case, there's also...)
http://nsis.sourceforge.net/Repair_path_names_with_../
My goodness that's a hairy piece of scripting! I think I'd much rather write a plugin to do that.
Quite right, though, that you need to do something of the sort if the path is relative or leads to to a file or folder that doesn't (yet) exist.
Afrow UK
13th March 2008 11:30 UTC
If the path is relative, GetFullPathName before hand should sort that out I think.
Stu
Daniel James
13th March 2008 17:56 UTC
Originally posted by Afrow UK
If the path is relative, GetFullPathName before hand should sort that out I think.
Yes, of course ...
Sorry, there was a typo/thinko back there. I meant to write:
...you need to do something of the sort if the path is relative
and leads to to a file or folder that doesn't (yet) exist.
GetFullPathname doesn't work on non-existent files (unfortunately).
Afrow UK
13th March 2008 18:20 UTC
Right so how about making that folder with the relative path, use GetFullPathName then perhaps remove it (if necessary).
Stu
AaronLS
13th March 2008 20:56 UTC
Thanks everyone, very helpful.