Solved

Moving files to a different directory depending on modified date information

Posted on 2006-07-11
21
210 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Padre_Corleone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 2
21 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087103
  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
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087123
I will give this a try and keep you posted
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 17087132
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")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:ElrondCT
ID: 17087136
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...
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087143
Sorry Elrond.. gotta get back on the score board, ya know :)
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087169
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
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087195
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
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087264
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
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087274
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.
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087304
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
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087312
"not working" is a pretty vague description... anything else you can tell me?
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087322
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.

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087332
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.
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087365
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
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 17087378
oops..

Change this....  Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToString

to  Dim monthDir As String = yearDir & "\" & modifiedDate.Month.ToString
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087473
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?
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087617
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 :)
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17087635
thanks for the help! if you have suggestions about my last question on date i appreicated. if not then i understand.  tahnks
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087742
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
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17091670
thank you.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17091696
Glad to help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question