1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Ampersand problem...

Discussion in 'Support' started by mathewsdw, Aug 10, 2010.

  1. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    822
    Likes Received:
    0
    I often download files from an external source on the web, and these files often have an ampersand ("&") in their name (as an example, "Tom & Jerry.doc") Over time I have had a gradually increasing number of processes that I want to perform on these files, and in all cases I write a C++ program to do it because of the (apparent?) inability of TCC to handle ampersands in character strings in batch files (a problem that C++ of course, doesn't have). But the point is that it would often be much quicker, easier, and simpler to simply write a .bat file to do the processing that I want, but again, TCC can't handle ampersands in character strings in batch files. So the question: Is there a way around this?
     
    #1
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    2,712
    Likes Received:
    14
    Code:
    C:\>echo Hello, world! > "Tom & Jerry.doc"
    
    C:\>ren "Tom & Jerry.doc" "A different name.*"
    C:\Tom & Jerry.doc -> C:\A different name.doc
         1 file renamed
    
    C:\>
    
    Quote any filename that contains ampersands or other special characters. Quote any filename that might contain ampersands or other special characters.
     
    #2
  3. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    822
    Likes Received:
    0
    Charles,

    Here's the contents of a very simple .bat file that illustrates the problem:

    Echo %@Path["C & D & E & F & G\H & I & J & K.txt"]
    Echo %@FileName["C & D & E & F & G\H & I & J & K.txt"]

    In the real program these paths\filenames are read from an external file with @FileRead. And note that in this example, the path\filename is enclosed in double quotes.

    Here's the (complete!) results of executing the above .bat file:

    Echo C
    C
    D
    TCC: Z:\AmpersandProblem.bat [1] Unknown command "D"
    E
    TCC: Z:\AmpersandProblem.bat [1] Unknown command "E"
    F
    TCC: Z:\AmpersandProblem.bat [1] Unknown command "F"
    G\
    TCC: (Sys) Z:\AmpersandProblem.bat [1] The system cannot find the
    file specified.
    "G\"
    Echo H
    H
    I
    TCC: Z:\AmpersandProblem.bat [2] Unknown command "I"
    J
    TCC: Z:\AmpersandProblem.bat [2] Unknown command "J"
    K.txt
    TCC: Z:\AmpersandProblem.bat [2] Unknown command "K.txt"

    I will note that it was not I who put the ampersands in the file names (by now I know better than to do that) - that is the way they "arrived" from an external website, so there's no way (other than writing a C++/Visual Basic program) that I can conveniently get rid of the ampersands.

    - Dan
     
    #3
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    2,712
    Likes Received:
    14
    Whoa, nasty!

    The problems you're having are caused by the values returned from the functions to ECHO, not by the values going into the functions. The solution is the same: double-quote 'em.

    Code:
    Echo "%@Path["C & D & E & F & G\H & I & J & K.txt"]"
    Echo "%@FileName["C & D & E & F & G\H & I & J & K.txt"]"
    
    (Looking at the help file, I see that the help page for @FILENAME says that you should double-quote the returned name, but the examples given don't do that. The page for @PATH does show double-quotes around the return values in the examples, but the formatting is peculiar to the point of being confusing.)

    I would never accuse you of it. (Just be glad they're not putting percent signs in their filenames ... yet.)
     
    #4
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    5,467
    Likes Received:
    8
    On Tue, 10 Aug 2010 12:05:43 -0400, mathewsdw <>
    wrote:

    |Quote any filename that contains ampersands or other special characters. Quote any filename that might contain ampersands or other special characters.

    You can also simply turn off the special meaning of the ampersand with
    SETDOS /X-5. One can almost always do without compound commands. But
    SETDOS /X-5 will also turn off the special meaning of "|" (for pipes)
    and that may be undesirable.

    Alternatively, you could also temporarily set the compound command
    character to something not likely to be in the text you're working
    with, say, SETDOS /C~ or SETDOS /C!
     
    #5
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    5,467
    Likes Received:
    8
    On Tue, 10 Aug 2010 14:46:46 -0400, vefatica <>
    wrote:

    |Alternatively, you could also temporarily set the compound command
    |character to something not likely to be in the text you're working
    |with, say, SETDOS /C~ or SETDOS /C!

    ... or even to something pretty darned sure not to be in the text, say
    SETDOS /C%@char[132] (temporarily, of course).
     
    #6
  7. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    2,712
    Likes Received:
    14
    That should work even in 4DOS. But you can get much more obscure in Take Command; how about SETDOS /C0x0569 or SETDOS /C0x13FE ?
     
    #7
  8. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    822
    Likes Received:
    0
    Thank you, Charles! I would have never guessed - and since I'm visually impaired (that's why this large font) so that it's hard for me to read the help file and having no expectation that the solution would be there, I didn't look. (I also must admit that your solution doesn't quite make total sense to me, but it works so who cares!!!)
     
    #8
  9. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    822
    Likes Received:
    0
    Thanks for your suggestions!!! These thing are good to know!
     
    #9
  10. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    822
    Likes Received:
    0
    Frankly, Charles, that's a little too obscure!!!!! :)
     
    #10
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    5,467
    Likes Received:
    8
    On Tue, 10 Aug 2010 16:04:10 -0400, Charles Dye
    <> wrote:

    |... or even to something pretty darned sure not to be in the text, say
    |SETDOS /C%@char[132] (temporarily, of course).
    |---End Quote---
    |That should work even in 4DOS. But you can get much more obscure in Take Command; how about SETDOS /C0x0569 or SETDOS /C0x13FE ?

    Don't forget it's TCC (not TCMD) that's handling the command
    separator. There's no problem with that in a bare console.

    Code:
    v:\> SETDOS /C0x0569
    
    v:\> echo foo %@char[0x0569] echo bar
    foo
    bar
    And you can access it more readily.

    Code:
    v:\> echo foo %+ echo bar
    foo
    bar
     
    #11

Share This Page