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

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

 Similar help and support threads Thread Forum batch del files in subfolders only certain file existHi all, I just want a batch file to delete all files with extension *.P3? in all subfolders on condition only some file exist in these subfolders like *.pkg file C:\test\abc\*.P3A, *.pkg C:\test\def\*.P3L C:\test\ghi\*.P3P, *.pkg So the script should only delete *.P3A in abc folder and... General Discussion Move files from original folder to existing folders - w batch file?My files have a format of "ID_date_time.d7d” Ex: 11111-5_2013_10_07_142149.d7d The ID is 1111-5 and was tested 10/7/2013 at 2:21:49 PM and the file extension is always .d7d The local files are automatically saved here by the program generating the data: C:\Local\Data I want them copied... General Discussion [Help] Batch File move every 15.000 files to another foldersHi guys I'm new here, please help I have about 2 million email files that i want to convert into mbox files. but the tool i used only can convert every 15.000 files. so i need a batch file that can move this 2 million files into another folder (15.000 files each folder) and the folder name... General Discussion How do I write batch file to delete thousands of filesWas infected by the matsnu1 virus recently which encrypes and locks all your data files. Got all my files back by running matsnu1decryp.exe You need an original clean file before the virus struck for it to work out how to decrypt all your other files does does save the day. Down side is that you... Software a simple batch file for copy files and foldersI want to write a batch file that do this for me: - makes a directory with the "yy.mm.dd.hh.mm" as the directory name, - copies files and folders from a TEST directory to above destination, - copies only new (or modified) files from the TEST directory to a separate folder. Any Idea? General Discussion

 Our Sites Site Links About Us Find Us Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp. © Designer Media Ltd All times are GMT -5. The time now is 15:54.