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
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

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
Avatar of mrsam3
mrsam3

ASKER

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
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bill Prew
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
Avatar of mrsam3
mrsam3

ASKER

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

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo