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

LVL 2
mrsam3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
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
0
mrsam3Author Commented:
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.
0
Robert SchuttSoftware EngineerCommented:
Try adding this line instead:
if FSO.GetFile(fileName).size=0 then FSO.deletefile fileName,true

Open in new window

fileName is a string, you can't test file size directly with just that.

While you're testing, it may be better to comment out the first line of your script so that errors are shown.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
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
0
mrsam3Author Commented:
Replacing with this line fixed the problem and now it works like we want. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.