繁体   English   中英

如何使用批处理检查文件是否存在

[英]How to check if a file is existing or not using batch

我写了一个脚本来检查文件是否存在并向我发送邮件。 但是它不能正常工作。 非常感谢您提供任何建议或解决方案。我的脚本如下。

SET file1=E:\Program.* 

Set vLogFile=C:\logfile1.log 
if exist %file1% goto fileexists 
goto nofile 

:fileexists 
echo "File exist" > %vLogFile% 
--my required mail sending info here-- 
goto end 
:nofile 
echo "No file exist" > %vLogFile% 
--my required mail sending info here-- 
goto end 

你有:end标签吗?

如果使用goto :EOF而不是goto end则不需要标签。

您需要使用所有双引号的更好语法:

@echo off
setlocal
SET "file1=E:\Program.*"

Set "vLogFile=C:\logfile1.log" 
if exist "%file1%" goto :fileexists 
goto :nofile 

:fileexists 
>"%vLogFile%" echo "File exist"
--my required mail sending info here-- 
goto end 
:nofile 
>"%vLogFile%" echo "No file exist"
--my required mail sending info here-- 
goto end 

我的问题解决了。 下面是我进行更改后的脚本,现在按照我的要求它可以正常工作。

SET file1=Program 
Set vLogFile=C:\logfile1.log 
cd \ 
e: 
dir %file1% /s /b > %vLogFile% 
if errorlevel 1 ( goto mail1) else (goto mail2) 

:mail1 
--my code of lines for sending mail-- 
goto END 

:mail2 
--my code of lines for sending mail-- 
goto END 

:END

您在评论之一中提到File类型。 如果我对它的理解正确,那么您正在谈论的是Windows资源管理器或“属性”对话框所示的文件类型。 但是,在编写批处理文件时,更容易处理文件扩展名 现在,简单地称为File的文件类型告诉我,您正在尝试验证该文件的存在的文件没有扩展名 ,即,其名称仅为Program ,而不是Program.something

但是,你正在使用的面膜IF EXIST命令, Program.* ,将同时匹配Program不带扩展名和Program用任意扩展,即Program.txtProgram.exeProgram.I.cannot.imagine.what.else等。也许,在E:\\目录中有一个不同于File的类型的Program文件,这导致脚本始终调用:fileexists分支。

如果要检查特定Program的存在,只需按原样指定名称,不要使用掩码Program.* 换句话说,更改此行

SET file1=E:\Program.*

对此

SET file1=E:\Program

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM