如何从批处理文件中检查文件是否存在

发布于 2024-10-06 05:58:43 字数 49 浏览 0 评论 0原文

仅当某个文件存在时我才需要运行实用程序。如何在 Windows 批处理中执行此操作?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骑趴 2024-10-13 05:58:43
if exist <insert file name here> (
    rem file exists
) else (
    rem file doesn't exist
)

或者在一行上(如果只需要执行一个操作):

if exist <insert file name here> <action>

例如,如果文件存在,这将在 autoexec.bat 上打开记事本:

if exist c:\autoexec.bat notepad c:\autoexec.bat
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
歌枕肩 2024-10-13 05:58:43
C:\>help if

在批处理程序中执行条件处理。

IF [NOT] ERRORLEVEL 编号命令

IF [NOT] string1==string2 命令

IF [NOT] EXIST 文件名命令

C:\>help if

Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command

耳钉梦 2024-10-13 05:58:43

请尝试类似下面的示例(引用自 Windows XP 上 IF /? 的输出):

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

您还可以使用 IF NOT EXIST 检查丢失的文件。

IF 命令非常强大。 IF /? 的输出将有助于仔细阅读。就此而言,请在许多其他内置命令上尝试使用 /? 选项,以获得许多隐藏的宝石。
 

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.  

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文