![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-18 12:04 |
47,814 topics / 349,908 posts / today 1 new / 48,259 members |
| DOS疑难解答 & 问题讨论 (解答室) » How to create a TXT file in DOS batch and write the current date |
| Printable Version 3,746 / 9 |
| Floor1 woshishiu | Posted 2006-10-28 03:19 |
| 新手上路 Posts 1 Credits 4 | |
|
请问如何在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 |
|
| Floor2 tianzizhi | Posted 2006-10-28 05:27 |
| 高级用户 Posts 214 Credits 623 | |
|
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 |
|
| Floor3 electronixtar | Posted 2006-10-28 05:31 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
If it's pure DOS, it's not easy to handle
|
|
| Floor4 cchessbd | Posted 2006-10-28 08:33 |
| 高级用户 Posts 222 Credits 525 | |
|
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. |
|
| Floor5 lxmxn | Posted 2006-10-28 08:55 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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. |
|
| Floor6 redtek | Posted 2006-10-28 23:01 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
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 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: 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: 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) If we often use batch files with parameters (%1....%9), For example: (tentatively named A.BAT) Then when we execute the above batch file and execute it with parameters in the following way: 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: 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: 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 ] |
|
| Floor7 tianzizhi | Posted 2006-10-29 02:50 |
| 高级用户 Posts 214 Credits 623 | |
|
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?????? |
|
| Floor8 不得不爱 | Posted 2006-10-29 03:24 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
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.
|
|
| Floor9 redtek | Posted 2006-10-29 04:36 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
Originally posted by tianzizhi at 2006-10-29 02:50: 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 :). |
|
| Floor10 lxmxn | Posted 2006-10-29 05:46 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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. |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |