Batch file: & filename with ampersand

The battleground for flame wars, off-topic derailments and spurious madness.

Batch file: & filename with ampersand

Postby therube » Wed Oct 21, 2015 3:59 am

(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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Postby therube » Wed Oct 21, 2015 4:27 am

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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Postby therube » Wed Oct 21, 2015 4:45 am

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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Postby therube » Wed Oct 21, 2015 4:52 am

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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Postby therube » Wed Oct 21, 2015 5:54 am

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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Batch file: & filename with ampersand

Postby therube » Sat Nov 24, 2018 2:20 pm

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: 7209
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA


Return to Ragnarök

Who is online

Users browsing this forum: No registered users and 1 guest