Unable to delete specific files from main foldes , sub folders and folders under subfolders as well using Batch Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pmpinaki
Posts: 1
Joined: 29 Jun 2022 23:26

Unable to delete specific files from main foldes , sub folders and folders under subfolders as well using Batch Script

#1 Post by pmpinaki » 29 Jun 2022 23:29

I have a folder name Test. Under that there are two subfolders and under each subfolders two more subfolders are there. The main Test folder has dlls and some of the same dlls are present in each of the sub folders and their sub folder as well. Below is the folder structure:
Image
Question: I need to remove certain dlls from Test folder and it’s subfolder as well. For example I need to delete 1.dll, 2.dll, 3.dll wherever these are present in the Test folder using batch script. In batch script code I provided the option for user to provide the input of the path, so that it can be used by anyone based on folder path. Below is my Batch script code.

Code: Select all

@echo off
cls
set /p path=Enter destination folder : 
set list=ActiveReports.Chart.dll ActiveReports.Document.dll ActiveReports.HtmlExport.dll
set back=%cd%


Rem echo %list%[0]

set count = 0
set notfounddlls=""
REM for %%a in (%list%) do echo(del /s/q %var%\%%a)

for /d /r %%d in (%path%\*) do (
    rem set test = %%d
    echo --------------------------
    rem echo %%d
    rem echo --------------------------
    
    pushd  %%d
    cls
    
    rem for %%i in %list% do (echo %%i)
    (for %%a in (%list%) do ( 
        rem echo %%a 
        IF exist %%a (
          set count =1. 
          del %%a.
          Echo %%a deleted
        ) ELSE (
          set count = 0.
          ECHO  %%a file not found in %%d.
          rem set notfounddlls=%notfounddlls%;%%a
          rem ECHO Nothing has been deleted from %%d.
          rem ECHO.
        )
    
    ))
    cd
     
    cd %back%
    
)


pause
However it does not delete the required files. Can someone look in to the code and check if any mistake I made.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Unable to delete specific files from main foldes , sub folders and folders under subfolders as well using Batch Scri

#2 Post by aGerman » 30 Jun 2022 12:18

FWIW Do not overwrite the predefined path variable.
Besides of that I think your attempt is quite overcomplicated. This should do the trick:

Code: Select all

@echo off
set "list=ActiveReports.Chart.dll ActiveReports.Document.dll ActiveReports.HtmlExport.dll"
set /p "dest=Enter destination folder: "
pushd "%dest%" || exit /b
del /f /s /a %list%
popd
pause

Post Reply