![]() |
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-13 23:01 |
47,812 topics / 349,916 posts / today 0 new / 48,268 members |
| 论坛回收站 » Another FOR question (updated, the main question is in post #3) |
| Printable Version 2,050 / 14 |
| Floor1 wwx0423 | Posted 2009-06-24 04:37 |
| 初级用户 Posts 30 Credits 33 | |
| Floor2 chenall | Posted 2009-06-24 04:42 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
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 ] |
|
| Floor3 wwx0423 | Posted 2009-06-24 04:59 |
| 初级用户 Posts 30 Credits 33 | |
|
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 ] |
|
| Floor4 wwx0423 | Posted 2009-06-24 05:00 |
| 初级用户 Posts 30 Credits 33 | |
|
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. |
|
| Floor5 wwx0423 | Posted 2009-06-24 05:13 |
| 初级用户 Posts 30 Credits 33 | |
|
Is chenall here? Can you get on QQ?
|
|
| Floor6 Hanyeguxing | Posted 2009-06-24 05:29 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
If you really want to use if, you can use the syntax if [not] errorlevel number command [else expression]
|
|
| Floor7 wwx0423 | Posted 2009-06-24 06:06 |
| 初级用户 Posts 30 Credits 33 | |
|
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. |
|
| Floor8 Hanyeguxing | Posted 2009-06-24 06:22 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
Did you say in post #1 that you wanted to check the file size? Read the help yourself.
|
|
| Floor9 wwx0423 | Posted 2009-06-24 06:23 |
| 初级用户 Posts 30 Credits 33 | |
|
The main question is in post #3.
|
|
| Floor10 chenall | Posted 2009-06-24 07:15 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
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 |
|
| Floor11 wwx0423 | Posted 2009-06-24 07:23 |
| 初级用户 Posts 30 Credits 33 | |
|
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 ] |
|
| Floor12 Hanyeguxing | Posted 2009-06-24 07:32 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
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 ] |
|
| Floor13 chenall | Posted 2009-06-24 07:36 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
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. |
|
| Floor14 wwx0423 | Posted 2009-06-24 07:57 |
| 初级用户 Posts 30 Credits 33 | |
|
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. |
|
| Floor15 Hanyeguxing | Posted 2009-06-24 08:06 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
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: Current XP already integrates a powerful Help and Support Center by default [ Last edited by Hanyeguxing on 2009-6-24 at 08:08 ] |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |