Redirecting STDERR and STDOUT to a DOSKEY macro

I am trying to define a DOSKEY macro that redirects the output of STDOUT and STDERR to a file. Doskey will be set by running the batch file. I can do this for STDOUT with only the following:

doskey logged_build=build $g build.log 

However, I cannot use the typical 2> & 1 option (as shown below) to redirect the output of STDERR to the same file as well.

 doskey logged_build=build $g build.log 2>&1 

I also tried

 doskey logged_build=build $g build.log 2$g&1 

which gives a syntax error and

 doskey logged_build=build $g build.log 2$g build.log 

which gives an error to which the file is unavailable because it is being used by another process.

I'm sure this is just a question using the correct macro (for example, using $ g instead of>), but I did a lot of search queries and still could not find anything. So I ask you a question, my friend SOFers.

+4
source share
2 answers

I also trolled a web solution with no luck. The best I could come up with is adding 2> & 1 to your macro call:

 doskey logged_build=build $* $g build.log 

Then call it as such:

 logged_build 2>&1 
+1
source

You cannot do this directly on the command line, but you can do this using the doskey macro. Macrofiles don't even need to use confusing magic, like $g ; they are not part of the shell, so special shell characters can usually be used and included in the definition of the macro instead of interpreting the shell before the macro is defined.

Create the file where you want (for example, %USERPROFILE%\mymacros.txt ) and paste the following line into it:

 logged_build=build >build.log 2>&1 

Then load the macros by running:

 doskey /MACROFILE=%USERPROFILE%\mymacros.txt 

You can put many macros in a file to load them at the same time; this simplifies the configuration of the command line as a whole; you can either modify the existing Command Prompt shortcut or create a new shortcut based on cmd.exe to make Target :

 %windir%\system32\cmd.exe /K doskey /MACROFILE=%USERPROFILE%\mymacros.txt 

and clicking the shortcut will create a command line with all the preloaded macros. The /K cmd.exe for cmd.exe runs the following command in the shell before giving the user an interactive prompt. It saves a lot of trouble if your tooltips automatically detect all your macros without having to install them every time.

Alternatively, to avoid having to change individual shortcuts, you can set a registry key that will load macros regardless of whether cmd.exe is called directly without going through the changed shortcut. Just run:

 reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "doskey /MACROFILE=%USERPROFILE%\mymacros.txt" 

You can change the HKCU to HKLM so that it is used globally for all users, not just for yourself, although in this case you want to put the macro file in a common place, and not in your user profile. Annoyingly, you cannot use REG_EXPAND_SZ for such cases (which will allow you to use variables like %USERPROFILE% to set the global HKLM parameter for files in relation to each directory of the user profile or handle the case the profile moves), but it works quite well.

+1
source

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


All Articles