'How do I open a file for edit from the command line under Windows?

How do I open a file for edit from the command line under Windows?

Mainly I am looking to open the file in the default editor associated for it (not to be confused with default action for this filetype).

This is different than just "executing" the file, so start filename is not a solution.

Note: this would require to use ShellExecute in one way or another.

Update: I added Python as an alternative to batch.



Solution 1:[1]

Here is a sample Python script that opens a file for edit, if there is an editor assigned to its filetype.

import os
from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

filename = "readme.txt"
os.startfile(filename, "edit")

try:
    os.startfile(filename, "edit")
except WindowsError, e:
    MessageBox(text=str(e))

Solution 2:[2]

64 bit Windows does not support edit command. https://www.computerhope.com/issues/ch001303.htm

To open file in with default associated app, in CMD use start <<file_path>> Reference: How to open file with default application in cmd?

To open a file in notepad, in CMD use notepad <<file_path>> in CMD

  • if file is not associated with default app to open - error will be displayed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sorin
Solution 2 rajkrish06