Secozzi
Secozzi

Reputation: 262

Modify existing cmd commands

I want to modify existing cmd commands, both to change out the code to another batch file and to modify the code.

For example, when I type "calc" it opens the calculator app but I want it to open a batch script I made, and when I want to edit the "help" screen. How can I modify the code that runs when I type in existing cmd commands such as tree, help and calc?

Upvotes: 0

Views: 1335

Answers (1)

Gerhard Diedericks
Gerhard Diedericks

Reputation: 51

You may not be able to replace or modify all exiting programs and/or scripts, but you can "trick" your Windows to prioritize / execute yours that are similarly named.

When you type "calc" in the command prompt, you will effectively execute the file:

C:\Windows\System32\calc.exe

enter image description here

You need to look at the Environmental Variable named "Path"; this is where you identify which directories should be seen by your system as "global". Any EXE, BAT or CMD you'll try to execute will look at the current directory first; and if it isn't found it will try to find it in the directories listed in "Path".

For example. Lets say you want to run your own file called help.exe; a console app that you created.

  1. Create a folder e.g. C:\CustomBatchFiles.
  2. Go to your computer's Advanced System Settings.
  3. Go to Environmental Variables

You should see the following screen: enter image description here

  1. Under "System Variables", find the one called "Path", then "Edit".
  2. Add the folder in which your custom scripts / batch files reside. Please be aware that new entries will be added to the bottom - and this list dictates the "priority" - so, once created, push the "Move Up" button until your directory is at the very top.

enter image description here

  1. Click OK, and OK again to apply this new change. You might have to reboot your system; or at the very least re - open your command prompt.
  2. From there you should be able to execute your commands no matter which directory is currently open, including "used" ones such as calc and help.

Here's a tiny program I wrote. I compiled it as help.exe, and copied it to the directory that I added as a Path (C:\CustomBatchFiles):

    class Help
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start executing your program / script from here.");
        }
    }

It will produce the following output:

enter image description here

Upvotes: 1

Related Questions