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-30 19:04
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » How to create a TXT file in DOS batch and write the current date View 3,636 Replies 9
Original Poster Posted 2006-10-28 03:19 ·  中国 广东 深圳 宝安区 电信
新手上路
Credits 4
Posts 1
Joined 2006-10-27 03:52
19-year member
UID 68442
Gender Male
Status Offline
请问如何在DOS批处理中创建一个TXT文件(如today.txt),并写入当前的日期为此文件的内容?另又如何可以在批处理中读取这个文件的内容,并把它赋予一个变量?谢谢

How to create a TXT file (such as today.txt) in a DOS batch and write the current date as the content of this file? And how can we read the content of this file in the batch and assign it to a variable? Thanks
Floor 2 Posted 2006-10-28 05:27 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
You can directly assign the time to a variable, such as: set b=%date%
Test the content of b to be usable: echo %b%
It will display: 2006-10-27 Friday

In addition, the command to create and write the current date to a txt: echo %date%>today.txt
To read it, use for: for /f "tokens=1 delims=\" %%i in (today.txt) do @set d=%%i
Floor 3 Posted 2006-10-28 05:31 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
If it's pure DOS, it's not easy to handle

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 4 Posted 2006-10-28 08:33 ·  中国 江苏 南京 电信
高级用户
★★
Credits 525
Posts 222
Joined 2006-08-28 21:07
19-year member
UID 61476
Status Offline
get seems to be able to obtain files and the current date. But when I tested get in the virtual machine, there was an error in obtaining the disk capacity.
Oh, I've been really stressed out lately!

There's also isdate which can compare the current date and time with the file date. Just search for it yourself.
Both are for DOS, and Windows doesn't seem to be able to use them.
Floor 5 Posted 2006-10-28 08:55 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

The several functions mentioned by the user above can actually be completed with CMD-BAT, but the code is relatively longer. It is best not to use third-party tools to solve problems. If we blindly rely on third-party tools to solve problems, then some things will be meaningless at all. We don't need to study batch processing so hard either.
Floor 6 Posted 2006-10-28 23:01 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
There are two methods to obtain the current date in pure DOS and assign it to a variable:

Method 1: The content of GetDATE.BAT is as follows, using version: MS-DOS 6.22

@echo off
echo exit|%comspec% /k prompt set mydate=$D$_|find "-">$redtek.bat
call $redtek.bat
for %%. in (%mydate%) do set mydate=%%.
del $redtek.bat >nul
echo %mydate%


The first method uses external commands (Find.exe and Command.com), but Prompt can obtain more interesting content :)

Another method: (This batch file must be named: CURRENT.BAT)
       (Because when I was writing this batch file, in order to reduce a temporary file, I let it call itself - callback)

The "inherent" environment variables of MS-DOS 6.22 that we can get are as follows:

PATH=C:\DOS
PROMPT=$P$G
COMSPEC=C:\COMMAND.COM


This method does not use any external commands, but it is only applicable to pure DOS such as MS-DOS 6.22 (due to date or time format issues).

The content of CURRENT.BAT is as follows:

@echo %dbg% off
if not == goto :end
echo.|date>$redtek.bat
$redtek.bat

:end
del $redtek.bat > nul
set mydate=%4
echo %mydate%


Principle: When executing the internal command DATE to obtain the system date, you must press the Enter key.
    Because this internal command is both to view the system date and to change the system date.

    ECHO. represents outputting an empty line (Enter)
    So, we need to give the DATE command an "extra" "automatic" Enter key, saving us from pressing it.
    ECHO.|DATE does not require us to press the Enter key, and the current date is automatically displayed.
    ECHO.|DATE>$REDTEK.BAT We redirect the output date to a file.
    This redirected file is commanded as a batch file type because we still need it to execute.

This is the output format of the system date: (This is the format in the batch file we redirected)

Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):


If we often use batch files with parameters (%1....%9),
For example: (tentatively named A.BAT)

@echo off
echo %1 %2 %3 %4


Then when we execute the above batch file and execute it with parameters in the following way:

