conditional FTYPE?

C

Csaba Gabor

I have a situation which doesn't seem that strange, but I can't figure
out a way around it. I have one file type (ASSOC .php=PHPFile) which I
would like to run one way if it is invoked from the command prompt:
FTYPE PHPFile="c:\php.net\php.exe" "%1" %*

and slightly differently if it is invoked from Explorer:
FTYPE PHPFile="c:\php.net\php-win.exe" "%1" %*

The problem is that this FTYPE acts globally so I can't execute it on a
per command window invocation basis (in other words, make a change
local to that command window session). So the way out that I see is to
have that FTYPE be a conditional to cover both cases. Is that even
possible? It'd be great if someone could show me how.

Thanks,
Csaba Gabor from Vienna


By the way, is there a difference between "%L" and "%1" above? All of
my tests (invoking on file names with spaces, either in or out of
quotes) on my Win XP Pro system showed the same behaviour. Ex: "test
me.php", and I had test me.php show its argv.
 
C

Clay Calvert

I have a situation which doesn't seem that strange, but I can't figure
out a way around it. I have one file type (ASSOC .php=PHPFile) which I
would like to run one way if it is invoked from the command prompt:
FTYPE PHPFile="c:\php.net\php.exe" "%1" %*

and slightly differently if it is invoked from Explorer:
FTYPE PHPFile="c:\php.net\php-win.exe" "%1" %*

I have associated extenions with batch files. The batch files then
have the logic to launch the proper application. In your case, how
are the file types being invoked from the command-line? Are they
being typed in, from a batch file, or what?
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
C

Csaba Gabor

Clay said:
I have associated extenions with batch files. The batch files then
have the logic to launch the proper application. In your case, how
are the file types being invoked from the command-line? Are they
being typed in, from a batch file, or what?

Thanks Clay, that is an excellent thought, and I should be able to make
that fly if the IF statement doesn't work out. To answer your
question, these scripts will primarily be invoked by typing:
test -d some_var=some_path
or just
test

when invoked from the command line (in other words, I plan to add in
PATHEXT), and from Explorer either double clicked or Enter being
pressed. This is primarily for my own efficiency. If invoked
programatically, I am certainly willing to spell things out (so to
speak).

In the meantime, I have put together the following, which pops up the
"What program do you want to use to run this with?" dialog, and when I
select Cancel get Access is denied. In other words, it doesn't like my
(all on one line) FTYPE:

FTYPE PHPFile=IF DEFINED APPDATA ("c:\php.net\php.exe" "%1" %*) ELSE
("c:\php.net\php-win.exe" "%1" %*)

although if I run the IF DEFINED ... from the command prompt, having
replaced "%1" %* with "test.php" it works fine. So I wonder if perhaps
I need to escape something somewhere

Finally, if I do

FTYPE PHPFile="IF DEFINED APPDATA (c:\php.net\php.exe) ELSE
(c:\php.net\php-win.exe)"

and then try it with test.php, I get an alert dialog indicating the
full path of test.php saying access is denied.

Csaba
 
C

Csaba Gabor

Before it gets too buried in the details, I wanted to mention the
reason for this dichotomy. php-win.exe is kind enough not to pop up a
command window when run from explorer, but it does not send output to
the console so if you call it from the command prompt you still get no
output.

php.exe on the other hand does output to the console, but you get a
command window popping up if you start it from Explorer that sticks
around till the program finishes. Ugly.

Each has its place.
Csaba
 
D

David Candy

%L is long file names.
%1 is long file names IF
* Explorer can find the exe file (it does not look very hard)
AND
* The file header says it is Win 95 aware Win16 exe, or
* It is a 32 bit program

Else %1 will be a short name.

Note that just because explorer can't find a file doesn't mean createprocess can't. Explorer just builds a command line and passes it to createprocess. No paths, no App Paths, no multi trying the name (like CreateProcess adding exe to end of the name etc), etc.
 
C

Clay Calvert

FTYPE PHPFile=IF DEFINED APPDATA ("c:\php.net\php.exe" "%1" %*) ELSE
("c:\php.net\php-win.exe" "%1" %*)

Since this is running from a batch, there may be no need to use Ftype.
How about:

Set PHP="c:\php.net\php-win.exe" "%1" %*
IF DEFINED APPDATA set PHP="c:\php.net\php.exe" "%1" %*

Then whereever necessary, simply use:

%PHP%

The script could even be simpler, but we'd need to know a little more
about what else is going on:

IF DEFINED APPDATA (c:\php.net\php.exe "%1" %*) else
(c:\php.net\php-win.exe "%1" %*)

Let us know.
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
C

Csaba Gabor

David said:
Command prompt doesn't use associations anyway.

Thanks for the clarification on %L - I suppose that's what I'll be
using now.
But I just didn't understand your comment about associations and the
command prompt - it must mean something different than ASSOC.

Csaba
 
C

Clay Calvert

Command prompt doesn't use associations anyway.

I would have replied via e-mail, however I can understand why one
wouldn't want an e-mail address is UseNet.

From the command-line in a directory containing a file called
"test.txt" simply type:

test.txt

and the file should be opened in the associated application, typically
notepad.

It seems that typing in a filename will launch the same action as
double-clicking on the file in Explorer.
Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
T

