I'm trying to write a program to move all files/folders (providing that the created/modified date is more than a month old) from one folder tree to another.
But I'm encountering a problem when creating the new folder, which for some reason is still trying to be created even if it already exists.
The following code is what I have so far, any help in getting this to work would be appreciated.
But I'm encountering a problem when creating the new folder, which for some reason is still trying to be created even if it already exists.
The following code is what I have so far, any help in getting this to work would be appreciated.
VB.NET:
Imports System.Collections.Generic
Imports System.IO
Public Class Archiver
Dim todaysDate As String = Format(Date.Today, "yyyy-mm-dd")
Dim destDir As String = "C:\Backup\" & todaysDate
Dim sourceDir As String = "C:\Temp\"
Public Sub Move()
ValidateFolders()
MoveFolders()
End Sub
Private Sub ValidateFolders()
If Not Directory.Exists(destDir) Then
Directory.CreateDirectory(destDir)
End If
End Sub
Private Sub MoveFolders()
Dim folders As New List(Of String)
Dim folder As String
folders.AddRange(Directory.GetDirectories(sourceDir))
For Each folder In folders
Dim dirInfo As New DirectoryInfo(folder)
If dirInfo.CreationTime.Date < Date.Today.AddMonths(-1) Then
MoveFolder(dirInfo.Name)
End If
Next
End Sub
Private Sub MoveFolder(ByVal strFolder As String)
Dim sourceDir As String = Path.Combine(sourceDir, strFolder)
Dim finalDir As String = Path.Combine(destDir, strFolder)
If Directory.Exists(finalDir) Then
finalDir += "_" + Date.Now.ToString("yyyyMMddhhmmss") + "_" + Guid.NewGuid.ToString()
End If
Directory.Move(sourceDir, processDir)
End If
End Sub
End Class
Last edited by a moderator: