I am trying to define a DOSKEY macro that redirects both STDOUT and STDERR output to a file. The doskey will be set by running a batch file. I can do this for STDOUT only with the following:
doskey logged_build=build $g build.log
However, I cannot use the typical 2>&1 option (as below) to also redirect the STDERR output to the same file.
doskey logged_build=build $g build.log 2>&1
I have 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 that the file cannot be accessed because it is being used by another process.
I am sure this is just a matter using the right macro wildcard (like using $g instead of >), but I have done loads of googling and have not yet been able to find anything. So I pose the question to you my fellow SOFers.
You can't do this directly at the command line, but you can do it using a doskey
macrofile. Macrofiles don't even need to use confusing magic like $g
; they're not part of the shell, so shell special characters can be used normally, and get included in the definition of the macro instead of being interpreted by the shell before the macro gets defined.
Create a file wherever you like (e.g. %USERPROFILE%\mymacros.txt
), and put the following line in 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 the file to load them all at once too; this makes it easy to customize your command prompt in general too; you can either modify the existing Command Prompt
shortcut or create a new shortcut based on cmd.exe
to make the Target
:
%windir%\system32\cmd.exe /K doskey /MACROFILE=%USERPROFILE%\mymacros.txt
and clicking the shortcut will create a command prompt with all of the macros pre-loaded. The /K
option to cmd.exe
runs the subsequent command in the shell before giving the user an interactive prompt. Saves a lot of hassle if your prompts define all your macros automatically without having to set them up each time.
Alternatively, to avoid needing to modify individual shortcuts, you can set a registry key that will load the macros no matter what, even if cmd.exe
is invoked directly, without going through a modified shortcut. Just run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "doskey /MACROFILE=%USERPROFILE%\mymacros.txt"
You can change HKCU
to HKLM
to make it apply globally for all users, not just yourself, though in that case you'd want to put the macro file in a common location, not your user profile. Annoyingly, you can't use a REG_EXPAND_SZ
for cases like this (which would allow you to use variables like %USERPROFILE%
to set a global HKLM
setting to files relative to each user's profile directory, or handle the case of a profile being relocated), but it works well enough.
I too have trolled the web for a solution with no luck. The best I could come up requires adding the 2>&1 to the invocation of your macro:
doskey logged_build=build $* $g build.log
Then invoke it as such:
logged_build 2>&1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With