bat script 点滴

1. 两个%括起来的变量,说明是用的变量的值,而不是变量本身。

  如 set version = "1.0"

      echo version //// print verison in console

      echo %version% ////print 1.0 in console

2. For parameter %%variable IN fittler DO do something

      For has four parameters:

     1) /d(only directory, it means this command will work on the direcoty not file),

     2) /l(迭代数值范围 Iterative numerical range) for /l %%i in (start#, step#, end#) do command, will execute command when start#<end#.

     3) /r(recurrence(递归,遍历), example: for /r c:\ %%i in (*.exe) do @echo %%i )

     4) /f (迭代及文件解析)

 eol=c           - specifies an end of line comment character
                          (just one)
        skip=n          - specifies the number of lines to skip at the
                          beginning of the file.
        delims=xxx      - specifies a delimiter set.  This replaces the
                          default delimiter set of space and tab.
        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
        usebackq        - specifies that the new semantics are in force,
                          where a back quoted string is executed as a
                          command and a single quoted string is a
                          literal string command and allows the use of
                          double quotes to quote file names in
                          file-set.

    Some examples might help:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

      %%variable: it may be a-z or A-Z, and it is case sensitive. For will set its value with any what it gets.

     IN: syntax

3. %* : it presente the parameter passed by command line. %1 means the firlst one, and %2 means the second one, and so on.

4. Interact with users: Set /p variable= [prompt string]. It is very important that must have a whiteplace after the "=". You can use %variable% get users input value.

5. The %~dp0 variable : The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file.The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.If you follow the percent character (%) with a tilde character (~), you can insert a modifier(s) before the parameter number to alter the way the variable is expanded. The d modifier expands to the drive letter and the p modifier expands to the path of the parameter. Example: Let’s say you have a directory on C: called bat_files, and in that directory is a file called example.bat. In this case, %~dp0 (combining the d and p modifiers) will expand to C:\bat_files.

 

     

posted @ 2018-01-31 17:14  my-sky  阅读(273)  评论(0编辑  收藏  举报