2017-02-03 73 views
0

我在写这个批处理脚本时遇到了这个小问题。问:如果批处理脚本中存在/如果不存在

:Delete_File 
cls 
title Delete File 
echo Welcome to the Utility Delete File! 
echo Type the name of the file you want to delete: 
echo. 
set /p var= 
echo. 
echo I'm checking the existance of the file... 
ping 192.0.2.2 -n 1 -w 2000>nul 
if EXIST %var% del %var% 
if NOT EXIST %var% echo The file doesn't exist. Check for errors and try again. 
ping 192.0.2.2 -n 1 -w 1500>nul 
set /p answer=1 - Go to Menu _ 2 - Go to Exit 
if %answer%==1 goto Menu 
if %answer%==9 goto Exit 

所以,问题是,当文件建立时,它被删除,但它显示在第12个字符串的消息。 我需要显示的文件被成功删除的消息!”重定向你的菜单......

echo The file was successfully deleted! 
echo Redirecting you to the Menu... 
ping 192.0.2.2 -n 1 -w 2000>nul 
goto Menu 

我对这个问题的语法错误感到抱歉,但我新。 感谢每一个响应我得到 费德里科

+0

您的意思是第12行代码。下面的答案解决了你的问题,但想想你正在使用的逻辑,你可以自己解决它。翻转线。 – Squashman

回答

0
if exist %var% (
    del %var% 
) else (
    echo The file doesn't exist. Check for errors and try again. 
) 
+0

忘了说,因为我在XP 2001上,我没有else语句...... –

+1

@ F.Tinelli,Windows XP当然有'IF ELSE'功能。 – Squashman

+1

@ F.Tinelli,'else'条款包含在'NT'中。你拥有了它。 –

0

如果您有其他问题,您可以使用多个标签,如:!

if NOT EXIST %var% goto :DeleteFileMissing 
del %var% 
Goto :DeleteFileContinue 

:DeleteFileMissing 
echo The file doesn't exist. Check for errors and try again. 

:DeleteFileContinue 
1

批次确实不是执行此类项目的最佳语言,但您应该颠倒IF IFIST和IF NOT EXIST行的顺序。一旦在IF EXIST行中删除文件,该文件就不再存在,并且IF NOT EXIST测试将立即生效。

相关问题