A.BAT A B C D E


It will display as follows: A B C D, and this A B C D are the parameters %1 to %4 that we output and brought in. So, we have redirected the content containing the current date to a batch file:


Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):


Why can't you also treat the word "Current" in the line "Current date is Sun 10-29-2006" as an external batch file?
Like the following:

Current.BAT date is Sun 10-29-2006


Can't this work? Of course it can! So why redirect the output date into an executable batch file.

So, we need to create a Current.bat batch file to output (display) the 4th parameter,
Of course, it is the "10-29-2006" parameter that we want to obtain.

Creating a new Current.bat also requires creating another temporary file outside the main batch program,
So, simply use Current.bat as the main batch file, and then only generate a batch file with the redirected system date.

So, in Current.bat, we redirect Echo.|date> to a file,
Then let that file execute, which is equivalent to bringing in parameters,
As soon as that file executes, it will call Current.bat (now it's ourselves),
Let it call itself,
So at the beginning, we judge whether the parameter is non-empty (indicating that the temporary batch file with the date content has executed and brought in parameters),
So we transfer the process to the :END segment to perform the assignment and display operations.
If we run the CURRENT.BAT batch file for the first time, of course, no parameters will be brought in,
So the system follows the method of redirecting the date to a temporary file... and executes according to our idea.

Since we can assign a date or time content to a variable,
Then is it still a difficult thing to find it to write to a file or do something with it?

[ Last edited by redtek on 2006-10-28 at 11:10 PM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 7 Posted 2006-10-29 02:50 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
redtek is really amazing, I'm confused and understand a bit.
@echo off
echo exit|%comspec% /k prompt set mydate=$D$_|find "-">$redtek.bat
call $redtek.bat
for %%. in (%mydate%) do set mydate=%%.
Don't understand these lines, I tried, there's nothing in that $redtek.bat, how does the date come out,???? confused.
@echo %dbg% off
if not [%4]==[] goto :end
echo.|date>$redtek.bat
$redtek.bat
How to explain this, what's the %dbg% and %4 about??????
Floor 8 Posted 2006-10-29 03:24 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
This webpage makes a D D D D D D D sound when opened in IE, and when opened in Maxthon browser, it requires closing the Maxthon browser.
Floor 9 Posted 2006-10-29 04:36 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally posted by tianzizhi at 2006-10-29 02:50:
@echo %dbg% off
if not == goto :end
echo.|date>$redtek.bat
...


The line "@echo %dbg% off" is for debugging convenience :).
Sometimes one has to repeatedly modify batch scripts to see where exactly the execution goes wrong. When debugging, one would use (@echo on),...
But after debugging, one has to change it back (such as @echo off), which is too cumbersome.

Therefore, adding "@echo %dbg% off" to the first line of the batch script,
usually, this %dbg% variable has no content and is not defined when this batch script is executed,
so it is equivalent to executing @echo off ~ :).

However, if I want to debug the batch script to see the running process,
just enter "SET DBG=ON" in the DOS command line.
Then, this dbg has a parameter defined, and when the batch script is executed,
it will be automatically brought in as a parameter, becoming: "@echo on off"
But it will always perform the function of @echo on (i.e., enabling command echoing).

When not wanting to debug,
just enter "SET DBG=" in the DOS command line
This is equivalent to clearing the DBG variable
Then when running the batch script again, %dbg% is empty, so @echo off takes effect.

Additionally: This "@echo %dbg% off" is not invented by me,
It was seen that the author of this forum "Object-Oriented Batch Language": Will Sort used this line,
At first, I also didn't understand why there was a %dbg% in the middle of echo off.
It looks like Debug abbreviation? Just type set dbg=on for fun, and it worked.
It can be seen that Will Sort's level is high :).

Personally, I feel that this "@echo %dbg% off" is very convenient for repeated debugging :).
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 10 Posted 2006-10-29 05:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  redtek is really amazing, I've learned...

  I didn't understand the meaning of "@echo %dbg% off" written by brother willsort at the beginning. Now that I saw your reply, I know the meaning.
Forum Jump: