Window Batch file to delete folders/subfolders that contain files


  1. Posts : 3
    Windows 7 Enterprise32 bit
       #1

    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 Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    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


  3. Posts : 3
    Windows 7 Enterprise32 bit
    Thread Starter
       #3

    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 Computer


  4. Posts : 5,092
    Windows 7 32 bit
       #4

    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 Computer


  5. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #5

    Thyagi said:
    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


  6. Posts : 3
    Windows 7 Enterprise32 bit
    Thread Starter
       #6

    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 Computer


 

  Related Discussions
Our Sites
Site Links
About 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 08:42.
Find Us