I have opened a file through on Windows command prompt, just by navigating to that directory and then typing it's name. Now is there a way to close that (open) file through the command prompt? I've been searching around and I can't find out how. Seems to be a pretty simple command to not exist.
-
3What do you mean by "open" exactly? Can you show some code? – Pekka Oct 12 '15 at 19:43
-
Have I gone through a time portal? – Ed Heal Oct 12 '15 at 19:44
-
1Opening a file this way means you're handing it over to an application that is registered to handle that kind of file, so it's no longer up to Command Prompt to end it. You'd have to record the process ID of the spawned process handling that file then kill the process to "close" it, but this would be far from recommended. This doesn't look pretty though: http://stackoverflow.com/questions/1807794/how-to-capture-the-pid-of-a-process-when-launching-it-in-dos – Adambean Oct 12 '15 at 20:18
1 Answers
There is no MS-DOS in Windows and has not been since about 2003. The "Command Prompt" program (named cmd.exe
) runs in a console window and looks and behaves like the command interpreter of MS-DOS, enough like it to seem the same to many people, although there are some small differences.
If you give a filename as a command to a command prompt and that named file is not a program, it looks at the extension to try to select an appropriate program to run and open the file. The .doc
extension usually corresponds to MS Word (Office) or the stripped-down version MS Works, but depending on what software you have installed and how it is configured .doc
could be something else. You can find out what it is on your system by the command-prompt commands assoc .doc
and usually then ftype (value from assoc)
or by looking in the registry under HKEY_CLASSES_ROOT
. Once the program runs (a running program is also called a process) it may keep the file open while running (like Word) or only open it briefly and then close it (like notepad).
Once you know what program/process is running, you can direct that process to stop with the taskkill
command. Type taskkill /?
for help information. If there is more than one process running the same program and you use the /im
option, it will stop all of them, not just the one you want. To use the /pid
option, tasklist
or TaskManager can help you find the right process -- but if you use TaskManager it can also stop the process, so taskkill
is unnecessary. If the program malfunctions and doesn't stop when directed, taskkill /f
will force it, but this may leave the open file(s) with damaged and incorrect or unusable data.
Also note that command
itself effectively keeps the directory open. If you are trying to rename or move a subtree of files including that directory, you must first either cd
the command
process somewhere else, or terminate it with exit
.

- 34,712
- 6
- 50
- 70