nhaas

We are trying to change backup to a Network Backup from our Nortel Voicemail.

I simply want to read a files date stamp and keep the newest 4 and remove the oldest files!

The way that the backup is set up is to create new files each time the backup starts and not to write over existing backups, The backup sizes are around 4 gigs.

Can I do something like this in VB I aventually would like to put this into a database to see when a new backup is in the directory and of course when a backup has been removed.

I have started with this code below but would like to know if I can delete a file. thanks

Dim backupd, Duration

Duration = 4 'as in files

backupd = "c:\CallPilot-Backup\"

If System.IO.Directory.Exists(backupd) = True Then

Me.ListBox1.DataSource = System.IO.Directory.GetFiles(backupd)

Else

MsgBox("This is not a valid path....")

End If

Thanks


Re: Visual Basic Express Edition removing files in directory

ReneeC

There are many ways delete a file

The Kill keyword will do it

This will also do it: (From the Object Browser)

Public Shared Sub Delete(ByVal path As String)

Member of: System.IO.File

Summary:

Deletes the specified file. An exception is not thrown if the specified file does not exist.

Parameters:

path: The name of the file to be deleted.






Re: Visual Basic Express Edition removing files in directory

DMan1

If System.IO.Directory.Exists(backupd) = True Then

Dim DI As New System.IO.DirectoryInfo(backupd)

Dim fis() As IO.FileInfo = DI.GetFiles

For Each fi As IO.FileInfo In fis

If fi.Exists Then

fi.Delete()

End If

Next

End If






Re: Visual Basic Express Edition removing files in directory

bdbodger

What about something like this it gets the files in a directory sorts them by LastWriteTime then keeps the 4 newest files .

Code Block

Imports System.io

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim FBD As New FolderBrowserDialog

If FBD.ShowDialog = Windows.Forms.DialogResult.OK Then

Dim thefiles() As String = Directory.GetFiles(FBD.SelectedPath)

If thefiles.Length > 4 Then

Dim comparer As IComparer = New Datesorter

Array.Sort(thefiles, comparer)

For FDelete As Integer = 4 To thefiles.Length - 1

File.Delete(thefiles(FDelete))

Next

End If

End If

End Sub

Public Class Datesorter

Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

Dim xinfo As FileInfo = New FileInfo(x)

Dim yinfo As FileInfo = New FileInfo(y)

If xinfo.LastWriteTime < yinfo.LastWriteTime Then

Return 1

ElseIf xinfo.LastWriteTime = yinfo.LastWriteTime Then

Return 0

Else

Return -1

End If

End Function

End Class

End Class

** Edit small correction I moved the delete files lines into the if statement for thefiles.length > 4 since you wouldn't delete files if there are less than 4 files .




Re: Visual Basic Express Edition removing files in directory

nhaas

Thanks, there is alot of information here. I will start going through it and see what I can build.