Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: MS-DOS command-line and '&' character  (Read 9116 times)

0 Members and 1 Guest are viewing this topic.

James2000

  • Guest
MS-DOS command-line and '&' character
« on: April 25, 2007, 08:40:53 AM »
Hello,

Is there any way to stop MS-DOS command-line from treating '&' as a special character ?

In my PATH environment variable, I have a directory included named  "C:\R&S".

In a batch file, I have a line  "set PATH=%PATH%;C:\other".

This is failing with the following error message:-

     'S' is not recognized as an internal or external command, operable program or batch file.

The reason why it is failing is because MS-DOS is treating the '&' in %PATH% as a special character.


THANK YOU FOR ANY HELP,
James

contrex

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #1 on: April 25, 2007, 10:49:15 AM »
I thought you had to set the path variable with the PATH command eg:-

PATH c:\mypath;c:\xxx;c:\yyy;c:\zzz

Never mind. If you enclose the path in quotes.

set PATH="%PATH%;C:\other"

does that work?

You do not say what version command interpreter you are using

eg MS-DOS 6.22 command.com, Windows 95.98/ME command.com, NT or XP cmd.exe...?

James2000

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #2 on: April 26, 2007, 06:18:37 AM »
Hi contrex,

It is Microsoft Windows XP [Version 5.1.2600] and cmd.exe.

On Windows XP, environment variables can be set under My Computer Advanced Properties.  Also, when you install software on XP, the installation process can automatically update the PATH.  Hence why I have a directory called "R&S" in my PATH.

The third way to update the PATH is from the DOS command-line (or a DOS batch file).  I have tried the following:-

      PATH  "%PATH%;c:\other"
      PATH="%PATH%;c:\other"
      set  PATH="%PATH%;c:\other"

None of these work on XP cmd.exe because the " characters cause the whole string to be treated as one long directory name!

I have also tried doing something sneaky, for example:-

      PATH  ";%PATH%;c:\other;"
      PATH  ""%PATH%;c:\other""
      PATH  "";%PATH%;c:\other;""

First of these causes whole string to be treated as one long directory name.  Second and third both result in the original error because DOS is treating the '&' in %PATH% as a special character.

In a way I'm actually GLAD that no-one has solved this problem, because it means I've not missed anything obvious myself!

Thanks anyway,
James

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: MS-DOS command-line and '&' character
« Reply #3 on: April 26, 2007, 06:55:51 AM »
Using the caret should override the function of a special character and treat it as a literal value:

md c:\R^&S

path %path%;c:\R^&S

 8)

As if things aren't cryptic enough
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

contrex

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #4 on: April 26, 2007, 06:57:24 AM »
I did it! Folder names with certain characters need to be in quotes in the PATH variable, if you ever want to add more folder names from the command line. & is one such character. You need to edit the PATH variable so that the folder name C:\R&S is enclosed in quotes. See below.

Code: [Select]
C:\>path
PATH=C:\Program Files\Windows Resource Kits\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\batch;C:\Program Files
\ATI Technologies\ATI Control Panel;C:\cygwin\bin;C:\Program Files\Support Tools\;C:\PROGRA~1\NetMailBot;C:\Program Files\UltraEdit;
C:\Program Files\ATI Technologies\ATI.ACE\;C:\PROGRA~1\NcFTP;C:\Program Files\QuickTime\QTSystem\;C:\WINDOWS\System32\spool\DRIVERS\
W32X86\3;C:\Program Files\MKVtoolnix;C:\Program Files\UltraEdit;C:\Program Files\JPSoft\4NT7;c:\utils;"C:\R&S"

C:\>path %path%;C:\basic

C:\>path
PATH=C:\Program Files\Windows Resource Kits\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\batch;C:\Program Files
\ATI Technologies\ATI Control Panel;C:\cygwin\bin;C:\Program Files\Support Tools\;C:\PROGRA~1\NetMailBot;C:\Program Files\UltraEdit;
C:\Program Files\ATI Technologies\ATI.ACE\;C:\PROGRA~1\NcFTP;C:\Program Files\QuickTime\QTSystem\;C:\WINDOWS\System32\spool\DRIVERS\
W32X86\3;C:\Program Files\MKVtoolnix;C:\Program Files\UltraEdit;C:\Program Files\JPSoft\4NT7;c:\utils;"C:\R&S";C:\basic

C:\>

A batch file placed in the C:\R&S folder runs when called from outside the folder, so it is properly on the path. So is c:\basic which was added after it.



« Last Edit: April 26, 2007, 07:13:52 AM by contrex »

contrex

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #5 on: April 26, 2007, 07:02:41 AM »
Using the caret should override the function of a special character and treat it as a literal value:

md c:\R^&S

path %path%;c:\R^&S

 8)

As if things aren't cryptic enough

That's what I first thought of, but he already has managed to get the R&S folder in his PATH, somehow.

Escaping the & character with a caret would enable him to add it to the PATH if it were not there already, as would enclosing that folder name in quotes.

His problem arises because, when he tries to add a folder to the PATH from the command line, the command interpreter expands the PATH variable and then borks when it comes to the & character and so will not now add any more folders, at all,  to the PATH, although it seems to process it correctly for execution purposes.

Why not just rename the d**n thing R-AND-S? (Not serious)

Anyway, I think I have fixed it. See my other post above.

contrex

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #6 on: April 26, 2007, 07:30:38 AM »
It just occurs to me that putting a folder name with an & character in it, at each end of your PATH variable, would be a way of preventing a casual user without admin privileges from extending the PATH variable...

like so

C:\Some text&sorry you can't add to the PATH variable;C:\WINDOWS\System32; [...] c:\utils;c:\Some text&sorry you can't add to the PATH variable

because you can do PATH [new folder];%PATH% as well as PATH %path%;[new folder]. I just logged in to my Guest account and did it from there, so no special privileges seem to be needed...




James2000

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #7 on: April 26, 2007, 07:56:18 AM »
Thanks contrex,

Problem solved.  Updated PATH (using My Computer Advanced Properties) and it works without needing to change the batch file.  Just put quotes around "C:\R&S" ...

Thanks again,
James

nd

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #8 on: May 01, 2007, 08:20:39 AM »
  This note is CRITICALLY important to me.  I spent a very long time searching for this answer.  The caret can be used to treat parentheses as literal within an echo statement within a for loop, which has been kicking my *censored*. 

  I recommend that this comment about the caret be moved into the "MSDOS command prompt" section, or somewhere else prominent on http://www.computerhope.com/msdos.htm  Who might be in charge of that?




Using the caret should override the function of a special character and treat it as a literal value:

md c:\R^&S

path %path%;c:\R^&S

 8)

As if things aren't cryptic enough

James2000

  • Guest
Re: MS-DOS command-line and '&' character
« Reply #9 on: May 01, 2007, 08:35:16 AM »
Computer Hope Admin is in charge.   See http://www.computerhope.com/forum/index.php?action=profile;u=1