问:如果EXIST / IF不存在于批处理脚本中

时间:2017-02-03 19:57:21

标签: batch-file

我在编写批处理脚本时遇到了这个小问题。

: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

我对这个问题的错误语法感到非常抱歉,但我是新的。 感谢我得到的每一个回复! 费德里科。

3 个答案:

答案 0 :(得分:1)

Batch实际上不是执行此类项目的最佳语言,但您应该颠倒IF EXIST和IF NOT EXIST行的顺序。删除IF EXIST行中的文件后,该文件不再存在,并且之后立即执行IF NOT EXIST测试将成功。

答案 1 :(得分:0)

/Users/autoboxer/development/learning-vue/src/app.vue
    12:3  error  Missing space after key 'name'  key-spacing

/Users/autoboxer/development/learning-vue/src/main.js
    6:2  error  Missing space after key 'el'  key-spacing

/Users/autoboxer/development/learning-vue/test/unit/specs/Hello.spec.js
    7:4  error  Missing space after key 'el'  key-spacing

答案 2 :(得分: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