• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Using IF to Check Whether a File Exists

Using IF to Check Whether a File Exists

Another variation of IF is the IF EXIST command, which checks for the existence of a file. This is handy, for example, when you’re using COPY or MOVE. First, you can check whether the file you want to copy or move exists. Second, you can check whether a file with the same name already exists in the target folder. (As you probably know, a file that has been copied over by another of the same name is downright impossible to recover.) Here’s a batch file called SAFEMOVE.BAT, which uses the MOVE command to move a file but first checks the file and then the target folder:

@ECHO OFF
CLS
IF EXIST %1 GOTO SO_FAR_SO_GOOD
ECHO The file %1 doesn't exist!
GOTO END
:SO_FAR_SO_GOOD
IF NOT EXIST %2 GOTO MOVE_IT
ECHO The file %1 exists on the target folder!
ECHO Press Ctrl+C to bail out or, to keep going,
PAUSE
:MOVE_IT
MOVE %1 %2
:END


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint