China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 04:04
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » How can I assign the output of a command to a variable? Thanks View 974 Replies 4
Original Poster Posted 2003-08-24 00:00 ·  中国 北京 联通
初级用户
Credits 108
Posts 2
Joined 2003-08-24 00:00
22-year member
UID 9137
Gender Male
Status Offline
I wrote a batch file. The original idea was to display today's date in YYYYMMDD format, but instead it shows type date.txt,

My bat file is as follows:
@call date /t | awk95 -F- "{ print($1 $2 substr($3,1,2)) }">date.txt
@set filename=type date.txt
echo %filename%

Thanks, many thanks
Floor 2 Posted 2003-08-30 00:00 ·  中国 湖北 武汉 联通
银牌会员
★★★
Credits 1,681
Posts 512
Joined 2003-08-02 00:00
22-year member
UID 7953
Gender Male
Status Offline
If you want to display it on the screen, then use type date.txt...
If you want to assign it to a variable... you may need to write a small program in QB
Somehow somewhere I've got to choose.
No matter if it is win or lose.
Floor 3 Posted 2003-08-30 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re msg:

First write a file, you can name it setfile.tmp, with the content “set filename=”. Note that the file must not contain a carriage return character, so you can't use edit to edit it, or if you use edit, then use debug to delete the last two bytes afterward.

Next, change the line @set filename=type date.txt in the program to

@copy setfile.tmp+date.txt setfile.bat >nul
@call setfile.bat
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2003-09-05 00:00 ·  中国 湖北 武汉 联通
银牌会员
★★★
Credits 1,681
Posts 512
Joined 2003-08-02 00:00
22-year member
UID 7953
Gender Male
Status Offline
Hehe, it's best to use copy /b, otherwise problems are very likely to occur (especially when using EDIT to write setfile.tmp)
Somehow somewhere I've got to choose.
No matter if it is win or lose.
Floor 5 Posted 2003-10-31 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re iceboy:

I don't see what effect /b has on this problem. I only know that it is just meant to avoid mistakenly interpreting and executing certain characters in a binary file according to their uses in the ASCII table.

And in my solution, if the temporary file carries a carriage return character, then whether /b is used or not, the correct result will not be obtained.

Also, isn't copy's default parameter /b rather than /a?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: