Assignment invocation parameters for batch processing

Source: Internet
Author: User

Excerpt from: http://blog.chinaunix.net/uid-15014334-id-3015867.html

3Assignment Invocation Parameters3.1Assign Value3.1.1Assigning a value to a literal string to a variable is the first to understandSetThis order.SetThis command is more complex, and at the command prompt, typeSet/? There is also a lot of help information available. But, to put it simply, usingSetYou can simply assign a value to the variable by following the variable and then using the equal sign = string. For exampleSetVar=hello world!. To confirm that the value of the variable var is Hello world! , you can useSetvar to see the value of the variable var. UseSetV to see all the values of the variable starting with the letter V. Direct inputSetYou can view the values of all variables. In addition, the variable side plus a percent sign% is used to represent the value of the variable (content). This allows the value of the variable to be assigned to other variables or used for processing such as calculation display. 3.1.2Assigning A numeric value to a variable isSetA parameter that is appended with/A can give the variable a numeric value, such asSet/A var=48 indicates that the number 48 is assigned to variable var. The numeric variable is a 32-bit integer value, which occupies 4 bytes, can represent a number of 2 32, including the sign, the range is: -2147483648~2147483647. 3.1.3The assignment of input from the outside is given inSetAfter adding the/p parameter, you can set the variable to one line input by the user. Displays the specified hint text before reading the input line. Of course, the hint text can also be empty. Like whatSet/P var= Please enter some text:, can display a paragraph of hint text "Please enter some text:" and can put the user input information into the variable var. The parameters of/p also have many functions, such as substitution, extraction, increment and subtraction of strings, can refer toSetinformation about the help. 3.1.4variable value of the given, display, transformation, calculation and other functions may be at this time some friends of percent percent of the understanding is still in a state of confusion, we can do some experiments. First, as the previous article says, assign a variable called var hello world! (in the command line, enterSetVar=hello world! )。 And then we're going to assign the value in Var to another variable, var1:Setvar1=%var%, at this time the value in Var1 is also Hello world! The If the percent% is not used, it is onlySetVar1=var, then the value of the variable VAR1 is only 3 letters of the Var. Look again.EchoThe use of, respectively, to enterEchoVar1 andEcho%var1%, the resulting return outputs are: Var1 and Hello world!, respectively. In addition, a percent semicolon can effectively edit or transform a string in a variable. It is worth mentioning the substitution function, which is formatted as follows: The name of the original variable followed by a colon: plus the content you want to replace, followed by an equal sign=, and then add the new content to replace, and finally use two percent sign to include the above. Although the value of the original variable does not change at this time, the contents of the percent sign can be assigned to a variable, which can be a primitive variable. Cases:Echo%var: o=z%, the effect is to put all the letter O in the Hello world! is replaced by the letter Z, and displayed, but the variable var value does not change. Of course, we're not satisfied with just replacing a single letter--sometimes we need to replace two. SetVar2=%var: LD=ms and bugs%, this command can be put in the Hello world! The LD is replaced by MS and bugs and the new result is assigned to the variable var2, Var still does not change. InputEcho%var2% Confirm If the result is the Hello worms and bugs! we expect. The deletion of a string in a variable at a particular location would be in such a format:%var:~m% and%var:~m, n%. m and n are integer parameters. A number m is a positive number to take the variable var from the left to the first m characters (single-byte characters)in the future, M is a negative number that represents the variable var from the right-hand side of the utilises and all the characters on the right, which is the new string produced by the first command. If the number n is positive, it means that in the new string above, the contents of n characters are taken from the left side, and if n is negative, the character is taken from the left side until it is left with a-n character. If you insist that this abstract expression is simply unable to explain how this damned m and shameless n is going to be, it is better to experiment with the following example. To make it easier to see the effect, we assume that the contents of the variable var are1234567890 (Setvar=1234567890), and then enter the following commands in turn and view the results. Input command result effectEcho%var% 1234567890Show AllEcho%var:~4% 567890Show starting from 4th characterEcho%var:~4,3% 567starts from the 4th character and displays only the first 3Echo%var:~-4% 7890start with the 4th character from the bottom of the displayEcho%var:~-4,3% 789start with the last 4th character and show only the first 3Echo%var:~4,-2% 5678starting from the 4th character, shown until 2 leftEcho%var:~0,3% 123display from the beginning and display only the first 3 charactersEcho%var:~0,-3% 1234567displayed from the beginning, showing up to 3 characters left in addition,SetIt is also possible to perform common operations on numeric variables. Set/A num=48Set/A result=%num%+12Echo%result% The above command indicates the first value48 assigns the variable num, and then assigns the value of the variable num to the value 12 to the variable result. The 3rd line can show the value of the variable result, and the result is obviously 60.. 3.2as with many programming classes, batching does not necessarily have to be executed in the order in which the commands in the text are arranged in a row. If you encounter aGoto、Pager、StartThis kind of jump, call, start and other statements, the program will often become multi-layered, execution will be more effective. 3.2.1JumpGotoIt must not be an incomprehensible thing for a friend who has a little bit of programming foundation.GotoFollowing the tag allows the program to proceed from the tag to continue with the subsequent command, regardless of where the label is located in theGotoThe front or back of the command. Labels must begin with a single colon: but are not case-sensitive. There's a special label.: EOFOr: EOFTo transfer control to the end of the current batch script file, it is not required to be defined beforehand. :::::::::::: " goto. Bat: ::::::::..@echo offGoto : Firstlable: SecondlableEchoand then show this sentencePauseGoto : EOF: FirstlableEchofirst show this sentencePauseGoto : Secondlable::::::::::::::::::::::::::::::::3.2.2calledPagerIt is mainly embodied in two aspects: one is to call another batch other than the batch (in fact, call the batch itself can also, but may bring unnecessary dead loops);GotoSimilar to the ability to jump to a specific label. HoweverPageris unique in that the control continues to execute after the processing of the called batch or label has completedPagerThe following statement. Let's start by looking atPagerThe effect of jumping. In order to facilitate the comparison, we will make the above batch processing as follows (the light text is the modified part). :::::::::::: " goto. Bat: ::::::::..@echo ifPager : Firstlable: SecondlableEchoand then show this sentencePauseGoto : EOF: FirstlableEchofirst show this sentencePause::goto:secondlable::::::::::::::::::::::::::::::::In usePagerJump to: FirstlabelAt the end of the program (at this pointPagerTask has just been completed) and will continue to return toPagerStatement after the: SecondlabelSecretariat IfGoto : SecondlabelIf the sentence is not commented out, then the control jumps to: SecondlabelPlace untilGoto : EOFSecretariatPagerMission is truly complete. AndPagerAfter completing the task, the following: SecondlabelThe content will be executed again. WhenPagerAs a function of calling other new batches, the current batch is paused until the new batch finishes, before the batch continues. For example, call a batch in the current path directlyPagerTest.bat, or the batch to invoke is in the current path up-level ABC folderPager.. \abc\test.bat, you can also use an absolute path to find the target batchPagerD:\abc\test.bat (refer to the [Preface] command in the pathCDIntroduction to usage). Of course, one of the most intuitive examples tends to be more illustrative of the problem. Create two batches (assuming they are under the same path) "Call. Bat" and "called. Bat", their contents are::::::::::::: call. Bat: ::::.:::::.@echo offEchoHere is the call.BatPausePageris called.BatEchoNow it's back to the call.BatPause::::::::::::::::::::::::::::::::::::::::::: called. bat. :::::::::EchoHere is called.BatPause::::::::::::::::::::::::::::::::Edit and save the two batches, and then run the batch "call. Bat", the results are exactly the same as we expected. 3.2.3StartStartAlthough it is not a simple command, the usage is absolutely not difficult to understand. Here are a few examples:StartMsconfig is used to open the "System Configuration Application";StartNotepad can open a notepad;Start"This is called the title."cmdUsed to open a new command prompt;Start"Write a title casually" Http://www.baidu.com will open the homepage of Baidu;Start"Play" E:\game\starcraft\starcraft.exe is the beginning of StarCraft (if you have the star in your computer and the path is consistent with the above) and so on. AlthoughStartA lot of parameters (specific usage in the input helpStartcan be obtained later), but usually we just need to knowStartAdd the title, followed by the program, command, or URL you want to execute. It is important to note that the caption is quoted in double quotation marks, otherwise it will be handled as an executable file, and if it is not a system internal program or command, we need to give a specific path, such as an absolute path. Of course,StartYou can also open a different batch. This seems to be withPagerSimilar, but there are some differences. In order to comparePagerAndStart, you can modify the "call. Bat" in the previous example.Pageris called. Bat forStartCalled. bat. "called. bat" after starting execution from "call. Bat"can indeed be called. However, the previous batch process also continues to execute at the same time. In fact, at this point the two programs are completely independent and are two separate processes. 3.3The parameters mentioned above are called, and here we have to add the parameters. What is a parameter? If you want to use the commanddir, the path file name that follows is good,/P (page display),/w (wide list display) or/s (all directories and subdirectories), these can be called as parameters. For a command, it can return (output) to us in addition to some information, we also often give it some information, the input information is parameters; However, for a batch, it can work for us (output results), there is a need for some information from outside. 3.3.1parameter passing in order to be very well aware of this, let's first revise a little bit of the"called. Bat"This batch file allows it to accept input parameters that are processed by the outside world. ::::::::::: called. bat. :::::::::EchoHere is called.BatEchoThe 1th parameter you entered is%1EchoThe 2nd parameter you entered is%2Pause::::::::::::::::::::::::::::::::which%1And%2Represents the two parameters that are followed when the "called. bat" Batch is run. So how does it get the so-called parameter 1 and parameter 2? Double-click Run "called. Bat" is of course not. You can enter the full name of "called. bat" at the command prompt and add two parameters to it (if "called. bat" is not in the current working path, you need to enter the "called. bat" path, such as an absolute path, so that the computer will find it). Just like: d:\ batch \test\ is called. Bat Tom and Jerry. At run time we will find%1And%2Shown separately as Tom and and, Jerry is handled as the 3rd parameter, but is not used in the batch%3。 Tip: In operating systems such as XP, the input to Chinese characters can be Ctrl +The space switches out the Chinese input method, or you can press the TAB key to automatically switch and replenish the path you want to enter. To further verify this, let's change the batch process"Call. Bat", let it add parameters when calling other batches:::::::::::: call. Bat: ::::.:::::.@echo offEchoHere is the call.BatPausePageris called.bat Hello world!EchoNow it's back to the call.BatPause::::::::::::::::::::::::::::::::3.3.2the input and output parameters of a parameter can be not only a value, but also a variable. If it is a variable, it can be either an input or an output. To do this, we need to change it separately."Call. Bat" and "called. Bat". :::::::::::: call. Bat: ::::.:::::.@echo offEchoHere is the call.BatPauseSet/P num=Please enter an integer:SetSquare=Pageris called.bat Num SquareEchoNow it's back to the call.bat, and the square of%num% is%square%. Pause::::::::::::::::::::::::::::::::::::::::::: called. bat. :::::::::EchoHere is called.BatEchoThe 1th parameter you entered is%1EchoThe 2nd parameter you entered is%2Set/A%2=%1*%1EchoAfter calculation, the 1th parameter you enter is%1EchoAfter calculation, the 2nd parameter you enter is%2Pause::::::::::::::::::::::::::::::::As you can see, in the batch "called. Bat" in thePagercalled. Bat num Square, where the variables Num and square are passed past just the variable name, not the value in the variable. In addition, try to "call. Bat" in thePagerCalled. Bat Num Square in exchange forPageris called.bat%num% Square, and then compare the results, I believe you will have something to gain. 3.3.3when a function is called inside a batch, what happens when the parameter is used? So this is the embryonic form of the function. Put the previous"Jump. Bat"change it a little bit .:::::::::::: " goto. Bat: ::::::::..@echo ifPager : FirstlableVery good, very powerful.: SecondlableEchoand then show this sentencePauseGoto : EOF: FirstlableEchoFirst show this sentence, followed by the argument is%1Pause::::::::::::::::::::::::::::::::ForGotoThe tag you are following orStartFollowed by the program, they can add parameters? The answer is: the former is negative; the latter is yes, try it. 3.4PostScript wrote for so many days, maybe we should come up with something more practical and more useful. A few days ago inadvertently discovered a 163 mailbox login, here as an example to introduce. ::::::: 163 Mailbox Login. Bat: :::.:::@echo off::do not display the command lineModeCon cols=32 lines=8::set the size of the windowTitle 163 Mailbox Login::set the title of the batchColor 3a::sets the color of the background (dark Indigo) and text (bright green). ::Also, enter color/at the command prompt to get detailed color informationSet/P Username=Account Number:Set/P password=Password:::give the input hint and assign the string to the corresponding variableStart"Signing in to Mailbox" "https://reg.163.com/logins.jsp?username=%username%&password=%password%&url=http:// Fm163.163.com/coremail/fcg/ntesdoor2 "::in fact, that URL link is the core part of the whole program, note that%username% and%password% are the string contents of variables username and password respectively. 

Assignment invocation parameters for batch processing

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.