Link to home
Start Free TrialLog in
Avatar of epicazo
epicazoFlag for United States of America

asked on

vbs to compress file

I have the code below that I obtained from EE, but it doesn't seem to work.   I want to be able to compress all the files in the "c:\prg" folder and run daily via scheduler.
strComputer = "chpcpsi.cmhhp.com"

dtmDate = Date - 1
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:\prg'} 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

Avatar of Kenneth Brown
Kenneth Brown
Flag of United Kingdom of Great Britain and Northern Ireland image

Im not an expert in vbs, but try using tried & tested generic debugging techniques to narrow down the problem:
If you have the ability to display message boxes etc or some other means of communicating the program's flow, choose key points and display intermediate results so you can narrow down where the problem lies ( you have not said exactly what the problem is), eg are there any files in filelist etc...
Once you know where the problem is, you can fix it and then remove the debugging stuff....
ASKER CERTIFIED SOLUTION
Avatar of epicazo
epicazo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of epicazo

ASKER

No suitable solution found.   Closed