NTFS File Compression with VBSCRIPT

I have a requirement for a script to go through a folder & compress files (using NTFS Compression) which are older than 90 days, and i'm struggling with the bit i thought would be the easy bit

using the oFile object in vbscript:

oFile.compress does not exist and

oFile.attributes = ofile.attributes xor 2048 is a non-starter because that bit is read-only (assumably because simply setting the bit does not kick off the compression activity)

I don't want to compress the entire folder, as many of the files in there will be under the target date

I cannot use third party tools. - The point of this exercise is to recover server space without requiring any change of practice for the end user (such as zipping the files)

Regards

LVL 5
Ignatius_AAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

yehudahaCommented:
change line 18 folder path
save as vbs

in case you want more than 90 days change line 3
strComputer = "."

dtmDate = Date - 90
strDay = Day(dtmDate)
If Len(strDay) < 2 Then
    strDay = "0" & strDay
End If
strMonth = Month(dtmDate)
If Len(strMonth) < 2 Then
    strMonth = "0" & strMonth
End If
strYear = Year(dtmDate)
strTargetDate = strYear & strMonth & strDay

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
( "ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where " _
& "ResultClass = CIM_DataFile" )

For Each objFile In FileList
	strDate = Left(objFile.CreationDate, 8)
	If strDate < strTargetDate Then
		compress(objfile.name)
	End If
Next


MsgBox "done"



Function compress(strfile)
	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set objFile = objWMI.Get("Cim_Datafile='" & strFile & "'")
	strerr = objFile.Compress
	If strerr <> 0 Then
		compress = strFile & " There was an error compressing the file: " & strerr
	Else
		compress = strfile & " File compression successful"
	End If
End Function

Open in new window

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
yehudahaCommented:
if you want lof file use this

as above
change line 18 to the folder to compress path
save as vbs

in case you want more than 90 days change line 3

change line 1 for log file path
strlog = "c:\ntfs.txt"
strComputer = "."
Set fso = CreateObject("scripting.filesystemobject")
Set objlog = fso.CreateTextFile(strlog,True)

dtmDate = Date - 90
strDay = Day(dtmDate)
If Len(strDay) < 2 Then
    strDay = "0" & strDay
End If
strMonth = Month(dtmDate)
If Len(strMonth) < 2 Then
    strMonth = "0" & strMonth
End If
strYear = Year(dtmDate)
strTargetDate = strYear & strMonth & strDay

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
( "ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where " _
& "ResultClass = CIM_DataFile" )

For Each objFile In FileList
	strDate = Left(objFile.CreationDate, 8)
	If strDate < strTargetDate Then
		objlog.WriteLine compress(objfile.name)
	End If
Next


MsgBox "done"



Function compress(strfile)
	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set objFile = objWMI.Get("Cim_Datafile='" & strFile & "'")
	strerr = objFile.Compress
	If strerr <> 0 Then
		compress = strFile & " There was an error compressing the file: " & strerr
	Else
		compress = strfile & " File compression successful"
	End If
End Function

Open in new window

Ignatius_AAuthor Commented:
Thanks,

That gave me exactly the calls I needed to resolve my issue, as well as a pointer towards the date format returned by the file object (i actually used lastmodified for my implementation, and processed subtrees too, but no problem).

Classed as A, because I didn't add those requirements to the initial request.

Regards

yehudahaCommented:
thanks
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.