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

Thyagi

New member
Local time
9:44 PM
Messages
3
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 Computer My Computer

At a glance

Windows 7 Enterprise32 bitCore 2 Duo4 GB
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Enterprise32 bit
CPU
Core 2 Duo
Motherboard
Intel
Memory
4 GB
Antivirus
Symantec
Browser
Chrome,Mozilla,IE
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 Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly,

You script works perfectly.Thank you :thumbsup:

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 Computer My Computer

At a glance

Windows 7 Enterprise32 bitCore 2 Duo4 GB
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Enterprise32 bit
CPU
Core 2 Duo
Motherboard
Intel
Memory
4 GB
Antivirus
Symantec
Browser
Chrome,Mozilla,IE

My Computer My Computer

At a glance

Windows 7 32 bitAMD 5200+ dual core2 GBNVidia GeForce 6150SE 128 MB
Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
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 Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Window Batch file to delete folders/subfolders that contain file

Hi Pyprohly,

Apologies for the delay in responding.

You script works perfectly.Thank you :thumbsup:
 

My Computer My Computer

At a glance

Windows 7 Enterprise32 bitCore 2 Duo4 GB
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Enterprise32 bit
CPU
Core 2 Duo
Motherboard
Intel
Memory
4 GB
Antivirus
Symantec
Browser
Chrome,Mozilla,IE
Back
Top