locked
CMD syntax question / opening a file in default text editor RRS feed

  • Question

  • Hi!

    I'd like to use a command line to open a file (not .txt) in the user's default text editor (the program that is associated with the txt file extension)

    I managed to get the path to the exe using 'assoc .txt' (which gives 'txtfile' as the output) and 'ftype txtfile', which gives the path to the exe.

    this extracts the exe path:
    for /f "delims== tokens=2" %a in ('ftype txtfile') do @echo %a

    However, it doesn't seem to respond to changes in the normal Windows Explorer file type associations.
    It always outputs notepad.exe, no matter what program I select to open .txt files.

    Is there any way to accomplish this with a single line?

    cheers,
    Daniel

    Wednesday, July 13, 2011 10:07 AM

Answers

  • I'm missing what you're trying to do.  Are you trying to determine WHAT program is the default program for opening .txt files?  Are you trying to determine the default program for some other extension?  Or are you trying to determine how to open a specific file type in a one-liner?

    If 1) http://support.microsoft.com/kb/257592 -- HKLM\Software\Classes or just HKCR and find the .txt key
    If 2) See above but look for any other extension besides .txt
    If 3) If it is a text-file editor you're looking to force always open a file with, oftentimes text editors will take the file to open as an argument so you don't even need to know the default extension:
       - notepad "sometext.txt"
       - wordpad "sometext.txt"
       - c:\program files\notepad++\notepad++ "sometext.txt"

    Of course you need to reference the proper path if the executable is not accessible via the PATH variable.

    • Proposed as answer by Bill_Stewart Wednesday, July 13, 2011 2:16 PM
    • Marked as answer by IamMred Saturday, July 16, 2011 6:12 AM
    Wednesday, July 13, 2011 11:37 AM