CMD: Fails with %1
Recently i posted a question about what the variable is of a file which you open with a batch file, which is %1.
I used this, to let my batch file copy %1 to a specific location.
After doing this, debugging and a lot of time, and it actually worked, after debugging it more, adding some features, and some more debugging, it stopped working. This is a snippet, where it seems it goes开发者_开发问答 wrong (i tried @echo on
to get my info.)
if not "%1:~-8%"==".package" (
set error=yes
set errmes1=File %1 does not have a .package extension.
goto :errpkg
)
copy "%1" "C:\path_to\folder"
:errpkg
cls
echo %errmes1%
...
(%1=C:\path\to\it\file_something.package
)
This is the point were it goes wrong, i guess. Because it ends up with this message. What it SHOULD do, is at if not
checking if the file has a .package extension. then, set error=yes
, just says what it does, set errmes1
sets an error message. and then the goto
just goes to some place in the batch file where it explains what the problem is. (using %errmes1%
)
So, that's it. Here is where it goes wrong. I am 100% sure it has a .package extension. Could anyone help me, to tell me what the problem is?
I seriously wonder how you ever got that to work. The substring syntax only works on environment variables (well, and pseudo-variables such as %cd%
). That means you can do
%foo:~-8%
but not
%1:~-8%
This wouldn't even work if you had an environment variable named %1%
.
Anyway, the usual idiom to check for the extension of an argument is
%~x1
So why complicate things more than necessary?
Tried using %~x1 to extract the extension? Your syntax doesn't work for me.
Also, can't setting variables inside the conditional get screwy in cmd.exe?
精彩评论