Solved

Moving files to a different directory depending on modified date information

Posted on 2006-07-11
21
198 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
  • 10
  • 9
  • 2
21 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
  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
Comment Utility
I will give this a try and keep you posted
0
 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
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
 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
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
Comment Utility
Sorry Elrond.. gotta get back on the score board, ya know :)
0
 
LVL 7

Author Comment

by:Padre_Corleone
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Expert Comment

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

Author Comment

by:Padre_Corleone
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you.
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
Glad to help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now