I have been doing a lot of reading and can't quite find an exact answer to my question. I have seen similar posts but not exactly this.
I have a program that generates names of files like:
d:\a and b\1 and 2.mkv
d:\a & b\1 & 2.mkv
d:\aandb\1and2.mkv
The point being is I can't control the output of the folder name or file name. I have a post processing script that takes that fully qualified file and runs commands on it. I can't seem to correctly call my post processing script that will correctly handle the fully qualified filename when the fully qualified filename could be any of the examples listed. Because I can't control the folder or file name, escaping the ampersand won't help. I have tried about ever combination of variables while using !variable! or %variable% and I can't seem to come up with a way that will handle any combination of folder or filename with ampersands or spaces in them.
How do I correctly pass the fully qualified filename to another batch file so that it correctly interprets the fully qualified filename whether it has spaces oe ampersands or doesn't have them?
Just one of the examples:
test.cmd "A & B\1 & 2.mkv"
where test.cmd
@echo off
@setlocal EnableDelayedExpansion
@set TARGETDRIVE=%~d1
@set TARGETPATH=%~p1
@set TARGETNAME=%~n1
@set TARGETEXTENSION=%~x1
@echo TARGETDRIVE=!TARGETDRIVE!
@echo TARGETPATH=!TARGETPATH!
@echo TARGETNAME=!TARGATNAME!
@echo TARGETEXTENSION=!TARGETEXTENSION!
The output:
'B' is not recognized as an internal or external command,
operable program or batch file.
'2' is not recognized as an internal or external command,
operable program or batch file.
TARGETDRIVE=z:
TARGETPATH=\temp2\A
TARGETNAME=
TARGETEXTENSION=.mkv
Just a few tips:
@means "do notechothe command to the console". Command-echoing is controlled byecho onandecho off.Echois ON by default, so the command@echo offmeans "Turnechooff, but do notechothis line to the console.`Hence, once an
@echo offis executed, there is no need to put@before commands.In your third
echostatement, you have specified targAtname, which is undefined, hence the report shows nothing for that item.!in place of%means "the value of the variable as it is at run-time, not at parse-time" and is really only applicable where a statement invokes a loop which changes the variable, like aforloop, but asaschipflhas pointed out, it comes in useful to overcome the special-characters effects in anechostatement.Let's examine what batch does for one assignment:
Batch substitutes the
pathpart of the 1st parameter for%~p1so the actual command executed ishence it assigns
Atotargetpathand then attempts to executeB\hence the error message.On a string assignment, it is standard practice on SO to use the syntax
The quotes protect against stray trailing spaces on the command line, and will also cure most problems with special characters.
BTW:
will
echoall variables whose names starttarget