In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are the advanced skills of DOS batch programming? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. Interactive interface design
There's nothing to say. Take a look at the menu interface designed by the master:
@ echo offclstitle ultimate multi-function repair: menuclscolor 0Aecho.echo = = echo Please select the operation to be performed, and then press enter echo = = echo.echo 1. Network repair and Internet related settings, repair IE, custom blocking website echo.echo 2. Virus kill tool, port close tool, turn off automatic playback echo.echo 3. Clear all redundant self-startup items and fix system error echo.echo 4. Clean up the garbage of the system and improve the startup speed of echo.echo Q. To exit echo.echo.:choset choice=set / p choice=, please choose: IF NOT "% choice%" = = "SET choice=%choice:~0,1%if / I"% choice% "= =" 1 "goto ipif / I"% choice% "= =" 2 "goto setsaveif / I"% choice% "= =" 3 "goto kaijiif / I"% choice% "= =" 4 "goto cleanif / I"% choice% "= =" Q "goto enddecho selection is invalid, please re-enter echo.goto cho
As long as you finish the previous chapters of this tutorial, the above program should be able to understand.
Second, if... Else... Conditional statement
As mentioned earlier, DOS conditional statements are mainly in the following forms
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
Enhanced usage: IF [/ I] string1 compare-op string2 command
Add / I to the enhanced usage and it is not case-sensitive!
There are also some symbols used to judge numbers in enhanced usage:
EQU-equals
NEQ-not equal to
LSS-less than
LEQ-less than or equal to
GTR-greater than
GEQ-greater than or equal to
The above command commands can all use parentheses to use a combination of multiple commands, including the else clause, in which conditional or loop commands can be nested.
For example:
IF EXIST filename (
Del filename
) ELSE (
Echo filename missing
)
It can also be written as:
If exist filename (del filename) else (echo filename missing)
However, this writing method is not suitable for the use of too many commands or nested commands.
III. Circular statement
1. Specify the number of cycles
FOR / L% variable IN (start,step,end) DO command [command-parameters]
Combine commands:
FOR / L% variable IN (start,step,end) DO (
Command1
Command2
……
)
2. Execute a circular statement on a collection.
FOR% variable IN (set) DO command [command-parameters]
% variable specifies a parameter that can be replaced by a single letter.
(set) specify a file or group of files. You can use wildcards.
Commands executed by command on each file can be combined with multiple commands in parentheses.
FOR / R [[drive:] path]% variable IN (set) DO command [command-parameters]
Check the directory tree rooted in [drive:] path and point to the
FOR statement. If no directory is specified after / R, the current
Catalogue. If the set is only a single point (.) Character, the directory tree is enumerated.
As before, command can be combined in parentheses:
FOR / R [[drive:] path]% variable IN (set) DO (
Command1
Command2
……
Commandn
)
3. Conditional cycle
The above loop structure is implemented with the for command. The disadvantage of the for command loop is that the whole loop is treated as a command statement, which involves variable delay.
Using Goto statements and conditional judgment, dos can implement conditional loops. It's very simple. Take a look at the example:
Example:
The @ echo offset var=0rem * loop begins: continueset / a var+=1echo% var% secondary cycle if% var% lss 100 goto continuerem * cycle ends echo loop execution completed pause
Example:
@ echo offset var=100rem * Loop begins: continueecho% var% Secondary cycle set / a var-=1if% var% gtr 0 goto continuerem * Loop ends echo Loop execution completed pause
4. Subroutine
In batch programs, you can call external runnable programs, such as exe programs, or other batch programs, which can also be regarded as subroutines, but they are not convenient enough. If many programs are called, they are not concise and tedious.
In windowsXP, a batch can call a segment of this program, which is equivalent to a subroutine, which is usually placed after the main program.
Subroutine call format:
CALL: label arguments
Subroutine syntax:
: label
Command1
Command2
.
Commandn
Goto: eof
In the subroutine segment, the parameter% 0 refers to the label: label
The subprocedure is usually placed at the end, and pay attention to add exit or jump statement at the end of the main program to avoid mistakenly entering the subroutine.
The variables in the subroutine and the main program are global variables, and their scope is the entire batch program.
The parameters passed to the subroutine are specified in the call statement and called in the form of% 1,% 2 to% 9 in the subroutine, and the data returned by the subroutine only needs to be referenced directly after the call is over. Of course, you can also specify the return variable, see the following example.
Subroutine example 1:
@ echo offcall: sub return Hello echo subroutine return value:% return%pause:subset% 1=%2goto: eof
Running result: Hello
Subroutine example 2: design a subroutine for the addition of multiple integers
@ echo offset sum=0call: sub sum 10 20 35echo data summation result:% sum%pause:subrem parameter 1 is the return variable name set / a% 1=%1+%2shift / 2if not "% 2" = "" goto subgoto: eof
Operation result: 65
In the win98 system, the above label call is not supported, the subroutine must be saved as a batch program separately, and then called.
Fifth, use ftp command to realize automatic download
Ftp is a commonly used download tool. There are more than 40 commonly used commands in the ftp interface. I have learned it myself and will not introduce it. This paper introduces how to call the ftp command with the dos command line to realize the automatic login of ftp, upload and download, and automatically exit the ftp program.
You can actually save the ftp command combination as a text file, and then invoke it with the following command.
Ftp-n-s: [[drive:] path] filename
The above filename is the ftp command file, including login IP address, user name, password, operation command, etc.
Example:
Open 90.52.8.3 # Open ipuser iware # user iwarepassword8848 # password bin # binary transfer mode promptcd tmp1 # switch to tmp1 directory under iware user pwdlcd d:\ download # local directory mget * # download all files under tmp1 directory bye # exit ftp
6. Use 7-ZIP to realize command line compression and decompression.
Syntax format: (see 7-zip help file for details, you can skip dizziness and learn again)
7z [...] [...]
Each command of 7z.exe has different parameters. Please see the help file.
Is the compressed package name
Is the file name, and wildcards or file lists are supported
Where 7z is the command line compression and decompression program 7z.exe, which is a command included in 7z.exe, listed as follows:
A: Adds files to archive. Add to compressed package
A command available parameters:
-I (Include)
-m (Method)
-p (Set Password)
-r (Recurse)
-sfx (create SFX)
-si (use StdIn)
-so (use StdOut)
-ssw (Compress shared files)
-t (Type of archive)
-u (Update)
-v (Volumes)
-w (Working Dir)
-x (Exclude)
B: Benchmark
D: Deletes files from archive. Delete files from the zip file
The available parameters of the d command:
-I (Include)
-m (Method)
-p (Set Password)
-r (Recurse)
-u (Update)
-w (Working Dir)
-x (Exclude)
E: Extract unzips the file to the current directory or specified directory
The available parameters of the e command are:
-ai (Include archives)
-an (Disable parsing of archive_name)
-ao (Overwrite mode)
-ax (Exclude archives)
-I (Include)
-o (Set Output Directory)
-p (Set Password)
-r (Recurse)
-so (use StdOut)
-x (Exclude)
-y (Assume Yes on all queries)
L: Lists contents of archive.
T: Test
U: Update
X: eXtract with full paths unzips the file to the current directory or specified directory with the full path of the file
The available parameters of the x command are:
-ai (Include archives)
-an (Disable parsing of archive_name)
-ao (Overwrite mode)
-ax (Exclude archives)
-I (Include)
-o (Set Output Directory)
-p (Set Password)
-r (Recurse)
-so (use StdOut)
-x (Exclude)
-y (Assume Yes on all queries)
7. Call VBScript program
Using the Windows script host, you can run scripts from a command prompt. CScript.exe provides command line switches for setting script properties.
Usage: CScript script name [script options.] [script parameters.]
Options:
/ / B batch mode: no script errors and prompts are displayed
/ / D enable Active Debugging
/ / E:engine uses the engine that executes the script
/ / H:CScript changes the default script host to CScript.exe
/ / H:WScript changes the default script host to WScript.exe (default)
/ / I interaction mode (default, as opposed to / B)
/ / Job:xxxx performs a WSF job
/ / Logo display logo (default)
/ / Nologo does not display logos: no flags are displayed during execution
/ / S saves the current command line options for this user
/ / T:nn timeout setting seconds: the maximum time allowed for a script to run
/ / X executes the script in the debugger
/ / U denotes the redirected Imax O from the console with Unicode
The script name is the name of the script file with the extension and required path information, such as d:\ admin\ vbscripts\ chart.vbs.
Script options and parameters are passed to the script. The script parameter is preceded by a slash (/). Each parameter is optional; however, script options cannot be specified without a script name specified. If no parameters are specified, CScript displays CScript syntax and valid host parameters.
Convert the batch to an executable file:
Because a batch file is a text file, anyone can edit it casually, which will destroy the commands inside, so if you convert it to an executable file in .com format, not only the execution efficiency will be greatly improved. and will not destroy the original function, but also can raise the priority to the highest level. Bat2Com can do this conversion work.
Knowledge: in the DOS environment, the priority of executable files from high to low is .com > .exe > .bat > .cmd, that is, if there are four types of files with the same file name in the same directory, when only typing the file name, DOS executes name.com, if you need to execute the other three files, you must specify the full name of the file, such as name.bat.
This is a free green tool with a size of only 5.43K, which can be run on the command line of a pure DOS or DOS window, usage: Bat2Com FileName, so that an executable file named FileNme.com is generated in the same directory, and the execution effect is the same as the original .bat file.
IX. Time delay
What is time delay? As the name implies, the execution of one command is delayed for a period of time before the next command is executed.
For the application of delay, see the following section: "Simulation Progress Bar".
1. Delay by using ping command
Example:
@ echo offecho before delay:% time%ping / n 3 127.0.0.1 > nulecho after delay:% time%pause
Explanation: the "/ n" parameter of the ping command is used to indicate how many requests to send to the specified ip. In this example, three requests are sent to the native ip (127.0.0.1). 127.0.0.1 can be abbreviated to 127.1. "> nul" is to block out what is displayed by the ping command.
2. Delay by using for command
Example:
@ echo offecho before delay:% time%for / l% I in do echo% I > nulecho after delay:% time%pause
Explanation: the principle is very simple, that is, to achieve the purpose of delay by using a count loop and shielding what it shows.
3. Using the vbs delay function, the accuracy is millisecond and the error is less than 1000 milliseconds.
Example:
Echo offecho% time%call: delay 5000echo% time%pauseexit:delayecho WScript.Sleep% 1 > delay.vbsCScript / / B delay.vbsdel delay.vbsgoto: eof
The run shows:
10:44:06.45
10:44:11.95
Please press any key to continue. . .
The above running results show that the actual delay is 5500 milliseconds, and the extra 500 milliseconds takes time to create and delete temporary files. The error is within a second.
4. Only batch commands are used to achieve arbitrary time delay with an accuracy of 10 milliseconds and an error of less than 50 milliseconds.
Deferred operations can be achieved with only batch commands.
Example:
@ echo offset / p delay= Please enter the number of milliseconds to delay: set TotalTime=0set NowTime=%time%:: read start time The time format is: 13:01:05.95echo program start time:% NowTime%:delay_continueset / a minute1=1%NowTime:~3,2%-100:: read start time set / a second1=1%NowTime:~-5,2%%NowTime:~-2%0-1000000 set: change the number of seconds of the start time to milliseconds set NowTime=%time%set / a minute2=1%NowTime:~3,2%-100:: read the minutes of the current time set / a second2=1%NowTime: ~-5 time%echo 2% delay% milliseconds echo actual delay time: change the number of seconds of the current time to milliseconds set / a TotalTime+= (% minute2%-%minute1%+60)% 60*60000+%second2%-%second1%if% TotalTime% lss% end time:% time%echo set delay time:% NowTime millisecond echo actual delay time:% NowTime millisecond pause
The run shows:
Please enter the number of milliseconds to delay: 6000
Start time of the program: 15Rd 32RL 16.37
End time of the program: 15Rd 32RL 22.37
Set delay time: 6000 Ms
Actual delay time: 6000 Ms
Please press any key to continue. . .
The principle of implementation: first set the number of milliseconds to delay, and then accumulate the time with a loop until the cumulative time is greater than or equal to the delay time.
Error: windows system time can only be accurate to 10 milliseconds, so there may be a 10 millisecond error in theory.
After testing, when the delay time is more than 500 milliseconds, the above delay program generally has no error. When the delay time is less than 500 milliseconds, there may be tens of milliseconds error. Why? Because the delay program itself has running time, and the system time can only be accurate to 10 milliseconds.
For ease of reference, change the above example to the form of subroutine invocation:
@ echo offecho program start time:% Time%call: delay 10echo actual delay time:% totaltime% millisecond echo program end time:% time%pauseexit::- is the delay subroutine -: delay@echo offif "% 1" = "" goto: eofset DelayTime=%1set TotalTime=0set NowTime=%time%:: read start time The time format is: 13:01:05.95:delay_continueset / a minute1=1%NowTime:~3,2%-100set / a second1=1%NowTime:~-5,2%%NowTime:~-2%0-100000set NowTime=%time%set / a minute2=1%NowTime:~3,2%-100set / a second2=1%NowTime:~-5,2%%NowTime:~-2%0-100000set / a TotalTime+= (% minute2%-%minute1%+60)% 60*60000+%second2%-%second1%if% TotalTime% lss% DelayTime% goto delay_continuegoto: eof
10. Simulation progress bar
Here is a program to simulate the progress bar. If you apply it to your own program, you can make your program more beautiful.
@ echo offmode con cols=113 lines=15 & the color 9fclsecho.echo program is initializing. . . Echo.echo ┌──┐ set/p= ■ run-> enter cmd-> edit-> ctrl+p (meaning special characters are allowed)-> press ctrl+a to display the smiley face pattern.
(if you want to continue entering special characters, press ctrl+p again, and then ctrl+ a letter)
The above is the input method of special characters, selected from the Hero tutorial, it is very useful. That is, use the editor program edit to enter special characters, then save them as a text file, then open the file under windows and copy the special symbols in it.
Some simple special symbols can be entered directly in the dos command window and saved as a text file with redirection.
Example:
C: > ECHO ^ G > temp.txt
"^ G" is inputted with Ctrl+G or Alt+007. Entering multiple ^ Gs can produce multiple tones.
The application of special characters is also very interesting. Here is just one example: backspace key.
The Backspace key means to delete the character on the left. This key cannot be entered normally in the document, but can be typed and copied through the edit editor. That is, "".
Using the backspace key, you can design flashing text effects.
Example: text flicker
@ echo off:startset/p= has bright moonlight in front of the bed nul:: setting delay time set/p a =
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.