Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ftype with %1 in batch file

Posted on 2004-04-08
13
Medium Priority
?
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85

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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 85

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
 
LVL 85

Accepted Solution

by:
oBdA earned 400 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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