1

Executable files like findstr.exe are placed in the \system32 folder and can be accessed anywhere from the command prompt. However doing this with one of my batch files only works in certain CDs. How can I turn my bat file into a command on Windows 10?

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
Mark Deven
  • 1,488
  • 9
  • 40
  • 71
  • 1
    The operating system uses a few different settings to determine where to find files when you don’t specify the full path. One you might be interested in is the PATH environment variable. Folders listed there are searched whenever you type in a file name without a path. If your batch file exists in one of those folders it will be found. System32 is a special folder. Depending on if you are running a 32-bit or 64-Bit program your file may or may not be found due to folder virtualization. Don’t use system32 for your own files. – Appleoddity Sep 02 '18 at 02:50
  • 1
    In addition to changing the path variable, consider making a context menu entry to "access it anywhere" – jiggunjer Sep 02 '18 at 16:58

1 Answers1

1

A scalable and simple way to make a batch script accessible like an implicit command

  1. Create a new folder on the system where you want the scripts to be accessible from "anywhere" from the command prompt, and then set this folder in the PATH environment variable (e.g. C:\LinkScripts).

  2. Use MKLink to link the actual scripts in others locations, and create a symbolic file link to each script to the new folder you created and added to the PATH environment variable (see How do I set or change the PATH system variable?).

  3. Open a new instance of command prompt once PATH adjustments are made and now when you type in the name of the script and press enter, it will run that batch file logic of the batch file(s) you linked with MKLink.


Batch Examples

md C:\LinkScripts
mklink C:\LinkScripts\test.cmd C:\Actual\Script\Folder\test.cmd
mklink C:\LinkScripts\pest.cmd C:\Colder\Script\Bolder\pest.cmd
mklink C:\LinkScripts\zest.cmd C:\Different\Script\Shoulder\zest.cmd

Results

Now you can open a new command prompt and run zest, test or pest with or without the appended .cmd (or .bat) file extension and it will run whatever logic is in each. This will allow you to easily be able to run a simple command for each or any newly added scripts which you need to use in this manner.


Further Resources

  • How do I set or change the PATH system variable?

    Windows 10 and Windows 8

    1. In Search, search for and then select: System (Control Panel)
    2. Click the Advanced system settings link.
    3. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
    4. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
    5. Reopen Command prompt window, and run your java code.

    Windows 7

    1. From the desktop, right click the Computer icon.

    2. Choose Properties from the context menu.

    3. Click the Advanced system settings link.

    4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.

    5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

    6. Reopen Command prompt window, and run your java code.

    source

  • MKLink

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
  • can I just put the actual file in the path folder instead of using MKlink? – Mark Deven Sep 02 '18 at 21:33
  • @MarkDodsons I don't think that will work as the `PATH` environmental variable is for "paths" and not files but you could add the actual "path" the scripts reside in to the `PATH`, and then it should work just the same and not require the MKLink. The MKLink solution is just to help standardize and have the option to add any script in any folder to it and since it's already part of the `PATH` then the script file symlinks within that folder work as you need. – Vomit IT - Chunky Mess Style Sep 02 '18 at 22:42