Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

# Windows 7: Window Batch file to delete folders/subfolders that contain files

 13 May 2015 #1 Thyagi Windows 7 Enterprise32 bit 3 posts Window Batch file to delete folders/subfolders that contain files I have a root folder called "Names"..It has subfolders ,these subfolders contains files. I will be giving the names of subfolders to be deleted in text file Ex.. John Tom Rambo. I will be reading the folder names from text file & delete only those folders with files inside the folder also . Please provide me the code for it..I tired some POC's..But it didn't work. My System Specs
 .
 14 May 2015 #2 Pyprohly Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan 722 posts In a house Hi, Here's some sample batch script, Code: @echo off if "%~1"=="" exit /b 1 pushd "%~dp0" for /f "usebackq delims=" %%I in ("%~1") do ( if exist "%%~fI"\* rd /s /q "%%~fI" && echo Removed folder '%%~nxI'. ) popd Place the above batch file in with the folders that will be removed (i.e. place it inside the "Names" folder), then drop your text file list of folder names to be deleted onto the batch file. You might want to test out this solution first. My System Specs
 14 May 2015 #3 Thyagi Windows 7 Enterprise32 bit 3 posts Hi Pyprohly, You script works perfectly.Thank you I was looking into other solution parallely.. I am planning to specify the path of the folder in the batch file EX. C:\Temp\RootFolder I tried with the below script ,but it deletes all the folders under the root folder Set RootDir=C:\Temp\RootFolder Set UserList=C:\UserList.txt for /F "tokens=*" %%u in (%UserList%) do ( for /F "tokens=*" %%f in ('dir /a:d /s /b %RootDir%\%%u') do ( echo Deleting folder "%%f..." rd /s /q "%%f" ) ) can you please let me know what is wrong with the above script. My System Specs
 .
 14 May 2015 #4 MilesAhead Windows 7 32 bit 5,147 posts Here is a program done in AHK that reads files/folders from a text file and deletes them: http://www.donationcoder.com/forum/index.php?topic=20153.msg179761#msg179761 My System Specs
 15 May 2015 #5 Pyprohly Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan 722 posts In a house Quote: Originally Posted by Thyagi I tried with the below script ,but it deletes all the folders under the root folder Deletes all folders under the root folder? Code: Set RootDir=C:\Temp\RootFolder Set UserList=C:\UserList.txt for /F "tokens=*" %%u in (%UserList%) do ( for /F "tokens=*" %%f in ('dir /a:d /s /b %RootDir%\%%u') do ( echo Deleting folder "%%f..." rd /s /q "%%f" ) ) This script appears to remove all subfolders of each of the folder names mentioned in the UserList.txt file, in contrast to what the problem question asks: remove the folder names mentioned in UserList.txt. This script is not a solution to the question being asked. Furthermore, the script will break should a path with spaces be used as the RootDir/UserList, or if a folder name mentioned in the UserList.txt file has a space in it. Also, the Dir command shouldn't have an '/s' switch. Here is another version of the batch script I posted above. This uses variables as the input method instead of dragging and dropping a text list onto the batch file. Code: @echo off set RootDir=C:\Temp\RootFolder set UserList=C:\UserList.txt pushd "%ROOTDIR:"=%" for /f "usebackq delims=" %%I in ("%USERLIST:"=%") do ( if exist "%%~fI"\* rd /s /q "%%~fI" && echo Removed folder '%%~nxI'. ) popd My System Specs
 20 May 2015 #6 Thyagi Windows 7 Enterprise32 bit 3 posts Window Batch file to delete folders/subfolders that contain file Hi Pyprohly, Apologies for the delay in responding. You script works perfectly.Thank you My System Specs