Batch file: & filename with ampersand

The battleground for flame wars, off-topic derailments and spurious madness.
Post Reply
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Batch file: & filename with ampersand

Post by therube »

(Windows 7)

Batch file: & filename containing an ampersand (&)

I want to drag & drop (or SendTo) a set of files to a batch file.
All works fine - unless the filename (including path) includes an ampersand (&) (& I imagine a few other chars; ^ ( ), maybe more).

MUX.BAT:

Code: Select all

@ECHO OFF
setlocal enableDelayedExpansion

set "V=a&v"
set "W=%V%"
set "DRAGNDROP=%~1"
set V
set W
echo "%DRAGNDROP%"
echo %cmdcmdline%
pause
exit
I have a file named "A&V" (& "V&A") that I want to drop onto MUX.BAT.

No matter what I've tried, I can't seem to get DRAGNDROP correct?

Results:

Code: Select all

V=a&v
W=a&v
windir=E:\Windows

V == "a&v"
W == "a&v"

DRAGNDROP == "C:\BIN\a"
COMMANDLINE == E:\Windows\system32\cmd.exe /c ""C:\BIN\MUX.BAT"  C:\BIN\a&v  C:\BIN\v&a"
> DRAGNDROP == "C:\BIN\a"

I'm looking to get, expecting:

> DRAGNDROP == "C:\BIN\a&v"


I can supply "a&v" as a command-line argument to MUX.BAT, & that will set DRAGNDROP as wanted.
But I can't seem to get a drag & drop to do the same.

I can set a variable from within the batch file itself & that works [V=].
I can then reference that variable from within the batch file & that works [W=]
I can then ECHO V & W, & that works.

%cmdcmdline% shows the entire command line & that looks OK too.


Ideas?
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

So before here, I had looked at page upon page upon page of code & tips & ...

And before closing all those pages, figure I'll review what I had gone though, & as it turns out, the very last page I opened, I never did read:

"Droplet" batch script - filenames containing ampersands

And that explains my problem, almost to a T (& yes, my [full] batch file uses ffmpeg too).

