MEL How-To #34 | ||
| ||
How do I enforce backslashes for Windows filepaths (or forward slashes for Maya)?Method #1: Use " Note: These are only relevant under Windows. The " string $path = "C:/path/file.mel"; // Result: C:/path/file.mel // toNativePath( $path ); // Result: C:\path\file.mel // string $path = "C:\\path/file.mel"; // Result: C:\path/file.mel // toNativePath( $path ); // Result: C:\path\file.mel // The " string $path = "C:\\path\\file.mel"; // Result: C:\path\file.mel // fromNativePath( $path ); // Result: C:/path/file.mel // string $path = "C:\\path/file.mel"; // Result: C:\path/file.mel // fromNativePath( $path ); // Result: C:/path/file.mel // Method #2: Use the " There are a few things to be aware of when using the " Firstly, "
string $path = "C:/path/file.mel";
while ( $path != ( $path = `substitute "/" $path "\\"` ) );
// Result: C:\path\file.mel //
Although converting from forward slashes to backslashes is straightforward,
replacing a backslash with a forward slash requires a little trickery. Since the
substitute command accepts a regular expression for its first argument you must
double-up all backslashes within the match string for
the 'substitute' command. In other words, if you are substituting the
"
// Maya doesn't like it this way.
while ( $path != ( $path = `substitute "\\" $path "/"` ) );
This attempt results in an error: // Error: line 1: Invalid escape sequence: Missing character after last backslash "\" in string "\". // (Or, if you're running a version prior to v2.5, Maya will crash and burn!) // Doubling each backslash in the first argument is required. string $path = "C:\\path\\file.mel"; while ( $path != ( $path = `substitute "\\\\" $path "/"` ) ); // Result: C:/path/file.mel // Furthermore, if you are replacing a backslash with two backslashes (to ESCape the string for a callback argument, for instance) you must first replace the character with a placeholder, and then replace all placeholders with the doubled character. Otherwise the while() loop would be infinite, finding more and more backslashes to replace. // First replace all backslashes with asterisk. while ( $path != ( $path = `substitute "\\\\" $path "*"` ) ); // Result: C:*path*file.mel // // Now replace all asterisk with double-backslash. // Note: Asterisk must be ESCed here. while ( $path != ( $path = `substitute "\\*" $path "\\\\"` ) ); // Result: C:\\path\\file.mel // Note that the " Method #3: Use the ‘ This method can prove to be more flexible because you have control over how the path is rebuilt; e.g. whether it has a trailing slash or whether it is a relative or absolute path. Here's the utility procedure I use to convert a path to Windows slashes. proc string slash( string $path ) // Input: Directory path with '/' or '\' slash delimiters. // Output: Directory path with '\' slash delimiters. { string $slash = ""; string $tokens[]; int $numTokens = `tokenize $path "\\/" $tokens`; int $i; $slash = $tokens[0]; for ( $i = 1; $i < $numTokens; $i++ ) { $slash += ( "\\" + $tokens[$i] ); } return $slash; } Example:
string $path = "C:/path/file.mel";
$path = slash($path);
// Result: C:\path\file.mel //
Other Examples Same as Converts any backslashes "\" in string to ESC'd backslash "\\" for use when the resultant filepath must be interpreted by an ‘ Related How-To's13 January 2002 | ||
Copyright ©2005 by Bryan Ewert, maya@ewertb.com Maya is a Registered Trademark of Alias |