"Multithreading" Processing code in a batch program _dos/bat

Source: Internet
Author: User

The following will give you a simple example of using batch processing for multiple processes concurrency under Windows to achieve the "multithreading" effect.

For example: You need to ping all the IP addresses of 192.168.0.1 to 192.168.0.254 and output the results of ping to Log.txt

Implemented as follows:

REM Main program. bat, just one line of code, call Checkip.bat, tested, cpu2.6g mem1g can concurrent 40 or so processes

Copy Code code as follows:

FOR/L%%i in (1,1,254) do (start/min cmd/c checkip.bat)

REM Checkip.bat contents are as follows:

Copy Code code as follows:

Ping%1-n 1
If%errorlevel%==0 (echo%1 alive>>log.txt) Else (echo%1 dead>>log.txt)

Based on the above information, I made my own changes, only need a main program to execute the code:
Program Evolution

Copy Code code as follows:

@echo off
If '%1 ' = = ' (for/l%%a in (1,1,100) do (start/min cmd/c%0 10.120.6.%%a&ping 127.0.0.1-n 1>nul))
If not "%1" = = "" (Ping-a%1-n 1|find "Reply"/i&&echo Alive%1>>logip.txt| | Echo dead%1>>logip.txt)

In order to make the code too long and easy to read, I split into two lines, in fact a line can be completed. You can modify your own, directly in the CMD window to run the statement, if you do not write out, you can ask me again.
Program Knowledge Point Description:

1. Determine if the parameter is empty
2. Use the start command, notice the difference between Goto and call
3. Use ping to do the delay
4. Use the Find command to determine if the IP is turned on and log in
5. Information is not allowed to be exported.

Program Evolution
To empty the log file after each execution, modify the code as follows:

Copy Code code as follows:

@echo off
If "%1" = = "" (
If exist "Logip.txt" Del LogIp.txt
FOR/L%%a in (1,1,100) do (start/min cmd/c%0 10.120.6.%%a&ping 127.0.0.1-n 1 1>nul))
If not "%1" = = "" (Ping-a%1-n 1|find "Reply"/i&&echo Alive%1>>logip.txt| | Echo dead%1>>logip.txt)

Of course, if you want to use two bat files, you can write two lines of code separately in two files, and each file can be written in one line.

Description

1. At the end of the for statement, there is ping 127.0.0.1-n 1 1>nul,-n 1 for ping1 times, followed by 1>nul indicating normal information redirection prohibit output, specific blog content other article handle redirection.
2. The statement if exist "Logip.txt" Del LogIp.txt can be replaced with the Echo [%date%%time%]>logip.txt, their function is the same, the specific use is to see personal usage
These are some of their own experience, afraid of their own forgotten, recorded here.

Supplementary reference Jack_meng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.