Removing meaning of & in string manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davidjsmith1967
Posts: 2
Joined: 29 Mar 2007 06:53
Location: UK

Removing meaning of & in string manipulation

#1 Post by davidjsmith1967 » 29 Mar 2007 07:05

Hi,

I am attempting to remove the meaning of & within a string manipulation. Is there a special character to do this (if you're familar with 'sed' for Linux then a \ normally suffices).

My code is:

set RPW_CLIP=%1
set RPW_CLIP=%RPW_CLIP:~3,-1%

Where %1 is from the command line and is:

"F:Bills Music\Blur\13\03. Coffee & TV.mp3"

I run the code and end up with

Bills Music\Blur\13\03. Coffee

I assume this is because the & is interpreted by the set command.

Placing quotes around the eval statement does resolve this.

set RPW_CLIP="%RPW_CLIP:~3,-1%"

However, by doing this causes me other issues, as I know have quotes around the variable which require removal (which is original objective of running the trimming to get rid of them).

If I am able to remove the meaning of the & (and any other special operator characters - if you know them let me know) this would enable the set to be evaluated and leave the formatting in place.

Any resolutions greatly appreciated!

Many thanks,


DJ

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 29 Mar 2007 19:33

DJ,

Try:

Code: Select all

set "RPW_CLIP=%RPW_CLIP:~3,-1%"


DOS IT HELP? :wink:

davidjsmith1967
Posts: 2
Joined: 29 Mar 2007 06:53
Location: UK

#3 Post by davidjsmith1967 » 30 Mar 2007 11:55

Hi

I tried this, not much better. It seems even ask DOS to evaluate from ARG %1 is causing issues.

Try this code into test.bat with the command line

set RPW_CLIP="%1"
set "RPW_CLIP=%RPW_CLIP:~3,-1%"
echo RPW_CLIP is %RPW_CLIP%

test "F:Bills Music\Blur\13\03. Coffee & TV.mp3"

You'll see DOS errors after the & during the set RPW_CLIP="%1" command as it think's the line is over and it's another command.

I guess it's just a matter of syntax and it will work.

Many thanks,


DJ

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 30 Mar 2007 21:38

DJ,

The ampersand is a command delimiter. The batch command processor sees " TV.mp3" as a separate command. To avoid this you will need to use quotes whenever you resolve a variable with an ampersand in the value.

The following will work:
call
test "F:Bills Music\Blur\13\03. Coffee & TV.mp3"
using this code:

Code: Select all

set RPW_CLIP=%1
set "RPW_CLIP=%RPW_CLIP:~3,-1%"
echo."%RPW_CLIP%"


You can also escape the ampersand using ^ i.e. call
test "F:Bills Music\Blur\13\03. Coffee ^& TV.mp3"
using this code:

Code: Select all

set RPW_CLIP=%1
set "RPW_CLIP=%RPW_CLIP:~3,-1%"
echo.%RPW_CLIP%


Or you can inject the ^ within the batch i.e. call
test "F:Bills Music\Blur\13\03. Coffee & TV.mp3"
using this code:

Code: Select all

set RPW_CLIP=%1
set "RPW_CLIP=%RPW_CLIP:~3,-1%"
echo.%RPW_CLIP:&=^&%


Depends on what exactly you want to do.
:)

Post Reply