Ampersand - How do you manage them as text in a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Ampersand - How do you manage them as text in a variable

#1 Post by Jer » 04 Jun 2015 21:38

My batch application in progress pulls in music file names based on prompted input.
Some files include the ampersand symbol. I am trying to take those prompted choices
(from a larger numbered list) and write them into a m3u file, which can be played by Windows
Media player, using the Start command. This works but it bombs when a file name
has the & symbol.

This code illustrates the problem--it is my attempt to find a solution to the ampersand in text.
I have no clue if usebackq should be part of the solution.
Thanks.

Code: Select all

@echo off
setlocal enabledelayedexpansion

Set "fileName=Black & White Rag.mid"

Call:writeM3U "%fileName%"

Type mplay.m3u

endlocal & exit /b


:writeM3U
setlocal enabledelayedexpansion

For /F "usebackq" %%a In ('%~1') Do (
    Set "mVar=%%a"
)

endlocal & echo %mVar%>mplay.m3u
exit /b


The error is:
& was unexpected at this time.
C:\Temp\>For /F "usebackq" %a In ('Black & White Rag.mid') Do (

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Ampersand - How do you manage them as text in a variable

#2 Post by npocmaka_ » 05 Jun 2015 00:21

Code: Select all

@echo off
setlocal enabledelayedexpansion

set "mVar=asd&zxc"

echo !mVar!>mplay.m3u
endlocal
exit /b

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Ampersand - How do you manage them as text in a variable

#3 Post by foxidrive » 05 Jun 2015 02:58

Here's another option:

Code: Select all

@echo off
Set "fileName=Black & White Rag.mid"
for /f "delims=" %%a in ("%filename%") do >>m3u.blah echo(%%a

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Ampersand - How do you manage them as text in a variable

#4 Post by Jer » 05 Jun 2015 11:47

I see that both solutions handle writing text that may have an ampersand to a file.
Thank you both for giving me the tools to tame the &.
Jerry

Post Reply