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
******************
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:
syntax now becomes:
IF NOT EXIST FILENAME Command
For Example,
IF NOT EXIST c:\mystylepro.doc ECHO It doesn't Exist
****************
For Example,
IF NOT EXIST c:\mystylepro.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.
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.