Open text file and program shortcut in a Windows batch file

Viewed 335278

I have two files in the same folder that I'd like to run. One is a .txt file, and the other is the program shortcut to an .exe. I'd like to make a batch file in the same location to open the text file and the shortcut then close the batch file (but the text file and program remain open).

I tried this with no luck:

open "myfile.txt"
open "myshortcut.lnk"

Also didn't work:

start "myfile.txt"
start "myshortcut.lnk"
14 Answers

The command start [filename] opened the file in my default text editor.

This command also worked for opening a non-.txt file.

Its very simple, 1)Just go on directory where the file us stored 2)then enter command i.e. type filename.file_extention e.g type MyFile.tx

To open a file with default software just need to type the path of the file or, if you are at the file location, the file name.

C:\Users\MyName>C:\User\MyName\Desktop\hello.txt

or

C:\Users\MyName\Desktop>hello.txt

If you want specific program like notepad you can specify it first.

C:\Users\MyName>notepad C:\User\MyName\Desktop\hello.txt

or

C:\Users\MyName\Desktop>notepad hello.txt

Note that notepad is usually default text editor for .txt, in this case would make more sense to type notebook only to open a .cs/.cpp/.py file if your default for that files is any IDE and you just want to see the file on notebook

Regarding the batch file it will work the same way but to open them at the same time and let the command line go away you should use:

start "title" {filename}

So the command can open the file and return to next line immediately.

start "" C:\Users\MyName\MyFolder\foo.exe
start "" C:\Users\MyName\MyFolder\notes.txt

or

start "" foo.exe
start "" notes.txt

The last one only works if the batch file is on the same location of the files.

If you plan on using the console to open the batch file and you want the console to close at the end you should indeed write exit on last line.

When in doubt, it always helps to read the docs:

>help start


Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
    B           Start application without creating a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.
    I           The new environment will be the original environment passed
                to the cmd.exe and not the current environment.
    MIN         Start window minimized.
    MAX         Start window maximized.
    SEPARATE    Start 16-bit Windows program in separate memory space.
    SHARED      Start 16-bit Windows program in shared memory space.
    LOW         Start application in the IDLE priority class.
    NORMAL      Start application in the NORMAL priority class.
    HIGH        Start application in the HIGH priority class.
    REALTIME    Start application in the REALTIME priority class.
    ABOVENORMAL Start application in the ABOVENORMAL priority class.
    BELOWNORMAL Start application in the BELOWNORMAL priority class.
    NODE        Specifies the preferred Non-Uniform Memory Architecture (NUMA)
                node as a decimal integer.
    AFFINITY    Specifies the processor affinity mask as a hexadecimal number.

Picture for the visual learners: help start

Related