Larry Smith said:
It shouldn't be necessary to muck around with this stuff if
"GetShortPathName()" was doing its job correctly.
But it is. It's telling you the correct name when it's known, and refusing
to do so when it can't be calculated.
That's the point. Moreover, you're not dealing with a simple parse.
Sure you are. Components in the path are separated by either "/" or "\",
and the legal 8.3 characters are well-defined. Remove everything after the
last component that needs to be translated. If you don't get an answer from
that, it's because the answer isn't known.
In other words, if you want to translate C:\Program Files\a\b\c\d\e, ask for
C:\Program Files and tack a\b\c\d\e onto the answer. If you don't get an
answer, C:\Program Files doesn't exist, and you can't really be sure what
its 8.3 name will be when it's created.
You need to deal with paths that may physically exist up to the nth
subfolder only (requiring an iterative check of some sort),
The OS call does that check for you
paths that aren't fully-qualified, etc.
I don't see why that matters.
The tilde character is also not guaranteed on all systems and MSFT states
that you shouldn't rely on it as I recall (though it's been a long time).
If I were MSFT, I'd say the same thing. No point making guarantees you
might have to break.
Guessing exactly how the shortname is created under all circumstances is
also unreliable. I may be naive to assume MSFT's algorithm always follows
the accepted convention of "AAAAAA~#". It might not and/or there may be
issues you and I aren't aware of (noting that file names have elaborate
rules that may impact the outcome). It's simply safer and easier to rely
on the OS.
Agreed. I'm not proposing you do otherwise. The only assumptions you need
to make are about the use of ":", "/", and "\", and that should be safe.