1

I'm trying to implement a slight modification to the code provided by @Argyll in this SO answer, specifically changing:

DOSKEY alias=notepad %USERPROFILE%\Dropbox\alias.cmd

to:

doskey alias=echo doskey $*>>%0

But the redirection fails when I use this new alias, and I simply get doskey <whatever I typed after alias> echo'ed to the console as per usual :-(

As you can probably tell, I'm a batch script noob, so what am I doing wrong here? MTIA :-)

Kenny83
  • 769
  • 12
  • 38
  • 2
    Proper escape it with carets `doskey alias=echo doskey $*^>^>%%0` or `doskey alias=echo doskey $*^^^>^^^>%%0`, depends on what you want – jeb Sep 02 '21 at 07:06
  • Thank you kind sir! Please ignore my previous comment if you saw it before deletion; the only part of what you wrote that doesn't work is the `%%0`, which I find quite strange, but meh I guess it's not hard to type ~20 extra characters once ;) If you could add some more background information to a proper answer re: why/how it works, and the `%%0` issue, I'd gladly mark it as the accepted answer. Cheers! :D – Kenny83 Sep 02 '21 at 09:18

1 Answers1

1

You need to escape the redirection, else the redirection is active in the moment where you call doskey alias=echo doskey $*>>%0, it appends the output of the doskey command (which is always empty in this case) to the current batch file.

But if you modify your line to

doskey alias=echo doskey $*^>^> "%~f0"

This creates a macro named alias like

alias=echo doskey $* >> C:\myfull\path\to\myBatch.bat

I use %~f0 for the full path, else the macro could be defined like

alias=echo doskey $* >> myBatch.bat

That will obviously fail, if you're not in the correct directory

jeb
  • 78,592
  • 17
  • 171
  • 225