Open a file via command shell in a friendly fashion

Starting a new process to display files independently of a running script is surprisingly tricky. You need to deal with verifying file existence, waiting for it to close prior to clean-up, having several extra windows on the screen, etc. Here’s a method proven to be robust, that uses a combination of double-quotes and double-quote environment variables. It is somewhat of a compromise because it uses a minimized command prompt until the opened file is closed. A breakdown of the command follows.

Example 1:

SET DQ=”

START /MIN cmd.exe /V:ON /c “Title Waiting for correlated_data.csv to close…&&%%DQ%%V:\Flying Probe\TESTDATA\correlated_data.csv%%DQ%%”

Example 2:

SET DQ=”

START /MIN cmd.exe /c “Title Waiting for correlated_data.log to close…&&%%DQ%%V:\Flying Probe\TESTDATA\correlated_data.log%%DQ%%”

SET DQ=” creates an environment variable that is later expanded into a double-quote (by a new command shell).

START /MIN cmd.exe starts a new, minimized command shell.

/V:ON delays the expansion of environment variables. I have included two examples, one with and one without. Interestingly in hindsight, it doesn’t seem to matter.

/c closes the command shell after the command completes.

“[yadda&&yadda]”, between the double-quote characters, is the command (actually, two) executed by the new shell.

Title Yadda Yadda... is the text displayed in the title bar of the new shell’s window, and also upon the title bar.

&& separates the two commands to be executed by the new shell.

%%DQ%% is expanded by the new command shell to be a double-quote character (“).

%%DQ%%path\filename%%DQ%% is the filename to open (with the default application), surrounded by double-quotes. (The double-quotes permit spaces in the filename.)

This entry was posted in Cmd scripting, Design and UX, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.