📜 ⬆️ ⬇️

Getting rid of the “historical reasons” in cmd.exe in an easier and standard way.

Hello! Today I want to share with you an easier and more correct, from my point of view, way to achieve a result similar to that described in the article by NikitaTrophimov .
I think it makes no sense to re-write the introductory part - the author of the original article did it before me - and, since my article would hardly have seen the light, if he did not write his own, and was just an alternative vision of how to solve the problem, I would focus on the main thing.

Like the author of the original article, I found the behavior of the “cd” (or “chdir”) command in cmd.exe rather strange. It seemed illogical that in order to change the active disk it is necessary to perform additional actions each time. Being a fan of the command line interface, I have repeatedly tried to improve the appearance and capabilities of the standard Microsoft Windows command interpreter, but I did not even think about the possibility of changing the standard behavior of the “cd” command. After reading the above article on Habré, I was haunted by the feeling of excessive non-standard and complexity of the described method ... and the desire to create my own bike and quickly start using it led to a handful of a couple or three hours spent in the next weekend debriefing. So, who are interested in my research - welcome under the cut!

A small educational program on the topic (you can not read)
The standard mechanism for changing the behavior of commands / programs "by default" (which is true for all operating systems known to me) is the launch arguments, the parameters specified a space after the name of the command or executable file. This mechanism allows changing the behavior of the command for changing the current directory in cmd.exe to the one we need. The "/ d" parameter specified when invoking the "cd" (or "chdir") command allows you to change the active disk simultaneously with the change of the current directory.

Another standard mechanism used for setting the parameters of the environment "by itself" is the alias mechanism. Well-known and often used in the Unix environment of similar operating systems, it often turns out to be deprived of attention and even undeservedly forgotten in Microsoft Windows. This mechanism allows you to assign your own call names for commands, programs, command / program pairs + parameters and, most importantly, allows you to change the behavior of standard commands and programs, because aliases have a higher priority than shell commands and programs found by the shell in the directories specified in the PATH environment variable.
')
This is what we will use. Let's get started!

Task setting: The “cd” command should automatically change the active disk if the path to the data on a disk other than the current disk is passed as an argument.

Solution: It would seem that something easier? Using the “doskey” we create the alias “cd = cd / d $ *” and enjoy life ... but what is the problem - the standard behavior of the “cd” command breaks when started without parameters. Launched without parameters, this command is a complete analogue of the “pwd” command from Unix shells of similar operating systems and should output information about the current directory to the console, but after setting the alias, it will already be executed with the “/ d” key, the presence of which implies The next parameter is the path to go. If it is not detected, the command will display an error message to the console and exit. This decision clearly does not suit us!

After some amount of reflection, thoughtful browsing of Habr's pages and active Googling, the next solution was born - why not write a .bat file? Wait, do not rush to shower me with tomatoes, I myself do not like crutches. The decision was well thought out and, as you will see, have a chance to be a worker.

My idea was as follows - I decided to write a .bat file that takes arguments and runs the standard “cd” command with the arguments passed to it. If there are no launch arguments for the file, it runs the “cd” command with no arguments (output mode of the current directory), if there are any arguments, it adds the “/ d” key in front of them and executes the “cd” command with the / d key and arguments passed to him. Those. When using this solution, we get what we want — the standard behavior of the “cd” command with no arguments works and the automatic disk change works.

@echo off if "%*" == "" ( cd ) else ( cd /d "%*" ) exit /b 

So, we have already received a working solution, but this is not what we need. It remains to replace the standard command for changing directories, and the alias mechanism will help us in this. Save the resulting .bat file to one of the directories described in the “PATH” variable (for example, in% WINDIR% \ System32) with the name cd.bat and create an alias with the command “doskey cd = cd.bat $ *”. Everything would be fine, but the alias will remain only until the current cmd.exe window is closed ... it is necessary to correct the situation! Create another .bat file to automatically set environment variables when launching cmd.exe windows - cmd_env.bat, which is also saved in the selected directory next to cd.bat

 @echo off doskey /macrofile="%USERPROFILE%\aliases" exit /b 

You also need to create a simple text file "aliases" and place it in% USERPROFILE%

cd="%WINDIR%\System32\cd.bat" $*



and .reg file to activate automatic launch cmd_env.bat when opening cmd.exe

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="C:\\Windows\\System32\\cmd_env.bat"



Save it, for example, with the name cmd_env_reg.reg

We import the parameters from the cmd_env_reg.reg file into the registry and run cmd.exe in any way possible. Done! Check the result!

This solution was successfully tested for performance in the Windows 2008 R2, 7, 2003 R2, XP environment.

PS: Many thanks to the author of the original article for pushing me into action. Without his article, I would have endured inconvenience for a long time and dreamed that "if there is time, I will work."

PPS: The cmd_env.bat file can and should be used to set up cmd.exe — for example, to set standard colors in the console with the “color 02” command or to set environment variables.

PPPS: Links for those interested in the topic - ConEmu , Clink , cmder and others - Console2 and its fork ConsoleZ , rCons , WinQConsole

Source: https://habr.com/ru/post/263105/


All Articles