⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batch file programming.txt

📁 1000 HOWTOs for various needs [WINDOWS]
💻 TXT
📖 第 1 页 / 共 3 页
字号:
CD\

CD %1

DEL %2



This script can be called from the DOS prompt in the following way:



C:\windows>batch_file_name windows\temp *.tmp



In a single script we cannot use more that nine replaceable parameters. This 

means that a particular batch file will have replaceable parameters from %1 to 

%9.Infact there is a tenth replaceable parameter, the %0 parameter. The %0 

parameter contains the name of the batch file itself.



************

HACKING TRUTH: Say you want to execute a batch file and once the procedure of 

execution is complete, want to leave DOS and return to Windows, what do you do? 

The EXIT command can be used in such situations. So simply end your batch file 

with the EXIT command.

EXIT

************



SHIFT: Infinite Parameters



Sometimes your batch file program may need to use more than nine parameters at a 

time.(Actually you would never need to, but at least you are sure you can handle 

it if you need to.)To see how the SHIFT command works, look at the following 

snippet of code:



@ECHO OFF

ECHO The first Parameter is %1

ECHO.

SHIFT

ECHO The Second Parameter is %1

ECHO.

SHIFT

ECHO The Second Parameter is %1



Now execute this batch file from DOS and see what happens.



C:\windows>batch_file_name abc def ghi



The first Parameter is abc



The Second Parameter is def



The Second Parameter is ghi



How does it work? Well, each SHIFT command shuffles the parameters down one 

position. This means that after the first SHIFT %1 becomes def, %2 becomes ghi 

and abc is completely removed by DOS. All parameters change and move one position 

down.



Both normal parameters (%1 , % 2 etc) and the SHIFT command can be made more 

efficient by grouping them with the IF conditional statement to check the 

parameters passed by the User.



THE FOR LOOP



The syntax of the FOR LOOP is:



FOR %%PARAMETER IN(set) DO command



Most people change their mind about learning Batch Programming when they come 

across the syntax of the For Command. I do agree that it does seem a bit weird, 

but it is not as difficult as it appears to be. Let's analyze the various parts 

of the For command. Before we do that look at the following example,



@ECHO OFF

CLS

FOR %%A  IN (abc, def, xyz) DO  ECHO  %%A



Basically a FOR LOOP declares a variable (%%A) and assigns it different values 

as it goes through the predefined set of values(abc, def, xyz) and each time 

the variable is assigned a new value, the FOR loop performs a command.(ECHO %%A)



The %%A is the variable which is assigned different values as the loop goes 

through the predefined set of values in the brackets. You can use any single 

letter character after the two % sign except 0 through 9.We use two %'s as DOS 

deletes each occurrence of a single % sign in a batch file program.



The IN(abc, def, xyz) is the list through which the FOR loop goes. The variable 

%%a is assigned the various values within the brackets, as the loop moves. The 

items in the set(The technical term for the set of values within the brackets) 

can be separated with commas, colons or simply spaces.



For each item in the set(The IN Thing) the FOR loop performs whatever command is 

given after the DO keyword.(In this example the loop will ECHO %%A)



So basically when we execute the above batch file, the output will be:



abc

def

xyz



The FOR loop becomes very powerful if used along with replaceable parameters. Take 

the following batch file, for example,



@ECHO OFF

ECHO.

ECHO I am going to delete the following files:

ECHO %1 %2  

ECHO.

ECHO Press Ctrl+C to Abort process

PAUSE

FOR %%a IN (%1 %2 ) DO DEL %%a

ECHO Killed Files. Mission Accomplished.



At execution time, the process would be something like:





C:\WINDOWS>batchfilename  *.tmp *.bak



I am going to delete the following files:

*.tmp *.bak



Press Ctrl+C to Abort process

Press any key to continue . . .



Killed Files. Mission Accomplished.

----------------------------------



IF: CONDITIONAL BRANCHING



The If statement is a very useful command which allows us to make the batch files more intelligent and useful. Using this command one can make the batch programs check the parameters and accordingly perform a task. Not only can the IF command check parameters, it can also checks if a particular file exists or not. On top of all this, it can also be used for the conventional checking of variables (strings).



Checking If a File Exists Or Not



The general syntax of the IF command which checks for the existence of a file is the following:



IF [NOT] EXIST FILENAME Command



This will become clearer when we take up the following example,



IF EXIST c:\autoexec.bat ECHO It exists



This command checks to see if the file, c:\autoexec.bat exists or not. If it does then it echoes or prints the string 'It exists'. On the other hand if the specified file does not exist, then it does not do anything.



In the above example, if the file autoexec.bat did not exist, then nothing was executed. We can also put in the else clause i.e. If the File exists, do this but if it does not exists, by using the GOTO command. Let's consider the following example to make it more clear:



@echo off

IF EXIST C:\ankit.doc GOTO ANKIT

Goto end

:ANKIT

ECHO ANKIT

:end



The IF statement in this code snippet checks to see if there exists a file, c:\ankit.doc. If it does then DOS is branched to :ANKIT and if it does not, then DOS goes on to the next line. The next line branches DOS to :end. The :end and :ANKIT in the above example are called labels. After the branching the respective echo statements take over.



******************

HACKING TRUTH: We can also check for more than one file at a time, in the following way:

IF EXIST c:\autoexec.bat IF EXIST c:\autoexec.bak ECHO Both Exist

