Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Opening a text file in its native application

by FM (Acolyte)
on Jan 16, 2006 at 05:56 UTC ( [id://523400]=perlquestion: print w/replies, xml ) Need Help??

FM has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,

I don't understand why the following piece of code does not work the way I expect it (it opens a command prompt window when I expect it to open notepad). My goal is to open a .txt file in its native application which I assume to be notepad. The file is created and closed before I attempt to run this piece of code.
sub logfile{ my $file = "$gGui{complogpath}/$gGui{complog}.txt"; my @args = ("start",$file); for (@args) { if (/ /) { $_ = qq{"$_"}; } }; system(@args) == 0 or warn "Couldn't launch '$file' : $!/$?/$^E"; }#logfile
The following code opens Guide in its native application (adobe). In this case I don't worry about the path because I know that that file is in the same folder than the application code.
sub help{ my $file = "Guide.pdf"; my @args = ("start",$file); for (@args) { if (/ /) { $_ = qq{"$_"}; } }; system(@args) == 0 or warn "Couldn't launch '$file' : $!/$?/$^E"; }#help

In the first code that does not work, I have to give the full path and I have a feeling that it is the problem. I don't pass all the pieces correctly. (?)

I would appreciate any thoughts you may have.

Thanks in advance for your time.

Claire

Replies are listed 'Best First'.
Re: Opening a text file in its native application
by sk (Curate) on Jan 16, 2006 at 06:44 UTC
    Start does not like spaces in the dir names (even if you quote them). Here is one way -

    use strict; use warnings; use File::Basename; my @args = ("start/d", "c:/program files/T.txt"); my $file = pop(@args); push (@args, '"'. dirname($file) . '"', basename($file)); system(@args) == 0 or die $@;

    (This will not work for filenames with spaces, not sure if start can handle it anyways).

    -SK

      Hi SK,

      This is wonderful! It works!

      Thank you so much!

      Claire

Re: Opening a text file in its native application
by helphand (Pilgrim) on Jan 16, 2006 at 06:10 UTC

    My guess is that the start command will not accept a path.

    START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE ¦ /SH +ARED] [/LOW ¦ /NORMAL ¦ /HIGH ¦ /REALTIME] [/WAIT] [/B] [command / pr +ogram] [parameters]

    You might try using the /Dpath switch to specify the startup directory, that may work. Can't check here, no windows system available at the moment ;)

    Scott

      Hi Scott,

      Thank you for your reply. I have tried a few different ways to use the /Dpath switch, but I am not sure I got the syntax right--even though Perl is not giving me any warning. Could you give me an example with my code?

      Thanks,

      Claire
Re: Opening a text file in its native application
by BrowserUk (Patriarch) on Jan 16, 2006 at 06:26 UTC

    It works for me. With or without the path; with the path equating to the cwd or not; it works.

    What if any error messages are you getting when it fails?

    What output do you get from the following commands?

    P:\>assoc .txt .txt=txtfile P:\>ftype txtfile txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Can you try the start command with a directory that contains a space? Start (at least for me) balks on dirnames with spaces. I am not sure if it will ever work on filenames with spaces. You have to use /d and specify the path with the space.

      Hi,

      I get the same answers than you when I run the two commands you gave me.

      Hi,

      It does not give me an error message. It opens a command prompt window instead of the file.

      hi,

      I try the start command and opening a file and it works for me too, but in my code, I have a button that calls that sub and that might be the problem??

      Thanks,

      CL

        sk hit the nail on the head. The start command appears to have problems with paths that contain spaces, even if you quote them. However, I have found that it you quote the individual element of the path that contains the space, it then works.

        So, for the path c:\program files\junk.txt, if you issue the command

        system 'start', '"c:\program files\junk.txt"';

        it fails, but if you issue the command

        system 'start', 'C:\"program files"\junk.txt';

        It succeeds. So you would need to modify your quoting mechanism to be something like this (untested):

        sub logfile{ my $file = "$gGui{complogpath}/$gGui{complog}.txt"; my @args = ("start",$file); for (@args) { s[ ( [\\/] ) ## A path delimiter ( [^\\/]* \s [\\/]* ) ## Two or more non delimiters ## including a space ( [\\/] ) ## Another delimiter ][$1"$2"$3]gx; ## Quote the path element }; system(@args) == 0 or warn "Couldn't launch '$file' : $!/$?/$^E"; }#logfile

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Opening a text file in its native application
by pKai (Priest) on Jan 16, 2006 at 11:47 UTC

    my $long = 'D:/temp/spa ce/ab cd.txt'; my $short = Win32::GetShortPathName($long); system('start', $short);

    This will also avoid the problem that spaces in the actual filename cannot be handled/escaped in the start call.

    It's not necessary to issue a use Win32;, since GetShortPathName is accessible as core function in AS perl on Win.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://523400]
Approved by perl_lover
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found