MS-DOS how to get output of command as variable

♀尐吖头ヾ 提交于 2019-12-31 03:16:10

问题


I've written a program that returns keycodes as integers for DOS but i don't know how to get it's output as a variable.

Note: I'm using MS-DOS 7 / Windows 98, so i can't use FOR /F or SET /P

Does anyone know how i could do that?


回答1:


A few solutions are described by Eric Pement here. However, for older versions of cmd the author was forced to use external tools.

For example, program tools like STRINGS by Douglas Boling, allows for following code:

echo Greetings! | STRINGS hi=ASK # puts "Greetings!" into %hi%

Same goes for ASET by Richard Breuer:

echo Greetings! | ASET hi=line # puts "Greetings!" into %hi%

One of alternative pure DOS solutions needs the program output to be redirected to the file (named ANSWER.DAT in example below) and then uses a specially prepared batch file. To cite the aforementioned page:

[I]n the batch file we need to be able to issue the command set MYVAR={the contents of ANSWER.DAT go here}. This is a difficult task, since MS-DOS doesn't offer an easy way to prepend "set MYVAR=" to a file [...]

Normal DOS text files and batch files end all lines with two consecutive bytes: a carriage return (Ctrl-M, hex 0D, or ASCII 13) and a linefeed (Ctrl-J, hex 0A or ASCII 10). In the batch file, you must be able to embed a Ctrl-J in the middle of a line.

Many text editors have a way to do this: via a Ctrl-P followed by Ctrl-J (DOS EDIT with Win95/98, VDE), via a Ctrl-Q prefix (Emacs, PFE), via direct entry with ALT and the numeric keypad (QEdit, Multi-Edit), or via a designated function key (Boxer). Other editors absolutely will not support this (Notepad, Editpad, EDIT from MS-DOS 6.22 or earlier; VIM can insert a linefeed only in binary mode, but not in its normal text mode).

If you can do it, your batch file might look like this:

@echo off
:: assume that the datafile exists already in ANSWER.DAT
echo set myvar=^J | find "set" >PREFIX.DAT
copy PREFIX.DAT+ANSWER.DAT  VARIAB.BAT
call VARIAB.BAT
echo Success! The value of myvar is: [%myvar%].
:: erase temp files ...
for %%f in (PREFIX.DAT ANSWER.DAT VARIAB.BAT) do del %%f >NUL

Where you see the ^J on line 3 above, the linefeed should be embedded at that point. Your editor may display it as a square box with an embedded circle.



来源:https://stackoverflow.com/questions/41782178/ms-dos-how-to-get-output-of-command-as-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!