Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Open txt file using default program on Windows

by tanyeun (Sexton)
on May 31, 2013 at 21:42 UTC ( #1036337=perlquestion: print w/ replies, xml ) Need Help??
tanyeun has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monk,

I am developing perl using ActivePerl on Windows.
How do I open a txt file using the default program?

Currently I used this:
system("start foo.txt");

It works perfectly on my machine but it doesn't work too
will when I try to run my program from other people's machine.

Any suggestions?

Thanks,

David

Comment on Open txt file using default program on Windows
Download Code
Re: Open txt file using default program on Windows
by Anonymous Monk on May 31, 2013 at 22:03 UTC

    Any suggestions?

    Type help start and read that document , also help cmd and  perl -V:sh , and use Win32::ShellQuote and cmd.exe /x /c start "" "file"

Re: Open txt file using default program on Windows
by BrowserUk (Pope) on May 31, 2013 at 22:41 UTC
    it doesn't work too wiell when I try to run my program from other people's machine.

    What does that mean?

    Possibilities:

    • The program lives on your machine and the associated file is on another machine, and when you run the script on their machine, it fails to find the program.
    • If you copy the program to their machine, and run the script on their machine, it fails to find the association.
    • You install the program on their machine and run the script on your machine; and it fails to find the program.
    • ...Too many more interpretations to list them all ...

    If English is not your first language -- or even if it is -- try asking someone else to read the text of your post and suggest improvements; because as written, it is not at all clear what you are asking for.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Open txt file using default program on Windows
by Jim (Curate) on May 31, 2013 at 23:22 UTC

    Curiously, this works on my Microsoft Windows computer. It opens the file named foo.txt in Notepad.

    C:\>perl -e "system('foo.txt')" C:\>assoc .txt .txt=txtfile C:\>ftype txtfile txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1 C:\>

    I've never written a Perl script that opens a text file in Notepad (or any interactive text editor), and I can't imagine why anyone else would.

      I've never written a Perl script that opens a text file in Notepad (or any interactive text editor), and I can't imagine why anyone else would.

      See perlbug.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        What's in perlbug about invoking Notepad with Perl?

Re: Open txt file using default program on Windows
by rpnoble419 (Pilgrim) on May 31, 2013 at 23:50 UTC

    Always check what program is assigned as the default text program. If its not Notepad, then assign it

      Not on MY Machine, you don't.

      Programs that try to modify my configuration without asking probably have other socially unacceptable habits as well.

      ----
      I Go Back to Sleep, Now.

      OGB

      Well... you got half it right... if there is no program associated with the ".txt" extension, you need to bring up some other default (or ask the user), but I wouldn't go changing their registry unless you ask them if then want you to fix it....
      I'm going to go ahead and assume that by "assign it", you meant "open the file in Notepad" and not "edit the registry to associate txt files with Notepad". Even if that assumption is correct, no. Just no. Don't override people's preferences for text editors. If the guy wants to view txt files in Notepad++ instead of Notepad (and who can blame him), then let him view his txt files in Notepad++.

      I use system ("\"$filepath/log.txt\"") to open log files in case of errors. It seems to work OK.

      I should know better then to answer these when I'm tired.

      As he has permission to install his program and Perl with it, I assume (we all know where that can lead) I hope he has the smarts to ask the end user first if its ok to make any changes. But a better solution would be to do the following

      system ('start c:\windows\notepad.exe filename.txt');

      Now he will always get Notepad to open the text file.

Re: Open txt file using default program on Windows
by ikegami (Pope) on Jun 02, 2013 at 19:19 UTC

    Why didn't you show the command that fails? You're leaving us guessing.

    You might have stumbled into a problem resulting from start's weird syntax which can be addressed as follows:

    my $doc = ...; system(qq{start "" "$doc"});
      Thanks guys for the reply

      When I am debugging programs at work, I need to do the following very often:
      1. cat the kernel log
      2. save it to a file
      3. open file and search for possible failures

      So I wrote this small program to do the above 3 steps with one button click via TK
      I put this program in a network drive in the lab where IT set it up that can be accessed by everybody on their own machines.

      I can't really show the error message here because I can't reproduce it on my machines or any of the machines in my lab.
      As I remembered that I couldn't open the text file on others' machine and windows popped out warning message.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-28 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls