Link to home
Start Free TrialLog in
Avatar of Padre_Corleone
Padre_CorleoneFlag for United States of America

asked on

Moving files to a different directory depending on modified date information

If file are older then 40 days, then
' check file modified date
'create a sub folder with year file modifed
'create a another suffolder with month modified
'then move file to month subfolder.

'this is my working code I have so far how can i implement in my current code.
'this current code moves all files that are older then 40 days but it doesn't check the file modified year and month and it 'doesn't create folders to the specific year and month.  

        Dim daysOld As Integer = AppSettings("daysOld")
            Dim di As New DirectoryInfo(searchDir)

 Dim fi() As FileInfo = di.GetFiles("*.txt")
            Dim dirs() As DirectoryInfo = di.GetDirectories()


            For Each fInfo As FileInfo In fi
                 If File.GetLastWriteTime(fInfo.FullName) < Today.AddDays(-40) Then
                    If Not Directory.Exists(Dest) Then _
                       Directory.CreateDirectory(Dest)
                    File.Move(fInfo.FullName, Dest & "\" & fInfo.Name)
                End If
            Next
            ' Go through all the directories in the current directory
            For Each dir As DirectoryInfo In dirs
                txtMoveFile(Dest, dir.FullName)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

  For Each fInfo As FileInfo In fi
      Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
      If modifiedDate < Today.AddDays(-40) Then
        Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
        Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
        If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
        If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
        File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
      End If
    Next
Avatar of Padre_Corleone

ASKER

I will give this a try and keep you posted
Avatar of ElrondCT
You can get the year and month with:

File.GetLastWriteTime(fInfo.FullName).Year
File.GetLastWriteTime(fInfo.FullName).Month

These will be returned as integer values. You could create a directory with:

Dim strYear as String = File.GetLastWriteTime(fInfo.FullName).Year.ToString
Dim strMonth as String = File.GetLastWriteTime(fInfo.FullName).Month.ToString
System.IO.CreateDirectory("C:\" & strYear & "\" & strMonth")
Grr, I hate it when I take several minutes to write a post then find someone else has written the same--or better--in the interim...
Sorry Elrond.. gotta get back on the score board, ya know :)
how can i pass the values from my txtmovefiles sub to sub paths.  
how do i change my despath to refelct the path depending on my file modification date?
my patch destination and source are coming from my appsettins files.

this is actuall a window service.


    Public Sub Paths()
        Try
            Dim despath As String = AppSettings("DestinationPath")
            despath = despath & "\" & Today.Year.ToString & "\" & Now.ToString("MMM") & "\" & Today.Month.ToString & _
        "_" & Today.Day.ToString & "_" & Today.Year
            Dim sPath As String = AppSettings("SourcePath")
            txtMoveFile(despath, sPath)
        Catch ex As Exception
        End Try
    End Sub
    ' recursively run through directories and search
    Public Sub txtMoveFile(ByVal Dest As String, ByVal searchDir As String)
        ' Declare variables
        Try
            Dim daysOld As Integer = AppSettings("daysOld")
            Dim di As New DirectoryInfo(searchDir)
            Dim fi() As FileInfo = di.GetFiles("*.xml")
            Dim dirs() As DirectoryInfo = di.GetDirectories()

            For Each fInfo As FileInfo In fi
                Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
                If modifiedDate < Today.AddDays(-40) Then
                    Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
                    Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
                    If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
                    If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
                    File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
                End If
            Next
            For Each dir As DirectoryInfo In dirs
                txtMoveFile(Dest, dir.FullName)
            Next


        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub
I'd try something like this...

This will start at your specified root directory, then move all xml files that meet your criteria from that directory. For each sub directory in that directory, it will move all the files, then do each subsubdirectory, etc

Public Sub MoveFiles(ByVal searchDir As String)
    ' Pass in your starting point
    Dim di As New DirectoryInfo(searchDir)
    Dim dirs() As DirectoryInfo = di.GetDirectories()

    Dim dest As String = AppSettings("Dest")
    For Each dir As DirectoryInfo In di.GetDirectories
      MoveFilesInDirectory(dest, dir.FullName)
    Next
  End Sub

  Public Sub MoveFilesInDirectory(ByVal Dest As String, ByVal searchDir As String)
    ' Declare variables
    Try
      Dim daysOld As Integer = AppSettings("daysOld")

      Dim di As New DirectoryInfo(searchDir)
      Dim fi() As FileInfo = di.GetFiles("*.xml")
      For Each fInfo As FileInfo In fi
        Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
        If modifiedDate < Today.AddDays(-40) Then
          Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
          Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
          If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
          If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
          File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
        End If
      Next

      For Each dir As DirectoryInfo In di.GetDirectories
        MoveFilesInDirectory(Dest, dir.FullName)
      Next
    Catch ex As Exception
      ' MsgBox(ex.Message)
    End Try
  End Sub
I get an error when I use getme.movefiles() on my sub main.  

  ' I get an error here << arguement not specified in parameter

' this will call my code when i click play
    Shared Sub main()
               MoveFiles()
   End Sub



Imports System.IO
Imports System.Configuration.ConfigurationManager
Public Class LMDArchive
    Shared Sub main()

        Dim getMe As New WindowsApplication2.LMDArchive
        getMe.MoveFiles()   ' I get an error here << arguement not specified in parameter
    End Sub
    ' Chooses where to look for files and where to put them and create folder

    '  Public Sub Paths()
    '     Try
    ' Dim daysOld As Integer = AppSettings("daysOld")
    ' Dim di As New DirectoryInfo(searchDir)
    ' Dim fi() As FileInfo = di.GetFiles("*.xml")
    ' Dim dirs() As DirectoryInfo = di.GetDirectories()
    ' For Each fInfo As FileInfo In fi
    'Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
    '    If modifiedDate < Today.AddDays(-40) Then
    'Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
    'Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
    'If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
    'If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
    'File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
    '     End If
    '  Next
    '   For Each dir As DirectoryInfo In dirs
    '          Dim despath As String = AppSettings("DestinationPath")
    '         despath = despath & "\" & modifieddate. & "\" & Now.ToString("MMM") & "\" & Today.Month.ToString & _
    '       "_" & Today.Day.ToString & "_" & Today.Year
    '            Dim sPath As String = AppSettings("SourcePath")
    ' txtMoveFile(despath, sPath)

    'txtMoveFile(Dest, dir.FullName)
    '         Next
    '      Catch ex As Exception
    ' MsgBox(ex.Message)
    '       End Try
    '  Try
    'Dim despath As String = AppSettings("DestinationPath")
    '       despath = despath & "\" & Today.Year.ToString & "\" & Now.ToString("MMM") & "\" & Today.Month.ToString & _
    '  "_" & Today.Day.ToString & "_" & Today.Year
    '     despath =
    '  Dim sPath As String = AppSettings("SourcePath")
    ' txtMoveFile(despath, sPath)
    ' Catch ex As Exception
    ' End Try
    '    End Sub
    ' recursively run through directories and search
    '    Public Sub txtMoveFile(ByVal Dest As String, ByVal searchDir As String)
    ' Declare variables
    '     Try
    '  Dim daysOld As Integer = AppSettings("daysOld")
    '  Dim di As New DirectoryInfo(searchDir)
    '  Dim fi() As FileInfo = di.GetFiles("*.xml")
    '  Dim dirs() As DirectoryInfo = di.GetDirectories()
    ' Go through each Txt file in the directory and move it if needed
    '     For Each fInfo As FileInfo In fi
    'If File.GetLastWriteTime(fInfo.FullName) > Today.AddDays(daysOld) Then
    'If Not Directory.Exists(Dest) Then _
    'Directory.CreateDirectory(Dest)
    ' File.Move(fInfo.FullName, Dest & "\" & fInfo.Name)
    ' End If
    ' Next
    ' Go through all the directories in the current directory
    ' For Each dir As DirectoryInfo In dirs
    'txtMoveFile(Dest, Dir.FullName)
    'Next

    ''            For Each fInfo As FileInfo In fi
    '   Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
    '              If modifiedDate < Today.AddDays(-40) Then
    ' Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
    ' Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
    '                If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
    '               If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
    '              File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
    '         End If
    '    Next
    '   For Each dir As DirectoryInfo In dirs
    '      txtMoveFile(Dest, dir.FullName)
    ' Next


    '    Catch ex As Exception
    ' MsgBox(ex.Message)
    '   End Try
    ' End Sub

    Public Sub MoveFiles(ByVal searchDir As String)
        ' Pass in your starting point
        Dim di As New DirectoryInfo(searchDir)
        Dim dirs() As DirectoryInfo = di.GetDirectories()

        Dim dest As String = AppSettings("Dest")
        For Each dir As DirectoryInfo In di.GetDirectories
            MoveFilesInDirectory(dest, dir.FullName)
        Next
    End Sub

    Public Sub MoveFilesInDirectory(ByVal Dest As String, ByVal searchDir As String)
        ' Declare variables
        Try
            Dim daysOld As Integer = AppSettings("daysOld")

            Dim di As New DirectoryInfo(searchDir)
            Dim fi() As FileInfo = di.GetFiles("*.xml")
            For Each fInfo As FileInfo In fi
                Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
                If modifiedDate < Today.AddDays(-40) Then
                    Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
                    Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
                    If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
                    If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
                    File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
                End If
            Next

            For Each dir As DirectoryInfo In di.GetDirectories
                MoveFilesInDirectory(Dest, dir.FullName)
            Next
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub
End Class
MoveFiles takes your starting directory as an argument. This lets you start in any directory.

i.e. MoveFiles(AppSettings("StartDir"))

Also, please don't post all your commented code... it is irrelevant and makes it hard to find the real issue.
sorry about that.  yes i did try that and as you can see below this is my code.  but for some reason is not working.


Imports System.IO
Imports System.Configuration.ConfigurationManager
Public Class LMDArchive
    Shared Sub main()
        Dim wa As New WindowsApplication2.LMDArchive
           wa.MoveFiles(AppSettings("SourcePath"))
    End Sub

  Public Sub MoveFiles(ByVal searchDir As String)
        ' Pass in your starting point
        Dim di As New DirectoryInfo(searchDir)
        Dim dirs() As DirectoryInfo = di.GetDirectories()
        Dim dest As String = AppSettings("DestinationPath")
        For Each dir As DirectoryInfo In di.GetDirectories
            MoveFilesInDirectory(dest, dir.FullName)
        Next
    End Sub

    Public Sub MoveFilesInDirectory(ByVal Dest As String, ByVal searchDir As String)
        ' Declare variables
        Try
            Dim daysOld As Integer = AppSettings("daysOld")

            Dim di As New DirectoryInfo(searchDir)
            Dim fi() As FileInfo = di.GetFiles("*.xml")
            For Each fInfo As FileInfo In fi
                Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
                If modifiedDate < Today.AddDays(-40) Then
                    Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
                    Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
                    If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
                    If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
                    File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
                End If
            Next

            For Each dir As DirectoryInfo In di.GetDirectories
                MoveFilesInDirectory(Dest, dir.FullName)
            Next
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub
End Class
"not working" is a pretty vague description... anything else you can tell me?
well i've noticed when i click play application events do not fire up, I believe I might be not firing my public sub when I click the play button.  I apologize i am fairly new to this and I am attemptingn to get this done for my work.

Put a breakpoint in the code to see if it executes.

If I remember right, you'll need to fire up the service, then attach a debugger to the process, so you might want to put the breakpoint in the recursive part of the applicaiton.

You might try putting the code in the onload event of a form first, since windows services are pretty tough to debug.
this might help i am not sure but when I use this code.  I press play and what it does it creates

-destination folder (destinationPath)
----12 (for the month folder) My xml files for the month are here but this folder should be withing 2005 if files are create in 2005
------- 2005 (for the year folder)  'this folder is empty.

basically it should be year first, month next, dump file.


    Shared Sub main()
        Dim wa As New WindowsApplication2.LMDArchive
        wa.Paths()

    End Sub

    ' Chooses where to look for files and where to put them and create folder

    Public Sub Paths()

        Try
            Dim despath As String = AppSettings("DestinationPath")

            Dim sPath As String = AppSettings("SourcePath")
            txtMoveFile(despath, sPath)
        Catch ex As Exception
        End Try
    End Sub
    ' recursively run through directories and search
    Public Sub txtMoveFile(ByVal Dest As String, ByVal searchDir As String)
        '   Declare variables
        Try
            Dim daysOld As Integer = AppSettings("daysOld")
            Dim di As New DirectoryInfo(searchDir)
            Dim fi() As FileInfo = di.GetFiles("*.xml")
            Dim dirs() As DirectoryInfo = di.GetDirectories()
   
            For Each fInfo As FileInfo In fi
                Dim modifiedDate As DateTime = File.GetLastWriteTime(fInfo.FullName)
                If modifiedDate < Today.AddDays(-40) Then
                    Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
                    Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString
                    If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(yearDir)
                    If Not Directory.Exists(monthDir) Then Directory.CreateDirectory(monthDir)
                    File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
                End If
            Next
            For Each dir As DirectoryInfo In dirs
                txtMoveFile(Dest, dir.FullName)
            Next
       Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
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
how can i change the file folder from
12 meaning december to actually showing the Month like: Dec

now.month("MMM") doesnt work for me it just show MMM as the file name?
If the recursion works, you need to close this question.

It's against the rules to ask several questions in one post.

And since you're a monthly subscriber, you don't need to work for the points :)
thanks for the help! if you have suggestions about my last question on date i appreicated. if not then i understand.  tahnks
Sure... there are a couple approaches... the easiest is shown here.

I'm sure there are prettier ways, but it has been a long, long day, and brute force has a certain appeal at the moment.

Private Function GetMonthName(month as integer) as String
Select Case Month
Case 1
Return "Jan"
Case 2
Return
"Feb"
Case 3
Return "Mar"
Case 4
Return "Apr"
Case 5
Return "May"
End Function
Case 6
Return "Jun"
Case 7
Return "July"
Case 8
Return "Aug"
Case 9
Return "Sep"
Case 10
Return "Oct"
End Function
Case 11
Return "Nov"
Case 12
Return
"Dec"
End Select
End Function
thank you.
Glad to help.