批处理-要求输入时,程序退出

时间:2018-11-04 17:20:27

标签: batch-file cmd

我有一个程序应该在给定目录中搜​​索文件,然后为您提供删除文件的选项。码:

@echo off

set counter=0
set counter2=0

setlocal enableextensions 
setlocal enabledelayedexpansion

:menu
cls
echo.
echo ---------------------------------------------------------------------------------------------------
echo.
echo ----------------------------------------Search By File Type----------------------------------------
echo.
echo ---------------------------------------------------------------------------------------------------

set /p inputE= Please type the file extension (ex, txt): 
set /p inputP= Please enter location to search (ex, C:\): 
::'where /r %inputP% *.%inputE%' 

for /f "tokens=*" %%a in ( 
    'where /r %inputP% *.%inputE%'   
) do ( 
    echo !counter! - %%a
    set file!counter!=%%a
    set /a counter+=1
)

set /a counter -= 1

set fileDel=file!counter!
echo !%fileDel%!

:delete
set /p del = Type E to exit, R to make another search, A to delete all items, or the array number to delete a specific number: 

set "var="&for /f "delims=0123456789" %%i in ("%del%") do set var=%%i 

if %del% == E (
    exit
) else if %del% == A (
    echo this does not work yet
    goto delete
) else if %del% == R (
    goto menu
) else if defined var (
    echo %del% is not an answer, please enter a new answer
    goto delete
) else (
    set /p finalC= Are you sure you want to delete %del%? (Y/N)
    if %finalC% == Y (
        set “fileDel=file%del%”
        del !%fileDel%! /q
        goto end
    ) else if %finalC% == N (
        goto delete
    )
)


:end
set /p next= Type E to exit, D to delete another item, or R to make a new search: 
if %next% == R goto menu
if %next% == D goto delete
if %next% == E exit

代码将一直工作到到达:delete部分。在那里,无论输入什么,它都会退出程序。为什么会这样呢?删除文件的代码是否可以正常工作?

1 个答案:

答案 0 :(得分:1)

您是否有义务在变量名中使用deldel是内部cmd.exe命令行的命令。

echo/Type E to exit, R to make another search, A to delete all items, or the array number to delete a specific number: & set /p _erase=

set /p _del=

for /f "delims=0123456789" %%i in ('@echo/%_del%') do set var=%%i 

可以处理 var 而无需退出 maybe