Solved

ftype with %1 in batch file

Posted on 2004-04-08
13
642 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:ileaf-qs
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 5

Expert Comment

by:millsoft
ID: 10786204
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
 
LVL 83

Expert Comment

by:oBdA
ID: 10787753
If you want to output a "%" sign in a batch script, you need to  duplicate it:
ftype ThinRDMReEditWVAnnotFile="c:\rdm2word\ThinRDMReEditWVAnnot.exe" "%%1"
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 10790082
//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
 
LVL 83

Expert Comment

by:oBdA
ID: 10790526
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 10794840
oBdA,

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

Lijun
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 10795002
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 83

Accepted Solution

by:
oBdA earned 100 total points
ID: 10797233
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 10799347
Thanks oBdA for your post!

Lijun
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 10799372
oBdA,
I will cut & paste your comments.

Lijun
0
 
LVL 4

Expert Comment

by:Caudax
ID: 10801701
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
 
LVL 8

Expert Comment

by:K_2K
ID: 10803615
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
 

Author Comment

by:ileaf-qs
ID: 10816108
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
 
LVL 8

Expert Comment

by:K_2K
ID: 10850742
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now