Avatar of mrsam3
mrsam3 asked on

Help with VBScript

I am new to vb scripting but have the below script that will find OST and PST files on a computer and write to a file text document if the file is over a certain size. I want it to delete the file if it does not find anything (size = 0).

I believe adding the following code after the record.close should do it but it is not working . I still have blank files and I don't know why.

 if fileName.size=0 then FSO.deletefile fileName.path,true

Open in new window


Full Script
on error resume next
' Write what we're scanning for
path = "c:\"

Set FSO = CreateObject("Scripting.FileSystemObject")

fileName = Replace(Replace("file-sizes_" & date() & "_" & time() & ".txt","/","-"),":","-")
Set record = FSO.CreateTextFile(fileName, True)

ScanDirectory FSO.GetFolder(path)

record.close


Sub ScanDirectory(oFolder)
        on error resume next
	' Scan the Files in this Directory
	ScanFiles oFolder
	' Essentially iterate in this function with any additional folder names found
	For each folder in oFolder.SubFolders
                ScanDirectory folder
	Next
End Sub

Sub Scanfiles(oFolder)
        on error resume next
	' Checks files in a folder and outputs any that haven't been modified in x days
        recordNumber = 0
        For each file in oFolder.Files
		If file.size > 5368709120 Then
                       recordNumber = recordNumber + 1
                       record.WriteLine(recordNumber & " , " & file.Path & " , " & file.Size & "Bytes")
		End If
	Next
End Sub

Open in new window

VB ScriptWindows OS

Avatar of undefined
Last Comment
mrsam3

8/22/2022 - Mon
unknown_routine

A record to textstreamis written ONLY if you have files larger than 5368709120  bytes
5368709120 KB is a very large file. 5 Tera byte file?

Are you really looking for files of this magninute? do they exist on your system.



I do not see an issue in your code.

Try to test your code with a smaller number . Lets says 10000000
ASKER
mrsam3

It works with any size file. I think I left it really big so that it does not find anything for testing. I don't want it to find a file of that size so it should be deleting the text document.
ASKER CERTIFIED SOLUTION
Robert Schutt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bill Prew

Another approach that is often used when the output wont be too huge is to just built the output text to be written as a string in the code, concatenating each new line as you need to. Then after the logic is done, you write the string to a file with one Write(). So in your case you can only create and write to the file if you had some output. Something like this:

on error resume next
' Write what we're scanning for
path = "c:\"
strOutput = ""

Set FSO = CreateObject("Scripting.FileSystemObject")

ScanDirectory FSO.GetFolder(path)

if strOutput <> "" then
        fileName = Replace(Replace("file-sizes_" & date() & "_" & time() & ".txt","/","-"),":","-")
        Set record = FSO.CreateTextFile(fileName, True)
        record.Write strOutput
        record.close
end if

Sub ScanDirectory(oFolder)
        on error resume next
        ' Scan the Files in this Directory
        ScanFiles oFolder
        ' Essentially iterate in this function with any additional folder names found
        For each folder in oFolder.SubFolders
                ScanDirectory folder
        Next
End Sub

Sub Scanfiles(oFolder)
        on error resume next
        ' Checks files in a folder and outputs any that haven't been modified in x days
        recordNumber = 0
        For each file in oFolder.Files
                If file.size > 5368709120 Then
                       recordNumber = recordNumber + 1
                       strOutput = strOutput & recordNumber & " , " & file.Path & " , " & file.Size & "Bytes" & vbCrLf
                End If
        Next
End Sub

Open in new window

~bp
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
mrsam3

Replacing with this line fixed the problem and now it works like we want. Thank you.