how to check file exists or not : batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rajnishjc_27
Posts: 21
Joined: 16 Aug 2019 23:35

how to check file exists or not : batch script

#1 Post by Rajnishjc_27 » 19 Sep 2019 05:19

hi all,

how to check file exists or not in folder with IF...Else condition.
please help

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: how to check file exists or not : batch script

#2 Post by ShadowThief » 19 Sep 2019 19:32

Code: Select all

If exist file.txt (
    echo File exists
) else (
     echo File does not exist
 )

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: how to check file exists or not : batch script

#3 Post by Squashman » 27 Sep 2019 12:02

The help file gives some pretty clear information on how to do that.

Code: Select all

C:\>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT               Specifies that Windows should carry out
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.

  string1==string2  Specifies a true condition if the specified text strings
                    match.

  EXIST filename    Specifies a true condition if the specified filename
                    exists.

Post Reply