The question
Given a path like thus:
G:\path\foo\..\bar\baz.txt
Which we can immediately parse in our minds, and as the OS will parse as:
G:\path\bar\baz.txt
What's the fastest way to do that programmatically? In other words, does a library function already exist that will do this for me cleanly so that I don't botch it myself.
My first shot at an answer
// in pseudocode
string[] s = input.split('\')
string output
for ( int i = s.length ; i > 0 ; i-- ) {
if ( s[i] == ".." ) i--;
else output = s[i] + "\" + output
}
return output
But notice that this answer doesn't cover the case (unexpected, but potential) for
G:\path\foo\..\..\bar\baz.txt
which would of course be
G:\bar\baz.txt
TSQL functions will work as well!!!!!
Not important to the question but I felt like sharing ;)
I'm passing it from a returned value (off a webservice call) into a sproc (for XML shredding) and want to eliminate the foo\..\
as the value I pass in is used as the key for preventing duplicate processing later. If I insert G:\path\foo\..\bar\baz.txt
and another script inserts G:\path\bar\baz.txt
the file will be processed twice.