![]() |
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-09-15 13:38 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » [Closed]Why is the 1 lost when executing echo %%i %%j 1 >> ok.txt? |
| Printable Version 5,881 / 22 |
| Floor1 hhh246 | Posted 2006-09-21 04:03 |
| 初级用户 Posts 31 Credits 74 | |
|
------- 1.txt ------
154154 123 2006-9-19 4564567 321 2006-9-30 123 54 2006-10-20 for /f %%i in ('date/t') do set adate=%%i for /f "tokens=1,2,3" %%i in (1.txt) do if %%k == %adate% echo %%i %%j 1 >>ok.txt [ Last edited by HAT on 2008-10-15 at 23:48 ] |
|
| Floor2 vkill | Posted 2006-09-21 05:09 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
First, let's see what your %date/t% is?
|
|
| Floor3 electronixtar | Posted 2006-09-21 08:11 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
>>ok.txt echo %%i %%j 1
1>>ok.txt will write standard output to ok.txt. Anyway, there cannot be a number before > or >> |
|
| Floor4 hhh246 | Posted 2006-09-21 08:12 |
| 初级用户 Posts 31 Credits 74 | |
Originally posted by he200377 at 2006-9-21 05:09: Set the current date to a variable, such as adate=2006-09-20 |
|
| Floor5 hhh246 | Posted 2006-09-21 08:15 |
| 初级用户 Posts 31 Credits 74 | |
Originally posted by electronixtar at 2006-9-21 08:11: It still doesn't work after removing 1 |
|
| Floor6 pengfei | Posted 2006-09-21 09:09 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Modify the code according to the content of the original poster's 1.txt as follows:
@echo off setlocal enabledelayedexpansion for /f "tokens=1-3 delims=- " %%i in ('date/t') do ( set age=%%i set /a month=1%%j-100 set /a daily=1%%k-100 for /f "tokens=1,2,3" %%a in (1.txt) do ( if /i "%%c"=="!age!-!month!-!daily!" ( echo %%a %%b 1 >>ok.txt ) ) ) The problem lies in the format, resulting in the if statement judgment being incorrect and unable to perform the redirection. |
|
| Floor7 namejm | Posted 2006-09-21 09:15 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The format of %date% is 2006-09-19, not 2006-9-19 in 1.txt. So, in your code, the if statement will never hold true in any case.
Also, the format of echo %%i %%j 1 >>ok.txt is incorrect. The final 1 is regarded as a handle value by CMD, which will result in no content being input into ok.txt. What electronixtar said in "Post #3" that "there cannot be a number before >>" is not precise enough. The accurate statement is: If the last column of the echo is any number from 0 to 9, no content will be written into ok.txt. |
|
| Floor8 hhh246 | Posted 2006-09-21 09:15 |
| 初级用户 Posts 31 Credits 74 | |
|
True expert, problem solved
|
|
| Floor9 pengfei | Posted 2006-09-21 09:22 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
In addition, the format of echo %%i %%j 1 >>ok.txt is incorrect... [/quote]
It seems that there is no such error as Brother Feiyang said. If 0-9 are single, they will be regarded as handles, but there are several values before this redirection, so the situation you mentioned will not occur! |
|
| Floor10 namejm | Posted 2006-09-21 09:29 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The error I'm talking about isn't that it goes wrong when executed, but that the content written into ok.txt isn't the complete content you want. For example, you can run the statement echo 123 1>ok.txt and try it. In ok.txt, it's not the 123 1 you want, but just 123, missing the last column of the number 1.
|
|
| Floor11 hhh246 | Posted 2006-09-21 09:30 |
| 初级用户 Posts 31 Credits 74 | |
|
Try adding a space after 1 more times can solve it
|
|
| Floor12 pengfei | Posted 2006-09-21 09:33 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Hehe~ Brother namejm is right.
The problem of unable to redirect is solved by adding a space after the楼主... |
|
| Floor13 namejm | Posted 2006-09-21 09:47 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
If the requirements are not too strict, you can add a space after the number in the last column to avoid CMD recognition problems. However, if the requirements are very strict, such as requiring that the numbers 0-9 in the last column be written into the text without a space after them, some processing should be done on the echo statement, and any of the following forms should be used:
In addition, you can also use the format echo ^content>test.txt, but this form has great limitations, that is, the escape character ^ can only be added in front of a single 0-9 in the last column. If it is the writing of echo ^123 1>test.txt, it is still not working. It should be written as echo 123 ^1>test.txt. |
|
| Floor14 pengfei | Posted 2006-09-21 09:49 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Yes, when this situation occurs, try to use character escaping...
|
|
| Floor15 electronixtar | Posted 2006-09-21 10:35 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
Can be escaped with "" - seen on ntcmds.chm
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |