1

Looking to delete folders (subfolders as well) with a certain size (150mb). The script I need would have to search multiple folders on different drives. For example

delete folders under 150mb in E:temp/ , D:temp/ ,F:temp/

Thanks again for the help. I do not mean to waste your time, I did search everywhere and tried to make my own script but failed.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Alex
  • 13
  • 2
  • 1
    Show the script you have tried as sign of your own effort! – geisterfurz007 Feb 10 '17 at 12:24
  • Dim objFD Set objFD = CreateObject("Scripting.FileSystemObject") Set objSelectedFolder = objFD.GetFolder("I:\Movies") Set colSubfolders = objSelectedFolder.SubFolders For Each objSubfolder In colSubfolders If objSubfolder.Size < 150000000 Then objSubfolder.Delete True End If Next – Alex Feb 10 '17 at 12:26
  • 1
    Folders don't have a size, the size reported is a cumulative value for all of the files contained within. For that reason you should reformulate your question to state exactly what it is you're intending to do when you append your code. – Compo Feb 10 '17 at 12:31
  • 1
    @Alex Please [edit] the code into your question. And please note that this is not batch but rather VBScript (I think). – geisterfurz007 Feb 10 '17 at 12:35
  • yes, i know..Im very new to this – Alex Feb 10 '17 at 12:51

1 Answers1

0
@echo off
set "150mb=157286400"
set "root_dir=E:\RootDir"
setlocal enableDelayedExpansion
:: recursive listing of the folders.
for /d /r  "%root_dir%\" %%# in ("*") do (
    rem echo %%~f#
    call :getSize %%#
    rem echo !size!
    if !size! equ !150mb! (
        echo rd /s /q "%%~f#"
    )
)



exit /b 0
:getSize    
    setlocal enableextensions disabledelayedexpansion

    set "target=%~1"
    if not defined target set "target=%cd%"

    set "size=0"
    for /f "tokens=3,5" %%a in ('
        dir /a /s /w /-c "%target%"
        ^| findstr /b /l /c:"  "
    ') do if "%%b"=="" set "size=%%a"
    endlocal & (
        set size=%size%
    )

folder size function was shamelessly stolen from here . Mind that in pure batch you can get the size of file/folder only in bites - size is hardcoded at the beginning of the script but you can change it. On this line echo rd "%%~f#" the target folder is only echoed. You'll need to delete the echo to make it work.

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • thanks for your help npocmaka, I have to learn batch language badly..its so useful. – Alex Feb 10 '17 at 13:12