开发者

How to check if a file exists from inside a batch file [duplicate]

This question already has answers here: 开发者_如何学Python How to verify if a file exists in a batch file? (3 answers) Closed 9 years ago.

I need to run a utility only if a certain file exists. How do I do this in Windows batch?


if exist <insert file name here> (
    rem file exists
) else (
    rem file doesn't exist
)

Or on a single line (if only a single action needs to occur):

if exist <insert file name here> <action>

for example, this opens notepad on autoexec.bat, if the file exists:

if exist c:\autoexec.bat notepad c:\autoexec.bat


C:\>help if

Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command


Try something like the following example, quoted from the output of IF /? on Windows XP:

IF EXIST filename.txt (
    del filename.txt
) ELSE (
    echo filename.txt missing.
)

You can also check for a missing file with IF NOT EXIST.

The IF command is quite powerful. The output of IF /? will reward careful reading. For that matter, try the /? option on many of the other built-in commands for lots of hidden gems.  

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