Windows 7 Forums

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
 
 
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 SpecsSystem Spec
.
14 May 2015   #2
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 SpecsSystem Spec
14 May 2015   #3
Thyagi

Windows 7 Enterprise32 bit
 
 

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 SpecsSystem Spec
.

14 May 2015   #4
MilesAhead

Windows 7 32 bit
 
 

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 SpecsSystem Spec
15 May 2015   #5
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by Thyagi View Post
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 SpecsSystem Spec
20 May 2015   #6
Thyagi

Windows 7 Enterprise32 bit
 
 
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 SpecsSystem Spec
Reply

 Window Batch file to delete folders/subfolders that contain files




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
How to get folders to sort both subfolders and files?
When I sort my folders using "date modified," instead of ordering everything, I instead get all of the subfolders sorted, and then below that everything else, so it looks like this: FOLDER A: 10th Jan FOLDER B: 7th Jan FOLDER C: 5th Jan Randomfile.png: 12th Jan Randomfile.mp3: 11th Jan...
General Discussion
Script to seach folders & subfolders for files containing keywords
Hello, I need help creating a basic script which will search C: drive, and all its subfolders for all files containing keywords in the filename. I would like the script to pull the keyword filenames from a list. For example, I will have a text file named list.txt, in this list it will...
Performance & Maintenance
[Help] Batch File move every 15.000 files to another folders
Hi 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 files
Was 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 folders
I 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
Make folders/subfolders on external for image files?
Hi. Curious. Do you make folders/subfolders on the external drive for the image files to identify them from the others? Such as month/date/partition, etc..? I know macrium dates and label them as well. Dont plan on putting anything else on the extrernal..yet. Can I move the ones I've already...
Backup and Restore


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 05:56.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App