Solved

Problem with escape characters when splitting string into path/filename

Posted on 2008-06-18
12
421 Views
Last Modified: 2008-09-20
I am trying to write a small bit of javascript which splits the path from a variable containing path/filename.  The problem is that the HTML is embedded in an app that returns the path/filename using a special token, and when it is returned, the path/filename contains only a single \ between folders like:

c:\folder\filename.doc

instead of a escaped double \\ like:

c:\\folder\\filename.doc

I really just dabble in javascript, and need some assistance extracting just the path.  I am using the attached code. Note that '{DOC:FILENAME}' below will be changed to something like 'c:\folder\filename.doc' when viewed within the source application.
var fullPath = '{DOC:FILENAME}';
strPath = fullPath.substring(0,fullPath.lastIndexOf("\\"));

Open in new window

0
Comment
Question by:maiadog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 

Expert Comment

by:zmoidin
ID: 21818049
Is there a way you can encode the {DOC:FILENAME} before passing it to the javascript code?else you will have to write an encode function to change the fullPath string to proper format and then retrieve the substring you want.
0
 

Author Comment

by:maiadog
ID: 21818204
No, the {DOC:FILENAME} is just a token that gets replaced by the application when the HTML is rendered, and the format the application uses is c:\folder\filename.doc. That is why I assign it to a variable so I can work with it.

Can you elaborate on the encode function, or point me to some sample code?  Thanks.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21818613
Easiest is to use split() to separate the pieces, and join to put then back together again.  For example, something like:


var str="c:\\folder\\filename.doc'";
var temp = str.split( '\\' );
var name = temp.pop();
var path = temp.join( '\\' );

alert( 'Path: ' + path + '\nname: ' + name );
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:maiadog
ID: 21818780
Yes, but in your example, you show escaped backslashes , but as I indicated initially, the problem is that the software I am using returns a value that does not include escaped backslashes.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21818797
That is true.  However, the way to represent the single backslash character,
when one is entering a string in JavaScript, requires that the backslash be
escaped using a backslash.

If you were to use the length() function to identify the length of the following
string, what do you think the result would be?

var str="c:\\folder\\filename.doc"

Here, try it yourself.    Go to this page:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split

and enter this "script", and press the "... click me" button
<html>
<body>
 
<script type="text/javascript">
 
var str="c:\\folder\\filename.doc";
 
document.write( 'str: "' + str + '"<br>' );
document.write( 'len: ' + str.length );
 
</script>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:maiadog
ID: 21818855
I do understand that, but I am not in control of how that value is returned to me from the source application, therefore perhaps I need a way to modify the string.

The only way I have of getting at that value is to use the token {DOC:FILENAME} which is returns a value that contains a non-escaped path when the page is rendered.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21820728
This string does contain only 1 backslash  Not 2.

var str = 'c:\\filename.txt';

Try this:

  alert( 'str: "' + str + '"' )

and you will see a message like:

str: "c:\filename.txt"

So it does not contain an escaped path.  The escaped backslash
is only in the source code processed by the JavaScript interpreter.
0
 

Author Comment

by:maiadog
ID: 21821853
I understand that your example is escaped and it is actually only one backslash.  But the application I am using *does not* render the page with an escaped backslash in the path.  I am trying to figure out how I can make it escaped as in your example.
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 63 total points
ID: 21824006
Well, it doesn't matter how the string value is obtained.
You can use the instructions shown above (and repeated here)
// Somehow, the variable "str" gets a fully qualified filename value,
// something like 'C:\folder\filename.doc'
// Note: this is simply a representation of the characters in the
//       string, not the syntax required to assign the value in
//       JavaScript
//-------------------------------------------------------------------
var temp = str.split( '\\' );    // Break the string into an array
var name = temp.pop();           // Remove the last item (filename.doc)
var path = temp.join( '\\' );    // Build the path using remnants
 
alert( 'Path: "' + path + '"\nname: "' + name + '"' );

Open in new window

0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 62 total points
ID: 21826166
I am not sure I understand what you need and how this works but if you want 2 backslashes as the result then use ...

var.replace(/\\/, "\\\\")

Of course you may need to use it in a different way and I can help with that but basically it will look for one backslash (escaped in the first argument above) and replace it wil 2 (both escaped in the second argument, the string, above).

Let me know if you have a question or need help using this.

bol
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question