Reputation: 16035
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.
// 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!!!!!
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.
Upvotes: 1
Views: 120
Reputation: 6689
public static string System.IO.Path.GetFullPath(string path)
is what you need.
Upvotes: 3
Reputation: 238246
Try
System.IO.Path.GetFullPath(@"G:\path\foo\..\..\bar\baz.txt");
Upvotes: 5