Only thing(s) are that he says the dropped filename gets truncated, where what I show (my %cmdcmdline%) shows the complete filename. (And then I'll have to actually read through just how he's handling it, to see if I can make sense out of it. [Tomorrow or so.]
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

he says the dropped filename gets truncated
No actually didn't say that.
He says the dropped name does arrive correctly.
It's only when it gets assigned to a variable that it truncates.

That's is exactly my situation.
Like who in the world would have known this, expected this.
If not for this guy (& Google)...

So a simple work-around at this point, without having to bother with messing with my batch file, & until I can get the time for it, if I run into this [which is the whole reason I've been trying to figure this out], I can just add a space in the filename, somewhere & it will work.


Unforeakin' believable, MS!
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

I can just add a space in the filename, somewhere & it will work.
And it's going to be more then just that too.

Cause my filename already has <space> & (&) & ( & ) & - & , & @, just about everything but unicode chars ;-).
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

I don't get it?

DRAGNDROP is getting set correctly.
BUT cmdcmdline is not?

How can that be?

I changed the filename to: "a&v TESTING" - so it does have a space in it.
And DRAGNDROP does get set correctly.
BUT cmdcmdline does not!

I change my batch file to:

Code: Select all

echo %cmdcmdline%
pause
And cmdcmdline is broken?

Code: Select all

COMMANDLINE == E:\Windows\system32\cmd.exe /c  ""C:\BIN\MUX.BAT"  "C:\BIN\a
'v' is not recognized as an internal or external command,
operable program or batch file.
It breaks at the &, even with a <space> in there.

I change the batch file:

Code: Select all

echo !cmdcmdline!
pause
AND IT WORKS?
How does that make any sense?

(This is still with just some test cases & not yet with real data/program.)

Guess I'll have to figure out the difference between %x% & !x! for starters?
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

So I've long had this (original) CONCAT.BAT file:

Code: Select all

@ECHO OFF

ECHO  CONCATenate all the parts of a multiple movie into one
ECHO  "specs" should be the same, files listed in the correct order, 1 to X...
ECHO  therube 01/23/2015
ECHO.
ECHO  %*
ECHO.
pause


SET     OUT=C:\OUT
ECHO    OUT=: %OUT%
SET   OFILE=%OUT%\%~n1_CONCAT%~x1
ECHO  Output filename: "%OFILE%"
ECHO. > %OUT%\CONCAT1.TXT
PAUSE


echo FOR: 
PAUSE


for %%i in (%*) do echo %%~i >> %OUT%\CONCAT1.TXT
cat         %OUT%\CONCAT1.TXT
PAUSE

sed -f %OUT%\sed_go1 < %OUT%\CONCAT1.TXT > %OUT%\CONCAT2.TXT
cat         %OUT%\CONCAT2.TXT
PAUSE

sed -f %OUT%\sed_go2 < %OUT%\CONCAT2.TXT > %OUT%\CONCAT3.TXT
cat         %OUT%\CONCAT3.TXT
PAUSE


ffmpeg -f concat -safe 0 -i %OUT%\CONCAT3.TXT -c copy "%OFILE%"
And it too has long had an issue with '&' in filenames.
No big deal, I remove the & in the name, run the CONCAT, then put the & back in.

But lately, I've run into a bunch of files with & in their names.

So I start looking through the code & it turns out that it is my quoting that was causing that issue.


14,15c14,15
< SET OFILE="%OUT%\%~n1_CONCAT%~x1"
< ECHO Output filename: %OFILE%
---
> SET OFILE=%OUT%\%~n1_CONCAT%~x1
> ECHO Output filename: "%OFILE%"
37c37
< ffmpeg -f concat -safe 0 -i %OUT%\CONCAT3.TXT -c copy %OFILE%
---
> ffmpeg -f concat -safe 0 -i %OUT%\CONCAT3.TXT -c copy "%OFILE%"


(I've never been good with DOS batch files, in particular with % & %% & "'s...
But at least it looks like I've got CONCAT worked out :-).)


(revised) CONCAT.BAT

Code: Select all

@ECHO OFF

ECHO  CONCATenate all the parts of a multiple movie into one
ECHO  "specs" should be the same, files listed in the correct order, 1 to X...
ECHO  therube 01/23/2015  adjust quoting to fix filenames with '&' 11/24/2018
ECHO.
ECHO  %*
ECHO.
pause


SET     OUT=C:\OUT
ECHO    OUT=: %OUT%
SET   OFILE="%OUT%\%~n1_CONCAT%~x1"
ECHO  Output filename: %OFILE%
ECHO. > %OUT%\CONCAT1.TXT
PAUSE


echo FOR: 
PAUSE


for %%i in (%*) do echo %%~i >> %OUT%\CONCAT1.TXT
cat         %OUT%\CONCAT1.TXT
PAUSE

sed -f %OUT%\sed_go1 < %OUT%\CONCAT1.TXT > %OUT%\CONCAT2.TXT
cat         %OUT%\CONCAT2.TXT
PAUSE

sed -f %OUT%\sed_go2 < %OUT%\CONCAT2.TXT > %OUT%\CONCAT3.TXT
cat         %OUT%\CONCAT3.TXT
PAUSE


ffmpeg -f concat -safe 0 -i %OUT%\CONCAT3.TXT -c copy %OFILE%
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.5
User avatar
therube
Ambassador
Posts: 7922
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Post by therube »

(Along the same lines...)


It seems like on other machine, = and ; are treated as parameter delimiters.

Ding.

So I was having an issue.
(Well, many issues, but that's a different matter ;-).)

I was parsing a "URL", actually just wanting to pass a URL from command line to a batch file.
And the URL had a '=' equal sign in it, http://www.test.com/abc123?key=456.
And when passed to the batch file, all that was returned was, http://www.test.com/abc123?key

(After much looking...) Ding.

So since "treated as parameters", that should mean that %2 would contain something - hopefully useful?

testcase:

Code: Select all

C:\> testpassedURL.bat  http://www.test.com/abc123?key=456
testpassedURL.bat:

Code: Select all

@echo off
echo Getting the passed URL:  %1
echo                         %%2=:  %2
echo.
echo                         ALL: %1=%2
pause
pause
pause
C:\>testpassedURL.bat http://www.test.com/abc123?key=456

Code: Select all

Getting the passed URL:  http://www.test.com/abc123?key
                   %2=:  456

                   ALL: http://www.test.com/abc123?key=456
Press any key to continue . . .

Now certainly not an end-all, but it will meet my needs :-).
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0 SeaMonkey/2.53.2 Lightning/5.8.1
Post Reply