1. What is Windows BATCH
Batch, sometimes called bat, is a running script on the Windows platform, just like the shell script on *nix (Linux and Unix) and other scripts (Perl,python), essentially a text file, But when you use specific software to explain it, it becomes a running script. On Windows, the executable script is the batch file, also called batch file, which is a name inherited from the DOS era, meaning to put a lot of commands together to run. Its extension is *.bat, double-click to run directly, at the command line (cmd or command prompt) The following can also be used as a command to run. Because this is something Windows supports, it can only be used on Windows platforms. (except for special circumstances, do not be bored, boring). For example, the famous Windows garbage cleanup gadget is a bat batch file.
2. Why do I need bat
In the final analysis, to improve efficiency. This is more difficult to explain, because in the GUI of the big line of the today, and then the command line, very many people will not be familiar with, or think this is a setback. But a lot of books on improving productivity will mention that command line can greatly improve the efficiency, because the command can easily write the path directly, rather than in the file browser layer of open, and command line can combine commands, that is, to combine different commands together to complete a task, Instead of having to open more than one GUI form (or even very often, GUI forms are not finished). For this *nix of the program should be the most experienced, the real *nix master will not leave the command line. Their job is simply to open a terminal (command line), edit with Vim, and all the other tools with command line to complete, even if they do not use the mouse!
Windows CMD is not as powerful as *nix's terminal, so it's unrealistic to do it all in its own, although there are a lot of tools that can make cmd stronger, but still can't reach *nix, because they're fundamentally different routes. Nix is for developers, and Windows is for the general public.
More detailed is to dry and automation, that is, to let the computer to help you over a number of mechanical repetitive tasks (for dry and automation can participate in "the Productive Programmer" and "pragmatic Programmer "). Because bat can combine commands, so an operation needs more than one command, or a larger number of parameters, or a longer path, it can be written as bat, into a simple command, at a time two may not feel, but when every day, it saves a lot of typing. For example, I want to often crawl tcpdump on my phone, and then look at it, usually using commands such as the following:
D:\>ADB Shell Tcpdump-p-S 0-i any-w/sdcard/capture.pcapd:\>adb pull/sdcard/capture.pcap.
Then use Wireshark to view the file. Suppose one goes knocking, easy is wrong, usually you have to try a few times to succeed. But suppose you write a bat script:
:: Tcpdump.bat@echo offset Dir=/sdcardset file=capture.pcapadb shell rm-r%dir%/%file%adb Shell Tcpdump-p-S 0-i any-w %dir%/%file% @echo On::gettcpdump.bat@echo offset dir=/sdcardset file=capture.pcapset wireshark= "C:\Program Files\ Wireshark\wireshark.exe "del/q%file%adb remountadb pull%dir%/%file%.%wireshark%%file% @echo on
The first script, Tcpdump.bat, is crawling, and the second script is looking at the crawled data, and I can easily see it every time I just need to execute these two commands:
D:\>tcpdumppress ctrl-c to stop Capturingd:\>gettcpdump
After that, Wireshark will open the data that is being crawled and look directly at it.
3. Where can I use the bat
No matter what needs to be done repeatedly, it is possible to try to use the script to complete. For example, the daily version number is updated, the files are copied, the commands are used every day, and so on. As mentioned earlier, you should consider using bat only if you want to do something more than a second time, or do it dozens of times a day. Or, some commands are very complex and use bat, even if it is worthwhile to synthesize two simple commands into a bat. For example, when debugging to see the data in the phone, you can use the script to complete:
:: Getmmsdb.bat@echo offset Datadir=/data/data/com.android.providers.telephony/databasesset mmsdb=mmssms.dbdel/q% MMSDB%ADB remountadb pull%datadir%/%mmsdb%. sqlite3%mmsdb% @echo on
Since the bat is capable of writing commands that can be executed in the cmd command line, in addition, very many applications also provide a command version number, such as WinRAR, if you need to copy something from somewhere and unzip it, you can use bat:
:: Copyversion.bat@echo offdel/q stable.rarset winrar= "C:\Program files\winrar\winrar.exe" copy/y \\192.168.0.1\ Version\release\stable.rar.%winrar% x Stable.rar@echo on
In any case, this is more convenient than doing it manually.
In addition, *nix has a lot of useful command-line programs with the version number of Windows, such as subversion, it has a version number is dedicated to the command line, so the installed software and some built-in commands to combine, A tool that can create very powerful automation.
Another is the expansion of third-party tools, Windows itself has very few built-in commands, but there are third-party tools to expand it, such as PowerShell, and these tools, can make the program Ape's toolbox more rich and powerful.
In fact, the most critical place here is not bat, or whether you can write this script, but whether you realize that the bat script can replace the manual. Can read "The productive Programmer" and "pragmatic Programmer" to get some inspiration.
4. How to use Bat
BAT is a scriptable script, which means that it runs with just double-clicking it, like a *.exe can run a program. The other way is to start it through CMD on the command line, but it needs to give its path, that is, to let the interpreter find it, so the recommended way is to put the written bat collection into a directory, and then put the file into the system environment variable path, This makes it possible to use it in whatever path.
For example, my configuration is this, put all the bat under D:\work\bin, and then add the D:\work\bin to the path variable.
The way to add an environment variable is to right-click My Computer--Properties--advanced--environment variables--Edit the path in the user variable (assuming no new one is added) and then add the D:\work\bin to it, separated from its path with a semicolon; It can then be executed like a built-in command. (Assuming you have permission, you can also change the system variable).
5. How to write Bat0. Editing of files
It is a text file, so you can edit it with the simplest notepad. Of course, you can use other powerful tools, such as the assumption that the Vim editor can also have syntax highlighting and other VIP-level treatment. No matter what the editor, be sure to keep in mind that the file extension must be remembered as *.bat, otherwise it is no longer a scriptable script, we are programmers, you know.
As mentioned earlier, Bat is actually a combination of commands in CMD or other commands that can be run, so the cornerstone of BAT is the command, which focuses on the commands below.
1. Basic commands and their common rules
The basic command referred to here is the built-in command in the cmd command, which is the command displayed by the Help command, which is not included in the environment variable and the third party, those are external commands.
The main commands are file operations, directory operations, time, and so on. For example: Del, Ren, RD, CD, copy, Xcopy, time, and so on.
A. General format of the command
As with the command of the *nix system, its format is also
command [options] [arguments]
Options are the act of controlling and altering commands, each of which must start with a/, followed by options, be able to write multiple options together, and be able to open with a blank, each option can also be followed by their own parameters. Arguments is the number of parameters passed to the command, which is the object to be run. Such as:
DEL/F/q/s tmp
Here/f/q/S is three options/F means forced deletion,/q is quiet mode, that is, not prompted before deletion,/s is to delete subfolders, and TMP is the object to delete is the del command.
dir/on tmp
Here/on is its option, where/O is the option, meaning sort, and N is the number of options, which together is sorted by name.
Typically, a command has default options and parameters, that is, you do not specify, and there are. There are different options for different commands for detailed options.
B. Some common options:
Although different commands have different parameters, there are several common:
- /? Display the Help information, all the commands have (as with the *nix-h)
- /s contains subfolders for folder operations (as with *nix-R)
- /q Quiet mode, that is, some actions do not prompt, for example, delete, or overwrite the file do not prompt
c. Getting Help
It's impossible to remember everything, so get the help information whenever you encounter a command.
Help command
Or
Command/?
will be able to see the command's help information. With the help command, you can view the CMD command-line assistance, which is what built-in commands it supports. It is important to note that help can only look at the built-in and display-built commands. For those commands in the environment variable path, it is not possible to get help information, and for those commands, only the command itself to find help information.
2. Extended Commands
The cmd command line is a non-built command that exists in the environment variable path, and these commands are not different from internal commands and can be called directly in CMD. These commands are usually provided by a third party or operating system, complete with certain areas of functionality, such as C:\Windows\System and C:\Windows\System32 a very many of the running programs can be used as external commands, For example, if you enter Notepad directly in the command line and press ENTER, Notepad will open. and installed third-party programs such as WinRAR.
These programs may support the command pattern, perhaps not, this goes to the documentation for the program, and also, even if it supports command lines, options and parameters.
3. Gaze and @ and echo off
Staring at a behavior that begins with two colons in bat:: You can only gaze at a line and must start at the beginning of a line
Such as:
:: This is a comment
:: Copy a file to somewhere
@ As with echo off, it is a special token for DOS batches and is used only to mask command-line echoing. The @ can only affect the current line, and echo off is off until echo on turns the back line on. Two are primarily used in bat scripts and are not visible in the command line directly. Each time a command is run in bat, the command is displayed in the cmd command, for example:
:: Mydir.bat
Dir/on
This is what the script will do when it executes:
D:\>mydir
D:\>dir/on
(Show contents of directory)
In order to run the script without displaying the current command, it is necessary to turn off the back row display function @ and echo off, so almost every bat has these two lines
@echo off
....
@echo on
Close the back row display before you start the script, and then open it at the end. The purpose of the @ is to turn off the display of the return line of the command to display the return line (sorry, a bit around).
Why do you want to turn off the back row display? Because the return line display is mixed with the output of the command being run, it looks very uncomfortable, especially if the bat script has a long command, so close it.
4. Piping
Like *nix, the bat is also capable of using pipelines to combine commands using pipelines, for example, the most common way to use them is:
Type LogFile.txt | More
View in Split screen.
5. Definition and use of variables
Use the SET command in bat to define a variable, and then use the two percent sign to refer to the variable, where the reference is completely replaced with the value defined, such as:
D:\>set logfile= "D:\logs\log.txt" D:\>echo%logfile%d:\logs\log.txt
6. redirection
Like *nix, the bat also supports file stream redirection.
- < redirect input streams, most often using files instead of standard input
- > Redirect Output stream, the most frequently used way is to put the program running results in a file
- >> redirect output stream, add mode, that is, from the end of the file to join, most often used is to save log with
Although Windows seems to be a far cry from *nix (especially in previous years), BAT has some important features (plumbing and redirection) similar to *nix and shell, so bat is still quite powerful, even though it can't be compared to the shell, But at least it can help us to finish a lot of mechanical and repetitive work.
7. Command-line Parameters
Like the programming language (c, C + +, Java, Perl) and the shell, BAT also supports command-line parameters, which is the number of parameters given when the command is run. Use%[1-9] in the bat to refer to them, such as:
:: Mytype.bat
@echo off
Type%1
Type%2
Then, at the time of execution,
C:\>mytype One.txt Two.txt
Where%1 is one.txt,%2 or two.txt.
Like the C and C + + languages, another%0 can use it as the name of the script.
8. Start command
In the shell of *nix, Command & can be used in the background to execute commands, that is, a new process to execute the command, without clogging the following commands. Sometimes this is useful, for example, if you want to open several xlsx files with bat, such as the following script:
:: Openxls.bat@echo offset excel= "C:\Program Files\Microsoft Office\office12\excel. EXE "%excel%" D:\documents\status.xlsx "%excel%" E:\report\report.xlsx "@echo on
This will be problematic, execution to open the first file will stop there, unless you turn off status.xlsx otherwise, it will jam there, the second file will not be executed to.
The solution is to open a new process for each blocked command, with the start command:
:: Openxls.bat@echo offset excel= "C:\Program Files\Microsoft Office\office12\excel. EXE "Start%excel%" D:\documents\status.xlsx "Start%excel%" E:\report\report.xlsx "@echo on
In this case, two files will be opened.
9. The shortcomings of Bat
It's all about how powerful bat is, but after all, Windows doesn't have the power to give command-line support like *nix, which is far worse than the shell, and may not be much different in running commands and file operations, but in some ways it's very weak:
A. Processing of inputs
Although it is possible to redirect the input stream with <, it handles the input very weakly, especially if it does not read the command (read in the shell), and it cannot read the file in rows. So when it comes to file processing, it seems to be out of reach.
B. The ability to process text
As stated in the previous article, it is very often necessary to read the content from a text file and then do some processing on it. The bat on this point is very weak and cannot be compared with the shell. Because the shell itself has a flexible read function, able to read by line, can also be a line of different words to read, plus its own string processing function, there is *nix has a wealth of string processing commands such as grep, sed, ed, awk and so on. Of course, these things now have the version number of Windows. But using bat to process files is still a rather troublesome thing.
For text processing, it is best to write in a procedural script such as Perl, which has very good support in handling input, reading and processing, and on the form.
10. Start your own active execution
Some software will have internal options to achieve power on their own initiative to execute, and even it always boot self-implementation, such as the famous QQ, Thunder storm, audio and video and so on. For me these are rogue software, especially those who can not cancel their boot self-executing, will cause the very card when booting.
However, we need to open some fixed software, such as e-mail client,eclipse, open a specific directory, open a specific page, and so on. This decent software is not "boot self-executing" option, how to do? At this point the bat comes in handy, can write a called Startup.bat, in the inside to execute the program to write, and then in the Start menu startup directory to build a shortcut to our Startup.bat:
:: Startup.bat@echo off "C:\WINDOWS\explorer.exe" "C:\Documents and Settings\user\ desktop \alex" set ie= "C:\Program files\ Internet Explorer\iexplore.exe "Set mail=" https://webmail.microsoft.com/"Start%ie%%mail%start" C:\Program files\ Microsoft Office\office12\excel. EXE "" D:\documents\report.xlsx "Start mstsc d:\work\rdps\linux. Rdpstart mstsc d:\work\rdps\windows. Rdpddms.batclean.batd:\eclipse-java-galileo-sr2-win32\eclipse.exe@echo on
Each tool has its advantages and disadvantages, we have to do is to use the right tools to do the right thing, in order to achieve the most efficient work efficiency. Despite the more powerful tools such as Perl, there are powertools and other outreach tools. But when you have environmental constraints, for example, because of the limitations of your work environment, you only have a Windows system, and when you don't have Perl or powertools, you can only use bat. This time will play the power of bat, complex it can not do, but simple it is still able to be very good competent.
Script, strong or limited, is to help us solve some of the recurring problems. So the key is not the script, but that we have to find the repetitive work in the daily work, and then replace with a script. Remember, the computer is far more repetitive than people, and the computer is designed to replace the human do some mechanical repetitive work, this is its mission, let it complete its mission!
Reference information: This article describes the specific
Basic introduction
"Stone Notes Batch File"
These two buddies have written a very multi-action string of bat
"Dos-string Operations"
"Dos-string Manipulation"
In layman Windows BATCH