Using Octave to "Edit" notepad file instead of "Open" in Windows - windows-10

I use Windows 10 and an .exe program (in-house code written by a colleague) that imports data from .txt files. Since 99% of my use of .txt files are for this program, I've changed the default Windows program so that this .exe file is run automatically when opening a .txt file. If I need to access the .txt file directly, or use it for another purpose, I right-click and choose "edit."
I'm now writing a program of my own (using Octave 4.4.1), which also uses .txt files that sometimes need to be opened/edited, but if I use "open(filename)" in my Octave script, of course it just opens the .exe file. I can open the .txt file from there, but I'd like to skip this middle step, since the aforementioned .exe program is not intended to be used in this process, and there are other users of my code that don't have the .exe program installed.
Is there a way to duplicate the right-click/edit feature in Windows within Octave code? "edit(filename)" opens the file in the native Octave editor, which is technically viable, but not exactly a desirable scenario. I've also tried changing the default Octave editor to Notepad, and I've tried Notepad++ as well, but I have had absolutely no luck, even with significant effort, of making Octave use an external default editor of any kind (even when I remove the .exe program as the default for .txt files). Thanks in advance for any advice you can offer.

You can send command-line commands from Octave using the system() function.
For example, to open the file in notepad, you could do
[status, output] = system("notepad <path_to_text_file>.txt");
If notepad isn't in your system path, you will have to add it to or use the full path to the notepad executable
Or, if you want to use Notepad++, add it to your system path and then do
[status, output] = system("notepad++ <path_to_text_file>.txt");

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

How do you change the default program to open a file in Python?

I'm using os.startfile(path\file.stl) to open an STL file. By default, the STL file is opened in the program Print 3D (using Windows 10). I would like my Python program to not open the STL file in the default program, but rather in Paint 3D (or a different stl viewing program of my choosing). How do I open a file in the non-default program?
import os
os.startfile(r'C:\Users\my\path\to\file.stl')
#opens file.stl in default program, not the program I would like
The docs for os.startfile suggest that there's no way to do that (at least no way with os.startfile).
the file is opened with whatever application (if any) its extension is associated.

How to run a bat file in windows from linux systm

I recorded a certain process by autohotkey and saved as auto.ahk in Windows system. I need to run that from Linux system, so I created a bat file with content start 'd:\auto.ahk' and saved as test.bat. But when I use the following code in terminal, it doesn't work
ssh user#192.00.00.00 'd:/test'
Because of the fact that you .ahk script requires the actual autohotkey program on windows to run (it's the interpreter between raw text and binary commands to windows), what you're likely running into is that the bat file is trying to just "run" your .ahk script, but can't run a raw text file like a program. What you may be able to do to fix this is compiling your .ahk script, making it an executable. Then, you could call the executable directly, just like you would any other .exe on the computer. (you might also want to try starting any other .exe this way like notepad to make sure your method of running the program is correct)
Another solution would be, as #Matthew Strawbridge suggested, to have a script running quietly in the background of the computer, periodically checking the contents of a file that the .bat can write to. This has the added bonus of you being able to customize what the script does by what is written in the check up file.

How can I pass a file to an executable, the way dragging-n-dropping allows?

Consider I have the following :
An Excel workbook, which generates a .txt file (through a macro).
An executable file (.exe) which can parse this .txt file.
My objective here is to program the following algorithm :
Get the user to enter his data in the worksheets.
Prepare the .txt file according to what the user entered.
Pass the newly created .txt file to the external program (.exe).
Now, the first two steps work perfectly, I can generate the proper .txt file. The problem comes when I need to pass it to the external executable. Here's what's tried and observed :
Calling Shell() with : "C:\path\to\program.exe" "C:\path\to\file.txt" (EDIT : as a single string) ; simple enough, yet this does not work : the external .exe doesn't like being given a path. I have also tried entering the command directly into Windows' Execute utility : same. It seems like giving the program a path to the .txt isn't enough.
Dragging the .txt file onto the program's .exe works! The external executable does its work correctly. Here, it is as if I had passed the "file" to the program, not just its path. As a UNIX user, this sound like quite a non-sense to me (without a notion of stream or pipe), so maybe I'm missing something here...
Now, here's my question. When dragging-n-dropping a file onto an executable, how does Windows "translate" it ? Obviously, it does not translate it to "pass the file's path as the program's first parameter". How can I pass the .txt file to the executable program, without just passing its path like I did with my first Shell() call ?
A few other notes :
I am using Microsoft Office Excel 2010 on Windows 7. I'm already dying from it, thanks for your concern.
This external .exe I'm talking about cannot be modified and recompiled (which would be awesome), that's the tricky thing. I have to use it as it is, whatever happens.
Using Shell() is not a problem, if you give me the proper command/fix. I have also tried calling Run on a VBA.CreateObject("WScript.Shell"), same result.
Now, here's my question. When dragging-n-dropping a file onto an
executable, how does Windows "translate" it ? Obviously, it does not
translate it to "pass the file's path as the program's first
parameter".
It does exactly that. Create a batch file:
#echo off
echo You passed parameter: %1
pause
Save it as test.bat
Browse to it in file explorer, and drag/drop something onto it:
Previous comments:
Looks like you have to send the path and filename as one parameter, e.g.:
Shell("C:\path\to\program.exe C:\path\to\file.txt")
or if the paths have spaces in them, maybe:
Shell("""C:\path\to\program.exe"" ""C:\path\to\file.txt""")
http://social.msdn.microsoft.com/Forums/en-US/a3a3d1a7-5c9a-4c91-ab96-41d367eac2fb/using-shell-function-in-vba-how-do-i-pass-a-parameter?forum=accessdev

I've downloaded an .exe file but it closes quickly as it opens

I am trying to open a downloaded .exe file but it closes as soon as it opens. Is there any possible way so that I can open it for a longer duration to read the content.
It's probably a console application rather than a GUI application. Use the command prompt to run the .exe.
Do the following...
Hold down your Windows key on your keyboard and then tap "R".
This will bring up the Run dialog. Type in "cmd" (without the quotes). Hit enter.
(this will work in all Windows versions - browsing the start menu/screen differs in each version)
If you saved the file to c:\downloads and it's called myFile.exe, type
C:
cd C:\Downloads
myFile.exe
Some of the steps are a bit redundant - if you know what you're doing in the command prompt then skip as needed (but then you probably wouldn't be posting this question). This will work even if you saved the file to D:\downloads.
Another example - if you saved the file to D:\folderA\Folder with a space\ and the file is called "my file with a space.exe" then type
D:
cd "D:\folderA\Folder with a space"
"my file with a space.exe"
If there is an issue (eg it's a 64-bit executable and you're on 32-bit Windows) then you may get a better error message at the command line.
There are so many reasons why the executable does not run. Here are some ways to check what is going wrong:
Is it your .exe? Do you known the "normal" behavior?
When you download it manually, it the result the same?
Do you download the .exe manually or via your application?
Do you see any problem in your Windows Event Viewer?
Is it the same result if you try to download the .exe via different browsers (IE, FF, ...)?
More details are welcome!
The nuget.exe file is not a console GUI application but rather a console package. Once you've downloaded it, you'll want to place it in a folder outside your Downloads folder. For example, C:\NuGet\nuget.exe - then set it as a PATH variable so that it's executable from anywhere.

Resources