******************



We can check to see if a file does not exist in the same way, the basic syntax now becomes:



IF NOT EXIST FILENAME Command



For Example, 



IF NOT EXIST c:\ankit.doc ECHO It doesn't Exist



****************

HACKING TRUTH: How do you check for the existence of directories? No something like IF C:\windows EXISTS ECHO Yes does not work. In this case we need to make use of the NULL device. The NULL device is basically nothing, it actually stands for simply nothing. Each directory has the NULL device present in it. (At least DOS thinks so.) So to check if c:\windows exits, simply type:



IF EXIST c:\windows\nul ECHO c:\Windows exists.



One can also check if a drive is valid, by giving something like:



IF EXIST c:\io.sys ECHO Drive c: is valid.



****************



Comparing  Strings to Validate Parameters



The basic syntax is:



IF [NOT] string1==string2 Command 



Now let's make our scripts intelligent and make them perform a task according to what parameter was passed by the User. Take the following snippet of code for example,



@ECHO off

IF %1==cp GOTO COPY

GOTO DEL

:COPY

Copy %2  a:

GOTO :END

:DEL

Del %2

:END



This example too is pretty much self explanatory. The IF Statement compares the first parameter to cp, and if it matches then DOS is sent to read the COPY label else to the DEL label. This example makes use of two parameters and is called by passing at least two parameters.



We can edit the above example to make DOS check if a parameter was passed or not and if not then display an error message. Just add the following lines to the beginning of the above file.



@ECHO OFF

IF "%1" == "" ECHO Error Message Here



If no parameter is passed then the batch file displays an error message. Similarly we can also check for the existence of the second parameter.

This command too has the NOT clause.



The CHOICE Command



Before we learn how to make use of the CHOICE command, we need to what error levels really are. Now Error levels are generated by programs to inform about the way they finished or were forced to finish their execution. For example, when we end a program by pressing CTRL+C to end a program, the error level code evaluates to 3 and if the program closes normally, then the error level evaluates to 0. These numbers all by themselves are not useful but when used with the IF ERROR LEVEL and the CHIOCE command, they become very kewl.



The CHOICE command takes a letter or key from the keyboard and returns the error level evaluated when the key is pressed. The general syntax of the CHOICE command is:



CHOICE[string][/C:keys][/S][/N][/T:key,secs]



The string part is nothing but the string to be displayed when the CHOICE command is run. 



The /C:keys defines the possible keys to be pressed. If options are mentioned then the default Y/N keys are used instead. 

For example, The command,

   

CHOICE /C:A1T0



Defines A, 1, T and O as the possible keys. During execution if the user presses a undefined key, he will hear a beep sound and the program will continue as coded.



The /S flag makes the possible keys defined by the CHOICE /c flag case sensitive. So it means that if the /S flag is present then A and a would be different.



The /N flag, if present shows the possible keys in brackets when the program is executed. If the /N flag is missing then, the possible keys are not shown in brackets. Only the value contained by STRING is shown.



/T:key,secs defines the key which is taken as the default after a certain amount of time has passed.

For Example,

 

CHOICE Choose Browser /C:NI /T:I.5



The above command displays Choose Browser[N,I] and if no key is pressed for the next 5 seconds, then it chooses I.



Now to truly combine the CHOICE command with the IF ERROR LEVEL command, you need to know what the CHOICE command returns.



The CHOICE command is designed to return an error level according to the pressed key and its position in the /C flag. To understand this better, consider the following example,



CHOICE /C:AN12



Now remember that the error level code value depends on the key pressed. This means that if the key A is pressed, then the error level is 1, if the key N is pressed then the error level is 2, if 1 is pressed then error level is 3 and if 2 is pressed then error level is 4. 



Now let us see how the IF ERROR LEVEL command works. The general syntax of this command is:



IF [NOT] ERRORLEVEL number command.



This statement evaluates the current error level number. If the condition is true then the command is executed. For Example,



IF ERRORLEVEL 3 ECHO Yes



The above statement prints Yes on the screen if the current error level is 3.

The important thing to note in this statement is that the evaluation of an error level is true when the error level us equal or higher than the number compared.

For Example, in the following statement,



IF ERRORLEVEL 2 ECHO YES



The condition is true if the error level is > or = 2.



Now that you know how to use the CHOICE and ERROR LEVEL IF command together, you can now easily create menu based programs. The following is an example of such a batch file which asks the User what browser to launch.





@ECHO OFF

ECHO.

ECHO.

ECHO Welcome to Browser Selection Program

ECHO.

ECHO 1. Internet Explorer 5.5 

ECHO 2. Mozilla 5

ECHO x. Exit Browser Selection Program

ECHO.

CHOICE "Choose Browser" /C:12x /N

IF ERRORLEVEL 3 GOTO END

IF ERRORLEVEL 2 START C:\progra~1\Netscape

IF ERRORLEVEL 1 start c:\progra~1\intern~1\iexplore.exe

:END



NOTE: Observe the order in which we give the IF statements.



Redirection



Normally the Output is sent to the screen(The standard STDOUT)and the Input is read from the 

Keyboard(The standard STDIN). This can be pretty boring. You can actually redirect both the Input and the 

Output to something other than the standard I/O devices.



To send the Output to somewhere other than the screen we use the Output Redirection Operator, > which is 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -