1.

Solve : Delete a folder if its over a certain size?

Answer»

Hello all,

I have returned with yet another dilemma for another unrelated project.

I need to delete a folder "F:\Backup\RegistryBackup" if it reaches a size over 1 Gig. I have attempted a round about way of detecting folder size but it will only output the folder size of the folder its currently IN. This as you can see causes a problem. For learning and understanding sake, I will post my entire CODE.

Code: [Select]@ECHO Off
TITLE Registry Backup Maintenance
COLOR F
ECHO *********************************************
ECHO * This Program was written by PPowerHouseK. *
ECHO *********************************************
cd F:\Backup\registrybackup
For /F "tokens=*" %%a IN ('"dir /s /-c | find "bytes" | find /v "free""') do @Set summaryout=%%a
For /f "tokens=1,2 delims=)" %%a in ("%summaryout%") do @set filesout=%%a&set sizeout=%%b
Set sizeout=%sizeout:bytes=%
Set sizeout=%sizeout: =%
ECHO ----------------------------------------------
Echo Size is :%sizeout%
ECHO ----------------------------------------------
IF %sizeout% gtr 100000000 ECHO Warning Backup Registry Size Larger than Allowed. Deleting...
IF %sizeout% gtr 100000000 DEL "F:\Backup\registrybackup"
ECHO ----------------------------------------------
IF %sizeout% lss 100000000 ECHO Registry Backup Size Within Allowed Limits.
ECHO ----------------------------------------------
ECHO Make sure you run the AutoReg.bat after this process.
IF EXIST "F:\Backup\registrybackup" ECHO Registry Still In Tact.
IF NOT EXIST "F:\Backup\registrybackup" ECHO Registry Successfully Deleted.
ECHO ----------------------------------------------
ECHO Proccess Completed.
ECHO ----------------------------------------------
pause

Any relevant suggestions would be excellent.here's a vbscript, that calculates folder size, without the hassle
Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject")
strFolder = "c:\windows"
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume INFORMATION" Then
For Each eFolder in objDIR.SubFolders
FolderSize = eFolder.Size
WScript.Echo FolderSize, "bytes, path: " & eFolder.Path
' Uncomment the below portion to do something to those folders
' If FolderSize > <amount> Then
' do something....
'End If
Go eFolder
Next
End If
End Sub

save as myscript.vbs and on command line:
Code: [Select]c:\test> cscript /nologo test.vbs
Thanks for the speedy response. I have never USED vbscript, so this is quite confusing to me. How would I alter that code to delete the folder if its larger than one gigabyte?
Also, why does it need a command line? Can I not just double click it or refer to it from another batch? Thanks in advance.
Quote from: PPowerHouseK on October 15, 2009, 07:33:05 AM

Thanks for the speedy response. I have never used vbscript, so this is quite confusing to me. How would I alter that code to delete the folder if its larger than one gigabyte?
while i can give you the answer, i want you to find out for yourself how to. Use google.
Quote
Also, why does it need a command line? Can I not just double click it or refer to it from another batch? Thanks in advance.
you can also put that cscript command inside a batch file. Well, I do greatly appreciate your response. I have Very limited time, so i will be unable to rummage through google to find a way with VBscript to achieve that. Since I have tried that with the batch VERSION, I am certain I will just end up on another forum asking someone else, this time in a language I have no foundation to build upon.
Don't get me wrong, I would love to learn another language, but I think its best to stick with Batch, since I have a decent handle on it already.
So if anyone knows how to MODIFY my code , I would greatly appreciate it.Code: [Select]For /F "tokens=*" %%a IN ('"dir /s /-c | find "bytes" | find /v "free""') do @Set summaryout=%%a

Alright, apparently in this segment here you just insert the directory right behind the word dir. Learning is fun. Thanks for the wonderful help.


Discussion

No Comment Found