Ampersand problem...

May 24, 2010
855
0
Northlake, Il
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?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
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?

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.
 
May 24, 2010
855
0
Northlake, Il
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.

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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
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.

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 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.
I would never accuse you of it. (Just be glad they're not putting percent signs in their filenames ... yet.)
 
May 20, 2008
11,401
99
Syracuse, NY, USA
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!
 
May 20, 2008
11,401
99
Syracuse, NY, USA
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).
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
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).

That should work even in 4DOS. But you can get much more obscure in Take Command; how about SETDOS /C0x0569 or SETDOS /C0x13FE ?
 
May 24, 2010
855
0
Northlake, Il
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.)

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!!!)
 
May 24, 2010
855
0
Northlake, Il
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).

Thanks for your suggestions!!! These thing are good to know!
 
May 20, 2008
11,401
99
Syracuse, NY, USA
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
 
Similar threads
Thread starter Title Forum Replies Date
P Rename files with ampersand in the name Support 1
R caret or ampersand Support 6
Dick Johnson Weird Color Problem Support 8
fishman@panix.com Problem with 27.15 Support 2
M Problem with VSDevCmd.bat in VS 16.7.3 Support 0
R Problem with @INT[ value] in V26 Support 9
M Selecting test "off by one" problem in Take command Support 4
Alpengreis UTF-8 problem in TCC related to Python Support 7
K_Meinhard Small problem in german IDE 26 Support 3
B Problem with color in nested shells Support 1
Joe Caverly Problem creating and switching to a DESKTOP Support 9
vefatica Another popup problem Support 10
Alpengreis ffind dialog (/W) problem Support 4
Alpengreis [TCMD v25.00.24] Small space problem with the DE translation in Prefs-GUI Support 1
Alpengreis [TCMD v25.00.24] Problem with copy and paste and the # char via mouse in TCC Support 6
A Problem with functions @int @decimal and identifying Powershell as a shell. Support 12
B IF command problem in tcexit.btm Support 9
fishman@panix.com Problem at Startup of TCC Support 3
P Problem with SFTP copies Support 7
P Problem with FTP copies Support 10
Jay Sage Problem with Context Menu Copy+Paste+Run Key Assignment Support 7
R Problem with %_do_loop in nested do loops Support 2
fishman@panix.com New Problem with later Windows 10 Support 10
Peter Murschall A little problem with LEAVE and COMMENT Support 5
M Handling of %~I problem Support 4
WinLanEm FOR problem Support 18
K Problem With SCRPUT /u Support 3
vefatica What's TCMD's problem with ^e[0m? Support 13
Peter Murschall IDE: RTL with non-English resources-problem is back in Build 28 Support 4
rps Regex problem: \xnn not recognized as a hex character Support 0
rps PRIORITY LOW problem Support 1
old coot Regex problem: \xnn not recognized as a hex character Support 12
vefatica Another problem with build 22. Support 3
Alpengreis [23.x] Download-Problem Support 2
S Problem with " Support 3
C Problem navigating forums Support 11
x13 Problem listing repository files using DIR http(s)://... Support 8
Joe Caverly Problem with TEE in v22 Support 2
Alpengreis Problem with thousands delimiter and colors Support 17
M Take Command 18, migrating to another machine, license problem. Support 1
B TCC 21.01.50 Problem with ALIAS /r and SET /r Support 2
rps How to? @search problem Support 2
WinLanEm @SELECT problem Support 6
T Fixed Problem with use of Batch parameters in the IDE Support 1
Oz Solomon Problem with "list" Support 14
S V21.24 theme problem Support 2
Alpengreis Problem if command prompt is not legacy Support 4
Alpengreis TCMD.INI: The "super hidden" problem ... Support 6
G Odd problem - screen brightness Support 4
rps V20 In-process pipes problem Support 8

Similar threads