zoukankan      html  css  js  c++  java
  • windows xp 批处理

    assoc

    显示或修改文件扩展名关联
    ASSOC [.ext[=[fileType]]]
     .ext      指定跟文件类型关联的文件扩展名
     fileType  指定跟文件扩展名关联的文件类型
    键入 ASSOC 而不带参数,显示当前文件关联。如果只用文件扩展
    名调用 ASSOC,则显示那个文件扩展名的当前文件关联。如果不为

    文件类型指定任何参数,命令会删除文件扩展名的关联。 

    Ftype 

    显示或修改用在文件扩展名关联中的文件类型
    FTYPE [fileType[=[openCommandString]]]
      fileType  指定要检查或改变的文件类型
      openCommandString 指定调用这类文件时要使用的开放式命令。
    键入 FTYPE 而不带参数来显示当前有定义的开放式命令字符串的
    文件类型。FTYPE 仅用一个文件类型启用时,它显示那个文件类
    型目前的开放式命令字符串。如果不为开放式命令字符串指定,
    FTYPE 命令将删除那个文件类型的开放式命令字符串。在一个
    开放式命令字符串之内,命令字符串 %0 或 %1 被通过关联调用
    的文件名所代替。%* 得到所有的参数,%2 得到第一个参数,
    %3 得到第二个,等等。%~n 得到其余所有以 nth 参数打头的
    参数;n 可以是从 2 到 9 的数字。例如:
        ASSOC .pl=PerlScript
        FTYPE PerlScript=perl.exe %1 %*
    允许您启用以下 Perl 脚本:
        script.pl 1 2 3

    A more general version with arguments

    The file that we have been discussing is limited to listing one particular folder and putting the list in one particular file. However, it is easy to make the file able to list whatever folder we want and to put the list wherever we want. Batch files can use arguments or data that is input from the user. The process makes use of placeholders of the form %1, %2, These are replaced in the script by our input data. This type of situation cannot be clicked directly but should be run in a command prompt. The new batch file would be@echo offdir %1 > %2Enter in Notepad and save as "makelist.bat". To run the file, open a command prompt and enter{path}makelist somefolder somewhere\list.txtwhere somefolder is whatever folder (with complete path) that you want to list in somewhere\list.txt. Now you have a little program that will list the contents of a folder whenever you want. If you want a list of all the subfolders as well, use the commanddir /s %1 > %2If you want a list that only includes files of a certain type, MP3 files for example, usedir %1\*.mp3 > %2The line above illustrates the use of the wildcard "*". The ability to use wildcards greatly enhances the power of batch files.

    Life will be easier if you put all batch scripts in a folder that is in the path environment.

    The Rem statement

    Very often batch files contain lines that start with "Rem". This is a way to enter comments and documentation. The computer ignores anything on a line following Rem. For batch files of any complexity, comments are a good idea. Note that the command interpreter actually reads Rem statements so using too many can slow down execution of a script.

    More examples

    Following the discussion on another page, it is easy to create batch files for some typical maintenance. To create a very simple backup script, use xcopy. The code might bexcopy %1 %2 /d /sThis will update all files in the input source folder %1 and its subfolders by copying to the backup folder %2. In practice, a useful backup script would probably need a few more of the switches discussed at xcopy.

    Again following previous discussion of the command "del", a file to delete all temporary files with extension TMP might containdel %1\*.tmp

    Prompting for user input

    You can also interact with a user and ask that data be entered. The old DOS had a "Choice" command for very limited interaction but that has been superseded in Windows XP/Vista by the more versatile "set /p". The syntax is:set /p variable= [string]"Variable" is the name of the variable that will be assigned to the data that you want the user to input. "String" is the message that the user will see as a prompt. If desired, "string" can be omitted. Here is an example that asks the user to enter his or her name:set /p name= What is your name?This will create a variable %name% whose value is whatever the user enters. Note that the user must press the "Enter' key after typing the input.

    (The "Choice" command has returned as a more powerful version in Vista.)

    Conditional branching with "If" statements

    Batch files can make decisions and choose actions that depend onconditions. This type of action makes use of a statement beginning with "If". The basic meaning of an "If" statement isIf something is true then do an action (otherwise do a different action)The second part of the statement (in parentheses) is optional. Otherwise, the system just goes to the next line in the batch file if the first condition isn't met. The actual syntax isIf (condition) (command1) Else (command2)The "Else" part is optional. The form "If not" can also be used to test if a condition is false. Note that "If" tests for true or false in the Boolean sense.

    "If exist" statement

    There is a special "If exist" statement that can be used to test for the existence of a file, followed by a command. An example would be:If exist somefile.ext del somefile.extYou can also use a negative existence test:

    if not exist somefile.ext echo no file 

    "If defined" statement

    Another special case is "if defined ", which is used to test for the existence of a variable. For example:if defined somevariable somecommandThis can also be used in the negative form, "if not defined".

    "If errorlevel" statement

    Yet another special case is "if errorlevel", which is used to test the exit codes of the last command that was run. Various commands issue integer exit codes to denote the status of the command. Generally, commands pass 0 if the command was completed successfully and 1 if the command failed. Some commands can pass additional code values. For example, there are five exit codevalues for Xcopy. These exit code values are stored in the special variable errorlevel. An example command would be:if errorlevel n somecommandwhere "n" is one of the integer exit codes. Note that the comparison is done by checking if errorlevel is greater than or equal to n. If used with "not" the comparison checks if errorlevel is less than n.

    Table I. Comparison operators in"If' statements
    OperatorMeaning
    EQUequal to
    NEQnot equal to
    LSSless than
    LEQless than or equal to
    GTRgreater than
    GEQgreater than or equal to

    Comparison operators

    In some cases the condition to be met is obtained by comparing strings. For exampleif string1 == string2Note that the "equals" sign is written twice. This condition is met if the two strings are exactly identical, including case. To render comparisons insensitive to case, use the switch "/i". For more general comparisons, use the operators in Table I. (The operators are given in upper case in the table but they are not case-dependent.) Numerical comparisons only work with all-digit strings. Otherwise, the comparison is done alphabetically. For example "a" is less than "b". For case independence, use the switch "/i". An example command might read:if /i string1 gtr string2 somecommand

    When comparing variables that are strings, it may be best to enclose the variable name in quotes. For example, use:if "%1" == somestring somecommand

    The "goto" command

    Generally, the execution of a batch file proceeds line-by-line with the command(s) on each line being run in turn. However, it is often desirable to execute a particular section of a batch file while skipping over other parts. The capability to hop to a particular section is provided by the appropriately named "goto" command (written as one word). The target section is labeled with a line at the beginning that has a name with a leading colon. Thus the script looks like...
    goto :label
    ...some commands 
    :label
    ...some other commands
    Execution will skip over "some commands" and start with "some other commands". The label can be a line anywhere in the script, including before the "goto" command.

    "Goto" commands often occur in "if" statements. For example you might have a command of the type:if (condition) goto :label

    The "End of File" (:eof) label for exiting a script

    Sometimes it is desirable to terminate a script if a certain condition is met (or not met). One way to exit is to use the special label :eofin a goto command. The label is not actually placed in the batch file. Windows XP and later recognize :eof without any label explicitly placed at the end of the batch file. Thus if you need to test for a particular condition that makes script termination desirable, you can write:if (condition) goto :eofNote that this terminates the script but does not necessarily close the command shell.

    Looping with "If" and "Goto"

    An ancient method for doing repetitive tasks uses a counter,"if" statements, and the "goto" command. The counter determines how many times the task is repeated, the "if" statement determines when the desired number of repetitions has been reached, and the "goto" command allows for an appropriate action to be executed, either the repetitive task or exiting. Generally, the more elegant methods provided by the powerful "for...in...do" command are preferable and they are discussed on the next page. However, for completeness and to illustrate some of what we have discussed, I will give an example that uses the clumsier method.

    The simple script below creates the numbers 1 to 99 and sends them to a file. It uses the "set" command to create a variable that is also the counter for how many times we have iterated.@echo off
    set /a counter=0
    :numbers
    set /a counter=%counter%+1
    if %counter% ==100 (goto :eof) else (echo %counter% >> E:\count.txt) 
    goto :numbers
    (Best programming practice would dictate that the variable %counter% be localized or destroyed at the end but for simplicity I have omitted the several extra lines needed to do that. As written, this environment variable would persist until the command shell itself, not just the script, was closed.)

    In anticipation, I can note that the same result as the script above can be achieved with a two-line script using the "for" statement discussed on the next page:@echo off

    for /l %%X in (1,1,99) do (echo %%X >> E:\count.txt) 

    The very useful "for...in...do" statement is discussed

    Computers are very good at doing the same thing over and over. The command line contains a powerful and versatile method for carrying out this type of operation. With this method, you can automate many time-consuming tasks. The basic statement is of the form:for {each item} in {a collection of items} do {command}(For those who persist in calling the command line DOS, note that the 32-bit version of the "For" statement is much more powerful than the old 16-bit DOS version.)

    A single-letter replaceable variable is used to represent each item as the command steps through the the collection (called a "set"). Note that, unlike most of Windowsvariables are case-dependent. Thus "a" and "A" are two different variables. The variablehas no significance outside the "For" statement. I will be using X throughout the discussion but any letter will do. (In principle, certain non-alphanumeric characters can also be used but that seems like a bad idea to me.) The variable letter is preceded with a single percent sign when using the command line directly or double percent signs in a batch file. Thus the statement in a batch file looks like this:for %%X in (set) do (command)What makes the "For" statement so powerful is the variety of objects that can be put in the set of things that the command iterates through, the availability of wildcards, and the capability for parsing files and command output. A number of switches or modifiers are available to help define the type of items in the set. Table I lists the switches. They are listed in upper case for clarity but are not case-sensitive.

    Table I. Modifying switches used with FOR
    SwitchFunction
    /DIndicates that the set contains directories.
    /RCauses the command to be executed recursively through the sub-directories of an indicated parent directory
    /LLoops through a command using starting, stepping, and ending parameters indicated in the set.
    /FParses files or command output in a variety of ways

    I will consider a number of examples that illustrate the use of "For" and its switches.

    Simple iteration through a list

    The set of things that are to used can be listed explicitly. For example, the set could be a list of files:for %%X in (file1 file2 file3) do command(Care must be taken to use correct paths when doing file operations.) A different example where the set items are strings is:For %%X in (eenie meenie miney moe) do (echo %%X)Wildcards can be also be used to denote a file set. For example:for %%X in (*.jpg) do commandThis will carry out the command on all files in the working directory with extension "jpg". This process can be carried further by using several members in the set. For example to carry out a command on more than one file type use:for %%X in (*.jpg *.gif *.png *.bmp) do command

    As always, keep in mind that the command line may choke on file names with spaces unless the name is enclosed correctly in quotes. Therefore, you might want to use "%%X" in the "command" section.

    Looping through a series of values

    The well known action of stepping through a series of values that was discussed in connection with "if" and "Goto" statements is succinctly done with the switch /l (This switch is an "ell", not a "one") . The statement has the form:for /l %%X in (start, step, end) do commandThe set consists of integers defining the initial value of X, the amount to increment (or decrement) X in each step, and the final value for X when the process will stop. On the previous page, I gave an example batch file that listed all the numbers from 1 to 99. If we use a "For" statement, that task can be accomplished with one line:for /l %%X in (1,1,99) do (echo %%X >> E:\numbers.txt)The numbers in the set mean that the initial value of X is 1, X is then increased by 1 in each iteration, and the final value of X is 99.

    Working with directories

    If you wish to use directories in the variable set, use the switch /d. The form of the command isfor /d %%X in (directorySet) do commandAn example that would list all the directories (but not sub-directories) on the C: drive isfor /d %%X in (C:\*) do echo %%X

    Recursing through sub-directories

    If you want a command to apply to the sub-directories as well as a parent directory, use the switch /r. Then the command has the form:for /r [parent directory] %%X in (set) do commandNote that you can designate the top directory in the tree that you want to work with. This gets around the often cumbersome problem of taking into account which is the working directory for the command shell. For example the statement:for /r C:\pictures %%X in (*.jpg) do (echo %%X >> E:\listjpg.txt)will list all the jpg files in the directory C:\pictures and its sub-directories. Of course, a "dir" command can do the same thing but this example illustrates this particular command.

    Parsing text files, strings, and command output

    Now we come to a truly powerful switch that was not even dreamed of back in the DOS days of yore. The switch /f takes us into advanced territory so I can only indicate the many aspects of its application. Things become rather complex so those who are interested should consult programming books or the Microsoft documentation. However, here is a brief sketch of what's involved.

    This version of the "For" command allows you to examine and parse text from files, strings, and command output. It has the formfor /f [options] %%X in (source) do command

    "Options" are the text matching criteria and "source" is where the text is to be found. One of the interesting applications is to analyze the output of a command or commands and to take further action based on what the initial output was. 

  • 相关阅读:
    关于 下载 nfs-utils时的 gssproxy conflicts with selinux-policy-3.13.1-102.el7.noarch 错误
    SCP命令
    DHCP服务
    NFS服务
    ssh免密登录
    可见性判断
    (八)图像处理
    (八)图像处理
    (八)Grahpics之Blit
    (七)时间动画_Time
  • 原文地址:https://www.cnblogs.com/jjkv3/p/2489774.html
Copyright © 2011-2022 走看看