How does File Explorer open files? - node.js

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.

Related

Opening a random file only knowing the extension name anywhere on the computer in python [duplicate]

I want to do the following:
Save numeric data in a CSV-like formatting, with a ".foo" extension;
Associate the ".foo" file extension with some python script, which in turns opens the .foo file, reads its content, and plots something with a plotting library (matplotlib most probably).
The use-case would be: double-click the file, and its respective plot pops up right away.
I wonder how I should write a python script in order to do that.
Besides, the windows "open with" dialog only allows me to choose executables (*.exe). If I choose "fooOpener.py", it doesn't work.
This isn't really a programming question, but what you need to do is to figure out how to get the Python executable into the registry key that opens your data file.
For example, I created a little Python script called opener.py that looks like this:
import sys
print(sys.argv)
input()
Then I created a testfile.foo and used the "change" button in that file's property dialog to choose opener.py. (You can do this if you click Browse and change the Open With dialog's file filter to "All Files".)
Of course this didn't work (as you noticed). So I opened regedit and searched for opener.py and found it at the following registry key:
HKEY_CURRENT_USER\Software\Classes\Applications\opener.py\shell\open\command
The default value of this key was "C:\opener.py" %1. I changed it to python "C:\opener.py" %1. It worked!
Long story short, to do this properly you need to custom-edit the registry. Actually setting up the file association is more complex than just editing that one key (you have to also indicate that .foo is associated with opener.py).
An alternative approach would be to turn your Python script into a standalone executable using one of the several tools available for that purpose, or write a small executable in C that launches the script.
press the windows key
type cmd
right click the result and choose "run as administrator"
assoc .foo=foofile
ftype foofile="C:\Users\<user>\AppData\Local\Programs\Python\PYTHON~1\python.exe" "C:\<whatever>\fooOpener.py" "%1" %*
Use pythonw.exe if it's a .pyw file (to prevent a cmd window from spawning).
If you want to use an existing file type, you can find its alias by not assigning anything. For example, assoc .txt returns .txt=txtfile.
instead of editing registry, you can create batch file
opener.cmd
with content
python "C:\<whatever>\fooOpener.py" %1
and associate extension with this file.
It works!

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.

How to set the working directory of WinGHCi

I just installed WinGHCi. When I try to load an .hs file with Ctrl+L, it opens the file browser in C:\Program Files (x86)\Haskell Platform\2012.2.0.0\winghci which is where I installed this. I don't want it to go there by default, I keep my code files in a different directory on a different drive and I don't want to have to navigate to D:\MyPath\Haskell every time I load a file.
I tried to set the "Start in" field of the shortcut I use to launch WinGHCi but it changed nothing.
How can I make WinGHCi look in my own directory by default?
It opens in the last directory you opened a .hs or .lhs from.
Try opening something from the folder you want, exit, restart.
Did it restart where you were?
Try working like that for a while, and if you like it (I do) then you're fine.
If you really do want to go to the same place every time, perhaps there's a way.
I can't find any setting to control this, but the information is stored in the registy.
Mine is at
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Haskell\WinGHCi 1.0.6]
"WorkingDir"="D:\\Files\\Andrew\\prog\\haskell"
You can save your version of that as resetghci.reg:
To make your own, open regedit and first try the same location. If not, search for WinGHCi.
You can export that subtree but you get the whole lot including recent expressions etc, which I don't think you should reset. Save that as ghcioriginal.reg just in case. Edit away all the lines except WorkingDir and save as resetghci.reg
When you double-click it it will ask you if you're sure you want to add that info to the registry. Yes you are.
Untested:
What follows works on older versions, but Microsoft seem to be going off .pif files and the start command, and I'm not able to test this in Windows 7.
Make a text file called ghci.bat in the same folder as the .reg you made. In it put
#echo off
regedit resetghci.reg
start WinGHCi
You might need to specify the full path to WinGHCi.
The start command is the windows equivalent of making a background process from a shell prompt, so this should terminate immediately.
Make a shortcut to ghci.bat (it will be called ghci.pif) and set it to open minimised and put a nice lambda icon on it (nick it from the winghci executable).
Pop that on your desktop, start menu or shortcut bar, and when you use it, you'll be popped back to your standard location.

Dreamweaver command to set location of fileview

is there a way to locate a specific file in the fielview of director via a shell command?
I open a file (test.php) like this in dreamweaver, using an other application:
"C:\Program Files\Dreamweaver.exe c:\test.php"
But the fileview listing the local files does not jump to the opened file.
Maybe I can write a script and launch this script via shell to locate the file in the fileview?
Thanks for help or hints!
I assume that you're talking about having the file opened via the command line automatically become selected in the Files panel.
I am aware of no documented method of getting Dreamweaver to perform operations from the command line.
About the closest you're going to easily get to would be to assign a keyboard shortcut to Site -> Locate in Site via Edit (Dreamweaver on Mac?) -> Keyboard Shortcuts. I'm not aware of it, but there may be some way to send an a specific application keystrokes via the command line.

Is it possible to call an application selection window (Right click->Open With->Other) from the linux console?

On Gnome/KDE you can select in which application you want to open file (Right click on file -> Open With -> Other). Is it possible open file that way, but from console?
For example: you print " file.ext" and instead of opening in concrete application, there are that application selection window forced and then users chooses - starts selected program.
I tried to figure out that myself, but not found anything like that.
"edit file.ext" doesn't fits my needs, because it starts preferred application and you cannot choose which. And also on my desktop it says:
"Error: no "edit" mailcap rules found for type "image/jpeg"
So, am I able to forse that "open with" window from console? If yes, can you say how?
Both on windows and mac you can do such things.
//edit at 2009-02-10 14:17
Thank you very much for answers. Command will be used in program code, so unfortunately probably I would not be able to make some extra bash scripts.
For GNOME:
gnome-open <file>
For KDE:
kfmclient exec <file>
These commands should open up the <file> in the preferred application in GNOME or KDE respectively, although I don't have an installation of either to test on.
Take a look at man run-mailcap, you can change or add selected applications for each mimetype modifying the /etc/mailcap, ~/.mailcap files and some others.
Traditionally, on Unix systens (and therefore Linux, too), you start applications from the console (and not from a UI). The command line (or console) expects you to enter the name of the application and then the filename (plus some options).
This allows to use applications (or commands) in shell scripts.
On Windows, there is no real console (the DOS box is just a reminiscence of the dark ages of MS DOS). So the MS developers came up with the idea to have the OS treat anything as a command. If it's not a real command or application, the OS will determine the file type (by extension on Windows and by some header information on Mac). For each file type, there will be an associated application in a look up table.
This is why on Windows, it appears that you can enter the name of a file on the console and you will get the application to edit that file.
If you want a quick way to fix this in the Unix console, create a script called "open" or "o" and use the file command with the option --mime to identify the file type. You can then use a case statement to launch your favorite editor.
As for the error about "mailcap rules": There is a file called "mailcap" on Unix where you can define abstract "commands" (open, edit, view, print) for file types. See the mailcap man page.

Resources