ftype with %1 in batch file

HI,

I have these 2 lines in a batch file to do file association.

assoc .ewv=ThinRDMReEditWVAnnotFile
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%1"

Is the syntax correct? When I ran it, it did not take the %1. It became a space instead of %1.

Is there anyway to fix this?

Thanks in advance.
ileaf-qsAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
No real need to be confused. Starting the script with the argument "%1" will work in this case, but this is treating the symptoms, not the cause.
"%1" in a batch file will expand to the first *argument* handed to the script. If you have a command that needs to contain the *string* "%1", and the argument passed is the string "%1", the "%1" *variable* in the script will expand to the *string* "%1" (which was passed in the command line). This will create the desired command, provided that the argument string "%1" is passed.
But as I said, this is treating the symptoms, not the cause, and it's likely to produce unnecessary errors. The cause here is the use of the *argument* "%1" in a command where there should not be the argument, but the *string* "%1". And to prevent the interpreter from treating "%1" as argument, you need to duplicate the "%" sign to "%%1", which will then be replaced by the interpreter in the resulting line with the desired *string* "%1".
The situation with the variable in a "for" loop is similar: if you use a "for" loop directly in the command window, you type "for %a in (...)". In a batch script, though, the interpreter would try to expand the "%a" while reading the line, which would then result in the command "for  in (...)". That's why in a batch script, you need to duplicate the "%" sign for the variable: "for %%a in".
0
 
millsoftCommented:
Hi ileaf-qs,

%1 in a batch file means take the first parameter off the command line and substitute it in.

Therefore, instead of running the batch file:

mybatch.bat

run it this way:

mybatch.bat %1


Cheers!
Brad
0
 
oBdACommented:
If you want to output a "%" sign in a batch script, you need to  duplicate it:
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%%1"
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lijunguoCommented:
//assoc .ewv=ThinRDMReEditWVAnnotFile
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%1"//

the syntax is correct.

ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%1"
this creates the file type
ThinRDMReEditWVAnnotFile
ans associates the prototype command
"c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%1"
with the type. Actually, the use of double quotes around the %1 parameter. It enrures that the command is handled correctly even if the specified document name contains spaces.

Lijun
0
 
oBdACommented:
lijunguo,
the syntax <ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%1"> is only correct if you enter it manually in a command shell. It is *not* correct for use in a batch file, because, as it happened to ileaf-qs, the <"%1"> will not be part of the command as it will be expanded to an empty string. The command generated in a batch file with this syntax is
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" ""
To generate a percent sign as part of a command, you need to duplicate it: <"%%1">

Try for yourself:

@echo off
setlocal
echo Echoing ["PercentSign DigitOne"],
echo which will come out as an empty string: ["%1"]
echo.
echo Echoing ["PercentSign PercentSign DigitOne"],
echo which will come out as ["PercentSign DigitOne"]: ["%%1"]
0
 
lijunguoCommented:
oBdA,

You're right, it is only correct in a command shell. It is *not* correct for use in a batch file.

Lijun
0
 
lijunguoCommented:
oBdA,
I'm a little bit confused about the syntax, if the syntax's not correct, if I run it with a command line argument, it's working. It's not empty. If I run it without a command line argument, it's not working as expected. So the syntax is correct or not, depends on how you are running, is that right?

Lijun
0
 
lijunguoCommented:
Thanks oBdA for your post!

Lijun
0
 
lijunguoCommented:
oBdA,
I will cut & paste your comments.

Lijun
0
 
CaudaxCommented:
Simply stated:

The problem is in the batchfile.

%1 is a variable that applies to file associations and batch files.

When it is run, the batchfile replaces %1 with the first word given after running the batch file.

Anyway, in order to fix the problem you have to double the %% sign so that it responds with a single % instead of taking the variable.

Here's the fixed code:

assoc .ewv=ThinRDMReEditWVAnnotFile
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%%1"
0
 
K_2KCommented:
I agree, oBdA's first responce was correct and should get the credit.

To see the cause of needing this, simple type both of these commands, in the immediate mode and in a batch file, and see the difference in the result:
   echo %
   echo %%

I add only to clarify: when you double-click a file, the ftype command is parsed with the command specified by the ftype associated with that file type, and the file name automatically added in place of the %1.  This is done so you can have the default open or print command for a given extension have any options you need added before or after the the file name as some commands may require.

After running the correct batch command with two %% you can see the result is the same as immediate mode using one % by entering the ftype command to display current setting:
   ftype ThinRDMReEditWVAnnotFile


(\o/)  Hope this helps,
2K
0
 
ileaf-qsAuthor Commented:
Dear All,

The solution from oBdA work and oBdA should get all the credit.  Thanks also everybody for responding to my question.

This is the first time I posted a question at EE and it is really nice working with you all. May I know how can I stop the thread for this question?
Robert
0
 
K_2KCommented:
You can't stop it, but it is "out of sight, out of mind" now that you accepted an answer, so by nature not many responses will be added, even though Google will now show your thread to anyone searching for a matching question.

You CAN stop getting emails when it's added to by clicking "Unsubscribe" if you really get bugged too much by more responses.

You're welcome to the help, and also welcome to browse the newer questions you may be able to help us answer.  Many of us experts are simply folks who've "needed that before" and remember the answer someone gave us.

(\o/)  Nice to meet you,
2K
0
All Courses

From novice to tech pro — start learning today.