Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 410
sorin Avatar asked Nov 13 '22 15:11

sorin


1 Answers

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))
like image 84
sorin Avatar answered Dec 21 '22 01:12

sorin