2

Yes, it's 2021 and I'm still using the CMD shell and batch scripts for simple tasks. I just recently tried to enhance my workflow by auto-running my own init-script for (each) CMD prompt session, which also includes setting a bunch of DOSKEY macros.

So this is a similar setup to what others have done. [1]

After having a weird issue with a new script, I came to realize, that DOSKEY macros can break interactive prompts in a batch script using the "SET /P"-command, when executed from such a modified shell with active macros.

Try something like this (enter text shown between [] at prompt + press <enter>):

@ECHO OFF
DOSKEY print=ECHO $*
SET /P var=Enter [print Hallo]:
ECHO var: %var%
EXIT /B 0

You will see, that the prompt variable holds the command prompt created by the DOSKEY macro ('ECHO Hallo').

Normally DOSKEY is not supposed to work/be available from within a batch file [2][3], as it only works on interactive prompts [4]. But "SET /P" seems to count as an interactive shell so DOSKEY replaces everything entered, that begins with a defined macro keyword.

Well, this seems like a bug to me (DOSKEY is now 30 years old btw [6]), but I couldn't find anything on the web about this issue. So, I was thinking how to workaround this. As macros cannot be easily deactivated/removed [5], it would be cumbersome to fix this from within any batch file, that does prompts.

My init-script also only runs for interactive sessions, so it isn't a problem, when you run a batch-script directly (on its own).

Tested on a machine with 'Windows 7 Home Premium SP1'.

Sources:

  1. https://gist.github.com/steve-jansen/5118149
  2. https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/doskey
  3. https://ss64.com/nt/doskey.html
  4. https://www.dostips.com/forum/viewtopic.php?t=1937#p8379
  5. How to remove a doskey macro?
  6. https://en.wikipedia.org/wiki/DOSKEY
e.d.n.a
  • 191
  • 1
  • 6
  • 1
    I could not replicate the output you've stated, either via a batch file or command line. – T3RR0R Jan 23 '21 at 07:48
  • OK, on what Windows version did you test this and did you exactly type in "print Hello", without leading spaces etc.? I haven't tested it with Win10 myself yet. It could be a fixed issue in newer Windows versions! – e.d.n.a Jan 23 '21 at 11:51
  • You could also test, if DOSKEY is active during a "SET /P"-prompt by testing a DOSKEY-shortcut to browse the history, like F7 or F8. – e.d.n.a Jan 23 '21 at 12:09
  • 1
    I tested on Windows Version 10.0.18363.657 – T3RR0R Jan 23 '21 at 12:13
  • @T3RR0R Did you *TYPE* manually the text: `print hello` at the prompt? Then it should work(show the problem) – jeb Jan 23 '21 at 14:39
  • I've posted an answer showing the failure to replicate – T3RR0R Jan 23 '21 at 16:19
  • 1
    I understand what you're meaning now. The issue does not arise when the macro is used as part of the prompt sting, rather when it is entered as prompt input, which in my view could be worded a bit clearer in the question. – T3RR0R Jan 23 '21 at 16:50
  • I updated the question to make the test-case more clear! – e.d.n.a Jan 24 '21 at 01:06
  • I mean, it is also helpful having doskey active at those input prompts to have the command history (including previously entered texts) available for repeated prompts! – e.d.n.a Jan 24 '21 at 01:22
  • IMHO, your second line shows a misunderstanding of macros. They don't handle arguments. You may say `DOSKEY print=ECHO` only - the $* handling is, what echo does anyway, but maybe this contributes to the error you get. – user unknown Jun 28 '22 at 21:28

2 Answers2

1

I cannot see a perfect solution, but as DOSKEY is for "interactive shells" the simplest solution is also interactive:

Just clear all macros manually via ALT+F10 [2][3] before running any batch, that uses prompting.

You can also backup and restore the current set of defined macros, e.g. if you have added some during the session.

DOSKEY /macros >%TEMP%\doskey-macros.txt
[Pressing ALT+F10]
batch_that_does_prompts.cmd
DOSKEY /macrofile=%TEMP%\doskey-macros.txt
e.d.n.a
  • 191
  • 1
  • 6
0

You can use another window for the set /p, there is no doskey loaded:

@echo off
FOR /F "tokens=3 delims=:" %%# in ("%~0") do goto :%%#

doskey x=XXXXXXXXXXX

echo Test1: Shows the problem
call :subTest

echo Test2: No problem
start "" /wait cmd /c %~d0\:subTest:\..\%~pnx0 pause
echo end
exit /b

:subTest
set "var=empty"
set /p var=[Please enter a single 'x' and press enter]:
set var
%~1
exit /b

Or you "unload" all macros

@echo off
doskey x=XXXXXXXXXXX

echo Test1: Shows the problem
call :subTest

echo Test2: No problem
REM Save macros
DOSKEY /macros >%TEMP%\doskey-macros.txt
call :unload_macros
call :subTest

REM Restore macros
DOSKEY /macrofile=%TEMP%\doskey-macros.txt

echo end
exit /b

:unload_macros
for /F "delims==" %%M in ('doskey /macros') do (
    doskey %%M=
)
exit /b
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thank you for sharing these possible workarounds, although one can clearly see here how complicated it is to circumvent the issue from within the batch file, as I said. So I actually don't like these options for a general solution to the problem. The second one would be at least the much more favorable one though, imho. – e.d.n.a Jan 24 '21 at 01:15