Create shortcut for complex commands in CMD

Asked

Viewed 1,819 times

2

I use the command taskkill /f /im nome.exe to stop lawsuits, because the same for all processes with the same name.

I’d like to raid that command for something like tk nome.exe.

I tried to create an environment variable, it even worked, but I have that type like that:

%tk% nome.exe

How can I create this command shortcut?

  • 2

    Doesn’t it pay to just create a link in your path, or even a bat to stay permanent? something like Tk.bat or Tk.cmd with @echo off n taskkill /f /im %1 content - I find it more practical than using a Doskey and having to make startup script to persist

  • 2

    you can create a batch file that takes its name as a parameter and execute the command

3 answers

7


Let me give a very simple example, which will not depend on any external programme:

  1. Create a folder in a specific place, for example C:\meus_comandos
  2. Go to your computer and right-click (mouse) and then select properties:

    pc propriedades

  3. Click Advanced System Settings:

    configurações avançadas

  4. Click on environment variables

    inserir a descrição da imagem aqui

  5. Select PATH and click Edit...

    inserir a descrição da imagem aqui

  6. Click on new:

    inserir a descrição da imagem aqui

Will be released the option to type, so write:

C:\meus_comandos

Creating the commands:

Now in the folder meus_comandos you can create how many files .bat want and they will be recognized as commands, create in the folder a file with the name tk.bat and add this content:

@echo off

taskkill /f /im %1

Then open cmd in any folder and type this:

tk nome.exe
  • 1

    I didn’t know you could do it this way, thank you Guilherme !

2


inserir a descrição da imagem aqui

The variable Path has character length limitations...

1) Because it is a Bat that does not delete/remove any file or folder, you can add this Bat in the folder of Windows, thus taking advantage that this folder already has its complete path saved in the variable Path, and yet, no additional action/configuration will be required for this to work, also without bumping into any limitation problem, due to errors reported on the variable Path, (commented/lynched on item 2).

  • For this, it is necessary to execute the cmd.exe as administrador:


inserir a descrição da imagem aqui


The command to create the Bat to that end, it would be:

echo/@echo off ^& taskkill /f /im "%~1" %2>"%windir%\tk.cmd"

After this done, just type in any drive:\pasta o comando:

  • tk chrome.exe /t

  • tk firefox.exe

  • tk qualquer.exe


Note: 1) The parameter/argument %2 may or may not be used, but in itself, this informs the taskkill.exe to finalize also some child process created/started by the executable to be finished. There are times it is useful when some plugin "Buga/latch" when finishing the browsed (example).

Note: 2) Many software developers/vendors/drives/components, such as Nvidia, also use the folder Windows to keep some Bats, and run them when necessary without adding/adding more folders/paths to the environment variable `Path.


inserir a descrição da imagem aqui


  • inserir a descrição da imagem aqui

2) When using a specific folder to maintain and run one or more bats, it will be necessary to add the path of that folder in the global variable Path.

Obs.: It is possible to use the instructions present in the operator’s response @Guilherme Nascimento, or use the command SetX to configure this value, so that it will be possible to view in the execution/output, any occurrence of error/warning: setx /M Path "%Path%;c:\Pasta_BATs"

  • For this, it is also necessary to execute the cmd.exe as administrador:


Consider

Just to make it possible aviso/erro in executing the command, as there are reported problems by users, due to a limit on character length of the values assigned to the variable Path.

  • Posts commenting/addressing the limiting:

StackOverFlow Environment Variable is Too large on Windows 10 # 38632 Views

SuperUser Why does Windows have a limit on Environment variables at all? # 13215 Views

YouTube Solved - Environment or User PATH Variable length limitation # 643 Hits

Intel Limitation to the length of the System PATH variable # No informs Visits


  • When executing the command SetX, I received the warning:

  • "data is truncated to 1024 characters when saved."


inserir a descrição da imagem aqui


The character length in my variable Path was 1412 characters before adding/editing the variable, and the number of folders present in it were 32.

After adding the folder c:\Pasta_BATs, the character count dropped to 1185, and the number of folders went to 28?.


inserir a descrição da imagem aqui


So we have a divergence of information from the graphical interface and on the command line, which one is most accurate?

The values of the variable Path in the console/command line environment, these are (full) folder paths, divided by the delimiter ;.

The values of the variable Path on the interface of Advanced System Settings, are displayed in a path and folder listing, one item per line, no delimiter.


Completion

  • Lost some folders after adding value c:\Pasta_BATs in the variable Path, so I considered using the Windows for the Bats my Bats, it is simpler, as also do some component manufacturers/drivers/softers/etc..

  • For those who prefer to use this variable, adding more paths to the variable Path, recommend watching the link video Youtube, remembering that there is comment in the passage citing not being possible/achievable in the Windows 10 the steps shown in the post, given the fact that, the display in the graphical interface is sectioned in a listing, is not a display of the complete content with a delimiter, only individual value listing.


2

You can create an alias for your command using DOSKEY

Just create a file in any directory, for example:

C: Users Tommelo Documents macros.Doskey

print=echo $*

Then just add a regkey to make the alias available every time you’re using cmd:

reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"C:\Users\TomMelo\Documents\macros.doskey\"" /f

Close cmd and open a new session, your alias should already be available. For more options on parameters take a look here also.

  • Interesting Tom, thank you for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.