Escaping escape characters in JScript

Discussion in 'ASP General' started by Jon Maz, Sep 14, 2004.

  1. Jon Maz

    Jon Maz Guest

    Hi,

    I have been getting hopelessly confused with escaping escape characters in
    JScript! All I want to do is write a simple funtion:

    function DoubleUpBackSlash(inputString)
    {
    ???????
    }

    which will do the following:

    <%
    var inputString = "D:\Internet\test2.txt"
    Response.Write(DoubleUpBackSlash(inputString));
    %>

    ....printing out the following on the screen:
    D:\\Internet\\test2.txt

    Can anyone fill in the blanks in the function for me?

    TIA,

    JON
     
    Jon Maz, Sep 14, 2004
    #1
    1. Advertising

  2. By the time you're passing the value to your function, you have to already
    have the \s escaped. So, if you're hard-coding in the string value, which
    you're currently doing, hard-code it with the \s doubled up already.

    What are you trying to do with the value afterwards? Are you putting it in
    a client-side javascript function? If so, escaping will have to be handled
    again, but tell us what you're doing first before we worry about that.

    Ray at work

    "Jon Maz" <> wrote in message
    news:...
    > Hi,
    >
    > I have been getting hopelessly confused with escaping escape characters in
    > JScript! All I want to do is write a simple funtion:
    >
    > function DoubleUpBackSlash(inputString)
    > {
    > ???????
    > }
    >
    > which will do the following:
    >
    > <%
    > var inputString = "D:\Internet\test2.txt"
    > Response.Write(DoubleUpBackSlash(inputString));
    > %>
    >
    > ...printing out the following on the screen:
    > D:\\Internet\\test2.txt
    >
    > Can anyone fill in the blanks in the function for me?
    >
    > TIA,
    >
    > JON
    >
    >
    >
     
    Ray Costanzo [MVP], Sep 14, 2004
    #2
    1. Advertising

  3. Jon Maz

    Jon Maz Guest

    Hi Ray,

    This is what I'm after (see my comments in the code):

    <%
    var fso = Server.CreateObject("Scripting.FileSystemObject");

    //doesn't work
    var wfile = fso.CreateTextFile("D:\Internet\test2.txt", true);

    //works
    var wfile = fso.CreateTextFile(" D:\\Internet\\test2.txt", true);

    //want this to work!
    var wfile =
    fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"), true);

    wfile.WriteLine("This is a test.");
    wfile.Close();
    fso = null;
    %>

    Thanks,

    JON
     
    Jon Maz, Sep 14, 2004
    #3
  4. The //want this to work won't work in jscript! Is there any particular
    reason that you don't want want to use the built-in escape functionality
    that is required? You can't just elect to not use it. If you share the
    reason for your desire, you may be surprised by a creative solution!

    Ray at work



    "Jon Maz" <> wrote in message
    news:...
    > Hi Ray,
    >
    > This is what I'm after (see my comments in the code):
    >
    > <%
    > var fso = Server.CreateObject("Scripting.FileSystemObject");
    >
    > //doesn't work
    > var wfile = fso.CreateTextFile("D:\Internet\test2.txt", true);
    >
    > //works
    > var wfile = fso.CreateTextFile(" D:\\Internet\\test2.txt", true);
    >
    > //want this to work!
    > var wfile =
    > fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"), true);
    >
    > wfile.WriteLine("This is a test.");
    > wfile.Close();
    > fso = null;
    > %>
    >
    > Thanks,
    >
    > JON
    >
    >
    >
    >
    >
     
    Ray Costanzo [MVP], Sep 14, 2004
    #4
  5. Jon Maz wrote:
    > Hi Ray,
    >
    > This is what I'm after (see my comments in the code):
    >
    > <%
    > var fso = Server.CreateObject("Scripting.FileSystemObject");
    >
    > //doesn't work
    > var wfile = fso.CreateTextFile("D:\Internet\test2.txt", true);
    >
    > //works
    > var wfile = fso.CreateTextFile(" D:\\Internet\\test2.txt", true);
    >
    > //want this to work!
    > var wfile =
    > fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"),
    > true);
    >
    > wfile.WriteLine("This is a test.");
    > wfile.Close();
    > fso = null;
    > %>
    >
    > Thanks,
    >
    > JON


    If you are supplying a string literal (as above) you would have had to
    already have typed in the \\ in order to get the string properly
    interpreted. It's the same as if you typed in a string literal containing a
    quote in vbscript:

    s = "he said "something""

    This would not be correctly interpreted either until you doubled the quotes.
    s = "he said ""something"""

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Sep 14, 2004
    #5
  6. "Jon Maz" <> wrote in message
    news:...
    > Hi Ray,
    >
    > This is what I'm after (see my comments in the code):
    >
    > <%
    > var fso = Server.CreateObject("Scripting.FileSystemObject");
    >
    > //doesn't work
    > var wfile = fso.CreateTextFile("D:\Internet\test2.txt", true);
    >
    > //works
    > var wfile = fso.CreateTextFile(" D:\\Internet\\test2.txt", true);
    >
    > //want this to work!
    > var wfile =
    > fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"), true);
    >
    > wfile.WriteLine("This is a test.");
    > wfile.Close();
    > fso = null;
    > %>
    >
    > Thanks,
    >
    > JON


    Unfortunately, it can't be done. It's the equivalent of trying to create the
    DoubleUpQuote function in VBScript. By the time you construct the string to
    pass to the function it's already too late. Sort of a weird catch-22
    situation.
     
    Chris Hohmann, Sep 14, 2004
    #6
  7. Jon Maz

    Jon Maz Guest

    Hi Ray,

    Reason's simple - it was a pain in the you-know-where copying file paths out
    of a browser address window and manually doubling up the back slashes, so I
    just thought I'd try to automate the process instead...

    Cheers,

    J


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
     
    Jon Maz, Sep 14, 2004
    #7
  8. Jon Maz

    Jon Maz Guest

    Hi Bob & Chris,

    Thanks, that explains what was screwy in my logic!

    Cheers,

    JON


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
     
    Jon Maz, Sep 14, 2004
    #8
  9. Wouldn't the browser address use a /?

    If your values are already in variables, you don't have to escape the \. If
    you have a variable with a value of "D:\Path" you don't have to escape that.

    Again, if you show us what you're doing, you may find an answer.

    Ray at work


    "Jon Maz" <> wrote in message
    news:e5H%...
    > Hi Ray,
    >
    > Reason's simple - it was a pain in the you-know-where copying file paths
    > out
    > of a browser address window and manually doubling up the back slashes, so
    > I
    > just thought I'd try to automate the process instead...
    >
    > Cheers,
    >
    > J
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
    >
    >
     
    Ray Costanzo [MVP], Sep 14, 2004
    #9
  10. Jon Maz

    Jon Maz Guest

    Sorry Ray, I meant the Windows Explorer address bar, not browser address
    bar.

    J




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
     
    Jon Maz, Sep 14, 2004
    #10
  11. How are you pulling in a Windows Explorer address bar from ASP code?

    Ray at work

    "Jon Maz" <> wrote in message
    news:u%...
    > Sorry Ray, I meant the Windows Explorer address bar, not browser address
    > bar.
    >
    > J
    >
    >
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
    >
    >
     
    Ray Costanzo [MVP], Sep 14, 2004
    #11
  12. Jon Maz wrote:
    > //want this to work!
    > var wfile =
    > fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"), true);


    The backslash indicates the following character to be printed as is. In
    this case you just have "D:Internettest2.txt". However, you may use
    another web typical syntax with slashes:

    fso.CreateTextFile(makeLocalePath("D:/Internet/test2.txt"), true);

    function makeLocalePath(path) {
    return path.replace(/\//g,"\\");
    }

    Daniel
     
    Daniel Kirsch, Sep 15, 2004
    #12
  13. Jon Maz

    Jon Maz Guest

    I'm not doing it from code, I'm doing it with the help of a mouse....

    I think Bob & Chris, in another branch of this thread, have explained that
    what I was trying to do is actually impossible...

    J
     
    Jon Maz, Sep 15, 2004
    #13
  14. Jon Maz

    Jon Maz Guest

    Thanks, Daniel.

    JON
     
    Jon Maz, Sep 15, 2004
    #14
  15. Daniel Kirsch <> writes:

    > Jon Maz wrote:


    >> fso.CreateTextFile(DoubleUpBackSlash("D:\Internet\test2.txt"), true);

    >
    > The backslash indicates the following character to be printed as
    > is.


    Actually, a backslash followed by another character together form an
    "escape sequence", which stands for a single character. Some escape
    sequences for non-printing characters are
    \n - newline
    \r - return
    \t - tab
    (there are more, but not many). For characters which are not defined
    as part of a special escape sequence, the escape sequence just becomes
    the second character itself.

    > In this case you just have "D:Internettest2.txt".


    So, in this case, "\t" becomes a tab character, and you have:
    "D:Internet est2.txt"
    ^ This is a tab character, ASCII 8.


    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 17, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    4
    Views:
    1,282
    Jon Maz
    Sep 9, 2004
  2. Maxwell2006
    Replies:
    5
    Views:
    2,414
    Steven Cheng[MSFT]
    Mar 7, 2006
  3. John Bentley
    Replies:
    14
    Views:
    366
    Jim Ley
    Feb 5, 2004
  4. Replies:
    8
    Views:
    105
    David Squire
    Jun 27, 2006
  5. VK
    Replies:
    4
    Views:
    363
    Thomas 'PointedEars' Lahn
    Jan 12, 2006
Loading...

Share This Page