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-08-01 00:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed]Why is the 1 lost when executing echo %%i %%j 1 >> ok.txt? View 5,544 Replies 22
Original Poster Posted 2006-09-21 04:03 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
------- 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 ]
Floor 2 Posted 2006-09-21 05:09 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
First, let's see what your %date/t% is?
Floor 3 Posted 2006-09-21 08:11 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
>>ok.txt echo %%i %%j 1

1>>ok.txt will write standard output to ok.txt. Anyway, there cannot be a number before > or >>

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-09-21 08:12 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
Originally posted by he200377 at 2006-9-21 05:09:
First, take a look at what your %date/t% is?



Set the current date to a variable, such as adate=2006-09-20
Floor 5 Posted 2006-09-21 08:15 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
Originally posted by electronixtar at 2006-9-21 08:11:
>>ok.txt echo %%i %%j 1

Writing standard output to ok.txt with 1. Anyway, there can't be a number before > or >>


It still doesn't work after removing 1
Floor 6 Posted 2006-09-21 09:09 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
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.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 7 Posted 2006-09-21 09:15 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-09-21 09:15 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
True expert, problem solved
Floor 9 Posted 2006-09-21 09:22 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
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!
业精于勤而荒于嬉,形成于思而毁于随。
Floor 10 Posted 2006-09-21 09:29 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-09-21 09:30 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
Try adding a space after 1 more times can solve it
Floor 12 Posted 2006-09-21 09:33 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Hehe~ Brother namejm is right.

The problem of unable to redirect is solved by adding a space after the楼主...
业精于勤而荒于嬉,形成于思而毁于随。
Floor 13 Posted 2006-09-21 09:47 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:


  1、(echo content)>test.txt
  2、>test.txt echo content


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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-09-21 09:49 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
20-year member
UID 58987
From 湖南.娄底
Status Offline
Yes, when this situation occurs, try to use character escaping...
业精于勤而荒于嬉,形成于思而毁于随。
Floor 15 Posted 2006-09-21 10:35 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Can be escaped with "" - seen on ntcmds.chm

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'>"
Forum Jump: