DOS Batch File Sometime Interprets String Manipulation as Commands

jamesnoe
jamesnoe used Ask the Experts™
on
Hi there,

I have a DOS batch file which has a filename as a parameter.  The string comes in with quotes that I want to remove.  The input filename has been stored in the variable %tmp

To remove the start and end quote, I do:

           set tmp2=%tmp:~1,-1%

However, if the filename has an ampersand in it I get the error "... is not recognized as an internal or external command, operable program or batch file."

... would be the first word after the ampersand.  For example...

Input filename = "Hello this is the & symbol.doc"

The command set tmp2=%tmp:~1,-1% will say "symbol is not recognized as an internal or external command, operable program or batch file."


Any ideas on how I can process these files?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The & is the command separator character, which allows you to do things like this:

  cd \temp  &  dir *.txt

So if the filename contains an amp character you will need to leave the quotes around it.


Author

Commented:
Thanks, but I think I need to be able to do replacement within the string as part of the script...

How can I do that?
If you are going to use this to do file operations on the file (such as copy or move) then you can replace the & with a ?

  set tmp2="%tmp:&=?%"
  set tmp3=%xyz:~2,-2%
@echo %tmp3%

11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Commented:
I would do as suggested above, but if the filename comes in as %1 (i.e. from command line) you can get that with %~1 to remove the quotes.  You can use that with string replacement like this for instance:

C:\>stringtest "this & that & the other"
String is now this and that and the other

@echo off
set string="%~1"
set string=%string:&=and%
set string=%string:"=%
echo String is now %string%

Author

Commented:
Hi,

Thanks for the suggestion, but utlimately the & needs to be preserved.

Below is my batchfile:

   @echo off
    echo DEBUG 1 = %1
    set tmp="%~1"
   
    echo DEBUG 2 = %tmp%
    set toRemove=%tmp:&=THISISANAMPERSAND%
    set toRemove=%toRemove:~1,-1%
   
    echo DEBUG 3 = %toRemove%
    set toRemove=%toRemove:THISISANAMPERSAND=&%
   
    echo DEBUG 4 = %toRemove%


Below is the output:

C:\>checkIt.bat "hello & goodbye.txt"
DEBUG 1 = "hello & goodbye.txt"
DEBUG 2 = "hello & goodbye.txt"
DEBUG 3 = hello THISISANAMPERSAND goodbye.txt
'goodbye.txt' is not recognized as an internal or external command,
operable program or batch file.
DEBUG 4 = hello

Commented:
In a batch file you are going to need to keep those quotes to deal with the special characters.

Any particular reason for needing to remove them?

Steve

Commented:
In DOS, ampersand is special character to join multiple command lines.
If you want to use ampersand as a normal string, you can use ^ before ampersand (^&) to escape special mode.
E.g:
set tmp="Hello this is the & symbol.doc"
set tmp1=%tmp:&=^&%
set tmp2=%tmp1:~1,-1%

=> So tmp2 is    Hello this is the ^& symbol.doc
=> DOS deals with ^& as ampersand
Test your restores, not your backups...
Top Expert 2016
Commented:
==> jamesnoe

Try the following to get your sample script to work:

@echo off
echo DEBUG 1 = %1
set tmp="%~1"
echo DEBUG 2 = %tmp%
set toRemove=%tmp:&=AMP%
set toRemove=%toRemove:~1,-1%
echo DEBUG 3 = %toRemove%
set "toRemove=%toRemove:AMP=&%"
echo DEBUG 4 = %toRemove:&=^&%


This produces:

DEBUG 1 = "hello & goodbye.txt"
DEBUG 2 = "hello & goodbye.txt"
DEBUG 3 = hello AMP goodbye.txt
DEBUG 4 = hello & goodbye.txt


~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial