Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript to compare 2 folder with familliar names and delete the small

Status
Not open for further replies.

thyr

IS-IT--Management
Feb 6, 2011
1
DE
Hi, im new here and please excuse my horrible english, but I need help with VBscripting.
The VBS schould get the size of a folder, the folder name is given
and consists of a number and a keyword.
Now the script should look if there are any other folder in the
directorey with the same keyword in its name, but an other mumber.
if the found folder is smaller then delete it if not then give out an error.

for example:
folder1: 123test 56GB
folder2: 754test 54GB

123 is the number and test is the keyword and the number and the keyword should be given as arguments to the vb script.

Usually I did this with a Batch file that i wrote but know i have the problem that the size of the folder and the files in that folder are bigger than 2 GB and Batch can only handle 32 Bit #.

My script:
@echo off
setlocal enabledelayedexpansion
set size = 0
set key=test
set date=212

if exist *%key%\. for /d %%x in (*%key%) do (if not %%x==%date%%key% set oldf=%%x & goto testen)
goto end

:testen
for /R %oldf% %%F in (*) do (
set /a value=%%~zF
set /a oldsize=!oldsize!+!value!
)
for /R %date%%key% %%F in (*) do (
set /a value=%%~zF
set /a newsize=!newsize!+!value!
)

if %newsize% GTR %oldsize% echo del %oldf%
if %oldsize% GTR %newsize% echo needs checking

:end
endlocal

Thanks for helping
 
Usually I did this with a Batch file that i wrote but know i have the problem that the size of the folder and the files in that folder are bigger than 2 GB and Batch can only handle 32 Bit #.

I could be wrong but I think a scripts (whether BAT, VBS, etc.) ability to handle large files is contingant on the architecture "width" of the OS, system, and file system (16-, 32-, 64-bit). Honestly, I've never had to deal with folder/files of such an enourmous size.

BAT files were slightly before my time but I think I was able to read the concept of your batch. Let's try this.

Code:
'define our variables
dim intTargetFolderSize
dim intCurrentFolderSize
dim strDateToken
dim strFoldersToCheck
dim strKeyword
dim strInitialDir

dim objArgs
dim objFolder
dim objFolders
dim objFSO
dim objShell

'define the objects you'll use to access certain data
set objFSO = CreateObject("Scripting.FileSystemObject")
set objArgs = WScript.Arguments

'get arguments WITHOUT VALIDATION
strKeyword = objArgs(0)
strDateToken = objArgs(1)
strInitialDir = objArgs(2)

'get all folders in the inital dir
set objFolders = objFSO.GetFolder(strInitialDir).SubFolders

'get the size of folder that meets the strDateToken and strKeyword requirements.
for each objFolder in objFolders
	if (objFolder.Name = strDateToken & strKeyword) then
		intTargetFolderSize = objFolder.Size
	end if
next

'do any of the other folders conatin the strKeyword?
for each objFolder in objFolders
	if (inStr(objFolder.Name, strKeyword)) then
		'Yes is does. Is it target folder? If not, what's the size of the folder?
		if (objFolder.Name <> strDateToken & strKeyword) then
			intCurrentFolderSize = objFolder.Size
		
			'If this folder is smaller than the target folder
			'then delete the current folder. Otherwise, check the current folder
			if (intCurrentFolderSize < intTargetFolderSize) then
				[red]'objFolder.Delete[/red]
			else
				strFoldersToCheck = strFoldersToCheck & objFolder.Path & ": (" & intCurrentFolderSize & " bytes)" & vbNewLine
			end if
		end if
	end if
next

msgbox "Target Folder:" & vbNewLine & strInitialDir & ": (" & intTargetFolderSize & " bytes)" & vbNewLine & vbNewLine &_
       "These folders were bigger than the target folder and need checking: " & vbNewLine & vbNewLine &_
       strFoldersToCheck


I THINK this does what you want. Although, being unsure I commented out "objFolder.Delete" (red text) as to not screwed your data structure.

-Geates


"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top