tags:

views:

46

answers:

3

Hello,

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?

A: 

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?

covener
Why should it get screwy? You're just doing variable assignment and a jump afterwards. Nothing fancy there and certainly pretty little chance to screw it up.
Joey
+2  A: 

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?

Joey
so how would i do `%~x1` in my case?
YourComputerHelpZ
Replace `%1:~-8%` by `%~x1`.
Joey
nevermind, got it. :) Works like a charm, thanks a lot.
YourComputerHelpZ
A: 

I agree with the previous answers about using %~x1 to check for the extension.

Edit: Try the following:

if not "%~x1%"==".package" (

Besides, you are probably missing a goto statement after the copy to skip the block where error messages are shown?

...
copy "%1" "C:\path_to\folder"
goto :skipErrpkg

:errpkg
cls
echo %errmes1%
...

:skipErrpkg
stakx
of course it does.
YourComputerHelpZ