-1
@ECHO OFF
CLS

SET found=0
SET found1=0
REM ECHO Enter the file extension you want to delete...
REM SET /p ext="\> "
SET  ext=txt
SET  ext1=xlsx

IF EXIST \*.%ext% ( rem Check if there are any in the current folder :)
DEL \*.%ext%
SET found=1

)

FOR /D /R %%G IN ("\*") DO (    rem Iterate through all subfolders
    IF EXIST %%G CD %%G
    IF EXIST \*.%ext% (
        DEL \*.%ext%
        SET found=1
    )
)

IF %found%==1 (
    ECHO.
    ECHO Deleted all .%ext% files.
    ECHO.
) ELSE (
    ECHO.
    ECHO There were no .%ext% files.
    ECHO Nothing has been deleted.
    ECHO.
)

%------THIS PART NOT FUNCTIONING------%

IF EXIST \*.%ext1% (    rem Check if there are any in the current folder :)
DEL \*.%ext1%
SET found1=1

)

FOR /D /R %%G IN ("\*") DO (    rem Iterate through all subfolders
    IF EXIST %%G CD %%G
    IF EXIST \*.%ext1% (
        DEL \*.%ext1%
        SET found1=1
    )
)

IF %found1%==1 (
    ECHO.
    ECHO Deleted all .%ext1% files.
    ECHO.
) ELSE (
    ECHO.
    ECHO There were no .%ext1% files.
    ECHO Nothing has been deleted.
    ECHO.
)

%-------------------------------------%

PAUSE
EXIT

Hi to all expert. How to allow the 2nd part not functioning to become functional?

Compo
  • 36,585
  • 5
  • 27
  • 39
Paul Ang
  • 13
  • 4
  • What do you mean by "it's not working?" What are you expecting it to do, and what is it actually doing? – SomethingDark Aug 18 '23 at 03:09
  • 1
    Are you aware that `\*.%ext%` means "all of the files matching `*.%ext%` **in the root directory** ? – Magoo Aug 18 '23 at 03:33
  • Also, `del /s directoryname\filename` will delete *filename* (which may contain `*` or `?`) in *directoryname* and all its subdirectories? Note - it is a dangerous command - be sure that is what you want to do before executing it. – Magoo Aug 18 '23 at 03:36
  • 3
    Your "happy faces" _DO_ close the `IF` commands: `IF EXIST \*.%ext% ( rem Check if there are any in the current folder :)` **<-** This IF contains just one `rem` command. The following commands does _NOT_ belong to the "then" part of this IF – Aacini Aug 18 '23 at 04:37
  • I recommend reading the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) and [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) and the DosTips forum topic [ECHO. FAILS to give text or blank line - Instead use ECHO/](https://www.dostips.com/forum/viewtopic.php?f=3&t=774) – Mofi Aug 18 '23 at 05:16
  • There is used best `ECHO(` instead of `ECHO.` because of `ECHO(` always results in output of an empty line __without__ making any file system access as done by `ECHO.` because of `cmd.exe` searches in this case for a file with name `ECHO` in the current directory. – Mofi Aug 18 '23 at 05:20
  • If you made the effort to indent your own code, _(as I have done for you now)_, you'd have more clearly seen the problems. I'd also recommend that you use an editor which highlights matching parentheses as you walk through the code. – Compo Aug 18 '23 at 09:37

0 Answers0