Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Archiving Log Files 1

Status
Not open for further replies.

OneArk

Technical User
Jul 25, 2008
12
CA
Hi
I am trying to create a VBScript that will go through log files on a server, then move it to an archive location if older than one day.

I am new to VBS, however I used Visual Basic back in 98 so I may still have some logic building skills but as far as vbs understanding I may not be very sharp.

Here is the flow chart

1. Zip one file at a time.
2. Check archive integrity.
3. Delete original log file after the zip file is created then checked for integrity.

What I have so far is I am able to zip the files and delete it. but I am unable to check the integrity before deleting. I am not sure if the switches -m and -T will be sufficient, also I am unable to get any output of the commands to the screen so that I can capture it in a log file.
Code:
************************************************
' Setting the Variables used in the Loop
Dim CurDate, CurTime, dn
Dim ObjLOG, ObjShellLOG, ObjLOGFolder, LOGHome
Dim ObjLOGCollection, ObjLOGFile, LOGModDate, LOGAge, LOGFileName
Dim StrFileLOG, StrCMDLOG, StrRunLOG
Dim CheckZipLOG

'set the datetime parameter
CurDate = Replace (FormatDateTime(Date, 2), "/", "-")
CurTime = Replace (FormatDateTime(Time(), 4), ":","")
dn = CurDate &CurTime

LOGHome = "D:\Logs\"
Set ObjLOG = CreateObject("Scripting.FileSystemObject")
Set objShellLOG = wscript.createObject("wscript.shell")
Set ObjLOGFolder = ObjLOG.GetFolder(LOGHome)
Set ObjLOGCollection = ObjLOGFolder.Files

'loop that will go through the files one at a time and zip it
For Each ObjLOGFile In ObjLOGCollection
	LOGModDate = ObjLOGFile.DateLastModified
	LOGFileName = ObjLOGFile.Path & "-" & dn & ".zip"
	LOGFileName = replace (LOGFileName, " ","")
	LOGAge = datediff ("d", LOGModDate, Now())
	StrFileLOG = ObjLOGFile.Path
	strFileLOG = replace (StrFileLOG, " ","*")
	StrCMDLOG = "zip  " & LOGFileName & " " &StrFileLOG & " >> d:\docs\logs.txt"
		
	if LOGAge > 1 Then 
	strRunLOG = objShellLOG.Run(strCMDLOG, 0, True)
	wscript.echo err.number
	End If
Next
 
I am unable to get any output of the commands
Use the Exec method instead of Run and play with the StdErr and StdOut properties of the WshScriptExec object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
See the FAQ's for an example: faq329-3330
 
Do you have an example of exec? I tried the following, it zips the fisrt file then quits. With Run it goes through the loop.

Code:
if LOGAge > 1 Then 
strRunLOG = objShellLOG.Exec (StrCMDLOG)
End If
 
Thanks, jges, but that is the example I used to exec but still did not work, I do not seem to be able to pass variables to it.

Microsoft VBScript runtime error: Object doesn't support this property or method
 
Is the code snippet you posted the only change from your original post? If so, try looking in the StdOut and StdErr properties as PHV suggested instead of trying to redirect the output to a text file.
 
[tt] '... etc
[red]'[/red]StrCMDLOG = "zip " & LOGFileName & " " &StrFileLOG & " >> d:\docs\logs.txt"
[blue]StrCMDLOG = "zip " & LOGFileName & " " &StrFileLOG[/blue]

if LOGAge > 1 Then
[red]'[/red]strRunLOG = objShellLOG.Run(strCMDLOG, 0, True)
'even with run and re-direction, this echo is ineffective, it never monitors err.number without setup of on error directive
[red]'[/red]wscript.echo err.number
[red]set[/red] [blue]wshexec=objShellLog.exec(strCMDLOG)
do while wshexec.status<>1 : wscript.sleep 50 : loop
s=wshexec.stdout.readall
t=wshexec.stderr.readall
'control signature string inside s or t depending on your application (zip)[/blue]
End If
[/tt]
 
The following part never worked and never created that text file

Code:
 & " >> d:\docs\logs.txt"

how would I transform the code to use the exec method? can I pass variables to it?
 
thanks Tsuji, will try that and let you know how it went.
 
That Works perfectly, what does the following code do? I removed it and it worked good as well.

Code:
do while wshexec.status <> 1 : wscript.sleep 50 : loop
 
It just makes sure the process so spawned has completed its course so that readall is reading all. For a process that's expected to run a fairly short period, it acts like it does not even notice. I would place it there for robustness, besides, 50 ms is not that long sampling period.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top