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-07-29 09:01
中国DOS联盟论坛 » 论坛回收站 » Another FOR question (updated, the main question is in post #3) View 1,951 Replies 14
Original Poster Posted 2009-06-24 04:37 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
Floor 2 Posted 2009-06-24 04:42 ·  中国 福建 泉州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Study the usage of FOR yourself.
Under CMD
for /?
Example
for /f %%i in (pcname.txt) do (
net use \\%%i\c$\windows\system32\ || echo %%i>>故障.txt
)

The function of || is that when the previous statement fails, the following statement is executed.
&& is the opposite.
& executes regardless.

All of these can be learned from CMD help.

[ Last edited by chenall on 2009-6-24 at 04:44 ]
QQ:366840202
http://chenall.net
Floor 3 Posted 2009-06-24 04:59 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
If there is a set of commands after do, should it be enclosed in ()? I used /? to check, but the help is not very detailed. I can't really understand it.
What I want to achieve is, read computer names from a text file, then do the mapping. If the mapping fails, record that computer name. If the mapping succeeds, then check the size of that computer's hots file. If the size is not equal to the size I want, also record that computer name; if the hots file size equals the size I want, then do not record it. Disconnect the mapping, then continue reading the next computer name. Before, you taught me how to get the hots file size.
But now, how do I determine success or failure of the mapping? And after letting for read one computer name and execute the required commands, then go read the second computer name—doesn't for /f read everything first and then execute?

[ Last edited by wwx0423 on 2009-6-24 at 06:25 ]
Floor 4 Posted 2009-06-24 05:00 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
Using if together with for is best.
But I don't know how to use if and for, or variables either. I just learned from you the difference between %i and %%i in cmd and in batch files.
Floor 5 Posted 2009-06-24 05:13 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
Is chenall here? Can you get on QQ?
Floor 6 Posted 2009-06-24 05:29 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
If you really want to use if, you can use the syntax if [not] errorlevel number command [else expression]
Floor 7 Posted 2009-06-24 06:06 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
Can what I want to achieve be done without if?
Can it be done using only for?
There are several places that need checking. Check whether the mapping succeeded, and check whether the file size equals the value I want.
Floor 8 Posted 2009-06-24 06:22 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Did you say in post #1 that you wanted to check the file size? Read the help yourself.
Floor 9 Posted 2009-06-24 06:23 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
The main question is in post #3. 
Floor 10 Posted 2009-06-24 07:15 ·  中国 福建 泉州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
I don't know whether you didn't read the thread carefully, but I already gave an example above.
net use \xxx\xx || execute on failure.
net use \xxx\xx && execute on success.

Although for reads everything, it still executes one line at a time.

You can consider not completing all functions inside for, but putting what needs to be done in one module.
First get the size of the HOSTS file
for %%i in (c:\windows\system32\drivers\etc\hosts) set hosts=%%~zi
for /f %%i in (pcname.txt) do call :检测 %%i
goto :eof

:检测
if "%~1"=="" goto :eof
net use \\%~1\c$\windows\system32\ || (echo %~1>>故障.txt&&goto :eof)
for %%i in (\\%~1\c$\windows\system32\drivers\etc\hosts) do (
if not "%%~zi"=="%hosts%" echo %~1>>失败.txt
)
goto :eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
Hanyeguxing +2 2009-06-24 07:38
QQ:366840202
http://chenall.net
Floor 11 Posted 2009-06-24 07:23 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
I can't understand what you wrote.
for %%i in (c:\windows\system32\drivers\etc\hosts) set hosts=%%~zi
for /f %%i in (pcname.txt) do call :检测 %%i
goto :eof

:检测
if "%~1"=="" goto :eof
net use \\%~1\c$\windows\system32\ || (echo %~1>>故障.txt&&goto :eof)
for %%i in (\\%~1\c$\windows\system32\drivers\etc\hosts) do (
if not "%%~zi"=="%hosts%" echo %~1>>失败.txt
)
goto :eof

Where does %~1 come from? Also, what I need to check is the mapped computer's HOTS. If the computer names in the list are, for example,
pc1
pc2
pc3
then what I need to check is pc1's hots and pc2's hots... if it isn't mapped first, how can I check it?

[ Last edited by wwx0423 on 2009-6-24 at 07:24 ]
Floor 12 Posted 2009-06-24 07:32 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
for %%i in (c:\windows\system32\drivers\etc\hosts) set hosts=%%~zi
::Expand the local computer's host file size and use it as the comparison baseline
for /f %%i in (pcname.txt) do call :检测 %%i
::Parse pcname.txt and call the label, while passing variable %%i into the sub-label
goto :eof

:检测
if "%~1"=="" goto :eof
::Check whether variable %%i (the one passed in by call from above, that is, the first one; %~1 is used to expand and remove the quotes) is empty
net use \\%~1\c$\windows\system32\ || (echo %~1>>故障.txt&&goto :eof)
::Try to establish the connection; if it fails then output %%i
for %%i in (\\%~1\c$\windows\system32\drivers\etc\hosts) do (
if not "%%~zi"=="%hosts%" echo %~1>>失败.txt
)
::Expand the size of the remote host and compare it with the local computer's host size
goto :eof

For the %~1 issue, refer to batch variable expansion and the use of the call command

[ Last edited by Hanyeguxing on 2009-6-24 at 07:37 ]
Floor 13 Posted 2009-06-24 07:36 ·  中国 福建 泉州 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Many thanks to the poster above for the explanation,

If the OP really wants to learn batch well,
you should really read the help for CMD commands carefully, and make good use of /?
There are generally examples in it. If you can't see what it does after trying once, then try a few more times.

I started learning from DOS commands. At that time the network wasn't very developed, so I would do one command/? and then try the parameters one by one.

Now the network is very convenient.
And CMD help is all in Chinese, so it should be fairly quick to learn.
QQ:366840202
http://chenall.net
Floor 14 Posted 2009-06-24 07:57 ·  中国 广东 深圳 电信
初级用户
Credits 33
Posts 30
Joined 2009-02-19 09:47
17-year member
UID 139249
Gender Male
Status Offline
Thank you very much. I found that just doing for %%i in (\\%~1\c$\windows\system32\drivers\etc\hosts) do (
if not "%%~zi"=="%hosts%" echo %~1>>1.txt is enough to compare them. No need to do the mapping again.

Thank you very, very much... I asked this way because it's rather urgent. I'm currently learning batch files.
Floor 15 Posted 2009-06-24 08:06 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
After using net use to establish the connection, commands and batch files can directly read and write remote files.
Here for directly reads the remote host file


Originally posted by chenall at 2009-6-24 07:36:
Many thanks to the poster above for the explanation,

If the OP really wants to learn batch well,
you should really read the help for CMD commands carefully, and make good use of /?
There are generally examples in it. If you can't see what it does after trying once, then try a few more times.

I started learning from DOS commands. At that time the network wasn't very developed, so I would do one command/? and then try the parameters one by one.

Now the network is very convenient.
And CMD help is all in Chinese, so it should be fairly quick to learn.


Current XP already integrates a powerful Help and Support Center by default

[ Last edited by Hanyeguxing on 2009-6-24 at 08:08 ]
Forum Jump: