1

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?

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • 5
    `set /p del = Type E...` remove the spaces around `=`. – DavidPostill Nov 04 '18 at 17:29
  • 1
    You have smart quotes in your code. Do not use those. You also have a Delayed Expansion problem. – Squashman Nov 04 '18 at 20:56
  • Please read the answers on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Nov 05 '18 at 05:55

1 Answers1

1

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Io-oI
  • 2,514
  • 3
  • 22
  • 29