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-09 01:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] zw19750516, please come in for a moment, thanks View 1,711 Replies 16
Original Poster Posted 2008-06-03 22:28 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Here's another use of the FOR statement. Please help me figure out how to get the exact result.

My batch file test1.cmd is as follows

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in (test1.cmd) do echo %%i
pause


The result is:

quya.txt
%systemroot%\x.txt
d:\test\y
" %%i in (复件test1.cmd) do echo %%i


The result I want:

quya.txt
c:\x.txt (of course C could also be D)
d:\test\y

How should I do it? Thanks!
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 2 Posted 2008-06-03 22:38 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Can I give it a try

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in ('type "%~0"^|findstr "^rem"') do echo %%i
pause


[ Last edited by 26933062 on 2008-6-3 at 10:40 PM ]
致精致简!
Floor 3 Posted 2008-06-03 22:42 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in ('findstr /v "in" %0') do echo %%i
pause

ps: In the future, when asking for help, please don't call me out by name directly. There are plenty of experts here; I'm really nothing special.

[ Last edited by zw19750516 on 2008-6-3 at 10:44 PM ]
批处理之家新域名:www.bathome.net
Floor 4 Posted 2008-06-03 22:48 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
The second reply is wrong this time. You can't search for REM, because I'm only using REM temporarily here; I don't want to leave junk files on your hard drives. In the actual operation it will be other code.

The third reply got rid of the junk in the last line, but it still didn't solve the problem of the variable in the second line not expanding.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 5 Posted 2008-06-03 23:03 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Hehe, I really did forget about that. Wait, let me think some more...
批处理之家新域名:www.bathome.net
Floor 6 Posted 2008-06-03 23:07 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by quya at 2008-6-3 22:48:
The second reply is wrong this time. You can't search for REM, because I'm only using REM temporarily here; I don't want to leave junk files on your hard drives. In the actual operation it will be other code.

The third reply got rid of the junk in the last line, but it still didn't solve the problem of the variable in the second line not expanding.

This time it should be fine:

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in ('findstr /v "%%i" %0') do call,echo %%i
pause


[ Last edited by zw19750516 on 2008-6-3 at 11:17 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
quya +2 2008-06-03 23:25
批处理之家新域名:www.bathome.net
Floor 7 Posted 2008-06-03 23:24 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Ah, that's really amazing!

If it could be modified a bit more to remove the junk line too—for example, judging that a line has lots of spaces (which is unlikely to be a filename)—that would be even better. Otherwise, using "in" to exclude lines might accidentally exclude other things too.

I never thought of do call, echo. Although I can guess part of it, I'd still like a detailed explanation. Thanks!

Going to sleep now, see you tomorrow.

[ Last edited by quya on 2008-6-3 at 11:26 PM ]
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 8 Posted 2008-06-03 23:28 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-6-3 11:07 PM:

This time it should be fine:

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in ('findstr ...



Is %%i in this set just an ordinary string?~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 9 Posted 2008-06-03 23:48 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Originally posted by quya at 2008-6-3 11:24 PM:
Ah, that's really amazing!

If it could be modified a bit more to remove the junk line too—for example, judging that a line has lots of spaces (which is unlikely to be a filename)—that would be even better. Otherwise, using "in" to exclude lines might acci ...


You can think of for as a special command-line interpreter. When the for command is initialized, it has already expanded all referenced variables, which means %%i gets replaced with rem sssss>>%systemroot%\x.txt

call can be understood as a child process opened by for. Inside this child process, the system environment variable %systemroot% gets replaced once again with the real value.
Floor 10 Posted 2008-06-04 00:28 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by quya at 2008-6-3 23:24:
Ah, that's really amazing!

If it could be modified a bit more to remove the junk line too—for example, judging that a line has lots of spaces (which is unlikely to be a filename)—that would be even better. Otherwise, using "in" to exclude lines might acci ...

I'm going to sleep too. This ought to be able to identify it without mistakenly excluding anything else.

@echo off
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
for /f "tokens=2 delims=>" %%i in ('findstr /v "delims=>" %0') do call,echo %%i
pause
批处理之家新域名:www.bathome.net
Floor 11 Posted 2008-06-04 01:23 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
:

Actually, the most accurate way is to put the content to be looped by for at the end of the file, then use for's skip to read it. That way it will be foolproof.
致精致简!
Floor 12 Posted 2008-06-04 01:34 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
At the end? Shouldn't it be at the beginning?
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 13 Posted 2008-06-04 07:33 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
This way there will absolutely be no mistaken exclusions.

@echo off
for /f "tokens=2 delims=>" %%i in ('more +3 "%~0"') do call,echo %%i
pause&exit
rem hhgjg>quya.txt
rem sssss>>%systemroot%\x.txt
rem dddd>>d:\test\y
致精致简!
Floor 14 Posted 2008-06-04 23:33 ·  中国 天津 联通
初级用户
Credits 26
Posts 13
Joined 2008-06-03 22:52
18-year member
UID 120488
Gender Male
Status Offline
I'm totally confused from reading this...
Floor 15 Posted 2008-06-04 23:46 ·  中国 江苏 苏州 常熟市 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
First, let me ask everyone a question: what does the "," after CALL mean? Does it have any special meaning?

Second, you all may have misunderstood the original intent of this program. This small piece of code is part of an encrypted batch file, meant to interfere with other people analyzing the encrypted variables by using >>filename. Of course, the line “for /f "tokens=2 delims=>" %%i in ('findstr /v "%%i" %0') do call,echo %%i” itself is also considered part of what gets encrypted.

In the end, "ECHO %%i" will be replaced with "del %%i"

So the most important thing is not really excluding the FOR statement itself, but excluding cases that might generate things like “del *.*”, which could cause unpredictable major errors. Although that's not very likely, it would be best if it can be taken into account, just in case. This time 26933062's programming approach drifted away from what I had in mind, though of course that was because I didn't explain it clearly, so that's my fault.

If instead of deleting the file being analyzed by others, it wrote false information to it, that would of course be best, but programming that way would be too complex and bloated, so it's not very feasible. What I want is to use relatively simple statements to create a good effect.

Although batch file encryption is difficult, if multiple methods are used together, it can still accomplish something.

[ Last edited by quya on 2008-6-4 at 11:48 PM ]
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Forum Jump: