Andy Levine
Andy Levine

Reputation: 45

Batch - When asking for input the program exits

I have a program that should be searching for files in a given directory, then give you the option to delete them. code:

@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

The code works until it gets to the :delete section. when there, no matter what the input is it exits the program. Why is this happening? and should the code to delete a file work?

Upvotes: 1

Views: 115

Answers (1)

Io-oI
Io-oI

Reputation: 2565

Are you obligated to use del in the variable name? del is a command of internal cmd.exe command line.

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=

or

set /p _del=

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

can handle the var without exiting, maybe?

Upvotes: 1

Related Questions