2017-02-03 98 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測試將立即生效。

相關問題