Todd Vargo

Csaba Gabor said:
Thanks for the clarification on %L - I suppose that's what I'll be
using now.
But I just didn't understand your comment about associations and the
command prompt - it must mean something different than ASSOC.

ASSOC displays and sets file associations. However, the command line only
uses the ones set forth in the PATHEXT variable. For example, if PATHEXT
contains the following...

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS

Then only those associated extensions are available directly on the command
line. Now if we add to it an associated extension, then that association is
available to the command line also.

SET PATHEXT=%PATHEXT%;.htm

So now if we have a file named MyExample.htm, we can simply type MyExample
and the file will be opened with it's associated application. As my example
has shown, the statement, "Command prompt doesn't use associations anyway"
is false in Windows 2000/XP and in Windows 9x/ME using the Win95cmd ported
from Windows 2000. (I don't know if NT has this capability also.)
 
C

Csaba Gabor

FTYPE PHPFile=IF DEFINED APPDATA (c:\php.net\php.exe "%1" %*) else
(c:\php.net\php-win.exe "%1" %*)

For those arriving late to the program, in today's exciting episode
everyone is wondering whether there is a way to have php.exe run when
test.php is typed at the command prompt Vs. having php-win.exe run when
it is double clicked from Explorer. This is because php-win.exe
suppresses the ugly command window when it runs, but only php.exe will
send its output to the command window (thus enabling useful things like
debugging).

Earlier we saw that
ASSOC .php=PHPFile
FTYPE phpfile="CScript.exe" c:\php-net\dispatchPHP.vbs "%1" %*

tied extensions to actions so that when either test.php was typed or
doubleclicked, the file (along with any arguments from the command
line) would be passed to dispatchPHP.vbs Let's tune in to see how it's
going......


Here's what I wrote for dispatchPHP.vbs:
Set oSH = CreateObject("WScript.Shell")
runPrefix = "c:\php.net\php.exe "
runLine = ""
Set oArg = WScript.Arguments
For i=0 to oArg.count-1 'Reassemble all the arguments
runLine = runLine & """" & oArg(i) & """ "
Next

'Next line detects if we are in a command window
cmdP = (oSH.Environment("Process").Item("=ExitCode")<>"")
If cmdP Then 'In a command window
oSH.Exec runPrefix & runLine
Else 'Else (Explorer)
runPrefix = "c:\php.net\php-win.exe "
oSH.Run runPrefix & runLine, 0, false
End If


Hurrah, hurrah, it works! But there is one problem which I would love
a solution for. Because FTYPE specified CScript, it means that when
invoked from Explorer, there will be a momentary flash from the CScript
command window while it dispatches PHP. Well, that is better than it
staying around for the whole process, but can't we do better by telling
FTYPE WScript instead of CScript? In fact, that will clean up the
flashy thingy on the screen, but now I cannot get output to the command
window when I invoke test.php from the command window. And that is
what today's episode is all about.


So let's assume that the FTYPE now has WScript instead of CScript.
How can I get WScript to get output to the command window?
I tried:
1. reentering via CScript and then WScript.StdOut.Write modeled on:
http://groups.google.com/group/micr....vbscript/browse_frm/thread/54d3df2e29211bad/
2. Set fs=CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("CON:",True)
a.Write "Something"
modeled on:
http://groups.google.com/group/micr....vbscript/browse_frm/thread/70a2ff21a70c5ca9/
3. An "unsupported" Tom Lavedas idea:
dim sout, oFS
const StdOut = 1
set oFS = CreateObject("Scripting.FileSystemObject")
set sout = oFS.GetStandardStream(StdOut)
sout.Write "Hello World"
described at:
http://groups.google.com/group/micr....vbscript/browse_frm/thread/eead9d107d85041c/

all to no avail.
1 doesn't complain - you just don't get anything.
2 dies with Permission denied.
3 dies with handle is invalid on the sout.Write line.

Can anyone improve my scenario?

Frankly, I think the best idea (though I have not got it work) would be
along the lines of modifying FTYPE to be something like:
FTYPE PHPFile=IF DEFINED PROMPT ("c:\php.net\php.exe" "%1" %*) ELSE
("c:\php.net\php-win.exe" "%1" %*)

possibly completely enclosed in quotes. But my attempts have so far
failed in permission denied or being asked by Windows what program to
use to run the file.

Csaba Gabor from Vienna

Oh, (note the last line should print to the command window) test.php
is:
<?php
$oWS = new COM("WScript.Shell");
$out = implode("\n", $GLOBALS['argv']);
$oWS->popup ($out, 4, "PHP Popup", 131120);
var_dump ($GLOBALS['argv']);
?>
 
T

Todd Vargo

Clay Calvert said:
I would have replied via e-mail, however I can understand why one
wouldn't want an e-mail address is UseNet.

From the command-line in a directory containing a file called
"test.txt" simply type:

test.txt

and the file should be opened in the associated application, typically
notepad.

It seems that typing in a filename will launch the same action as
double-clicking on the file in Explorer.

Where did I ever get that dumb %PATHEXT% info? That's what I get for reading
NT/XP material a year before using XP full time. Well, I did remember the
command line uses file associations (without the need for START as Win9x/ME
does).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top