Padre_Corleone
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(fInf o.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
' 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(fInf
If Not Directory.Exists(Dest) Then _
Directory.CreateDirectory(
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
ASKER
I will give this a try and keep you posted
You can get the year and month with:
File.GetLastWriteTime(fInf o.FullName ).Year
File.GetLastWriteTime(fInf o.FullName ).Month
These will be returned as integer values. You could create a directory with:
Dim strYear as String = File.GetLastWriteTime(fInf o.FullName ).Year.ToS tring
Dim strMonth as String = File.GetLastWriteTime(fInf o.FullName ).Month.To String
System.IO.CreateDirectory( "C:\" & strYear & "\" & strMonth")
File.GetLastWriteTime(fInf
File.GetLastWriteTime(fInf
These will be returned as integer values. You could create a directory with:
Dim strYear as String = File.GetLastWriteTime(fInf
Dim strMonth as String = File.GetLastWriteTime(fInf
System.IO.CreateDirectory(
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 :)
ASKER
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("DestinationPa th")
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(fInf o.FullName )
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
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
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("DestinationPa
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(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(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(fInf o.FullName )
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
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
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,
Next
End Sub
Public Sub MoveFilesInDirectory(ByVal
' 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(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(monthDir)
File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
End If
Next
For Each dir As DirectoryInfo In di.GetDirectories
MoveFilesInDirectory(Dest,
Next
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End Sub
ASKER
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.Confi gurationMa nager
Public Class LMDArchive
Shared Sub main()
Dim getMe As New WindowsApplication2.LMDArc hive
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(fInf o.FullName )
' If modifiedDate < Today.AddDays(-40) Then
'Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
'Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
'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("DestinationPa th")
' 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("DestinationPa th")
' 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(fInf o.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(fInf o.FullName )
' If modifiedDate < Today.AddDays(-40) Then
' Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
' Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
' 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(fInf o.FullName )
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
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
' 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.Confi
Public Class LMDArchive
Shared Sub main()
Dim getMe As New WindowsApplication2.LMDArc
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(fInf
' If modifiedDate < Today.AddDays(-40) Then
'Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
'Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
'If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
'If Not Directory.Exists(monthDir)
'File.Move(fInfo.FullName,
' End If
' Next
' For Each dir As DirectoryInfo In dirs
' Dim despath As String = AppSettings("DestinationPa
' 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("DestinationPa
' 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(fInf
'If Not Directory.Exists(Dest) Then _
'Directory.CreateDirectory
' 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(fInf
' If modifiedDate < Today.AddDays(-40) Then
' Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
' Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
' If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
' If Not Directory.Exists(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,
Next
End Sub
Public Sub MoveFilesInDirectory(ByVal
' 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(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(monthDir)
File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
End If
Next
For Each dir As DirectoryInfo In di.GetDirectories
MoveFilesInDirectory(Dest,
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("Sta rtDir"))
Also, please don't post all your commented code... it is irrelevant and makes it hard to find the real issue.
i.e. MoveFiles(AppSettings("Sta
Also, please don't post all your commented code... it is irrelevant and makes it hard to find the real issue.
ASKER
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.Confi gurationMa nager
Public Class LMDArchive
Shared Sub main()
Dim wa As New WindowsApplication2.LMDArc hive
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("DestinationPa th")
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(fInf o.FullName )
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
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
Imports System.IO
Imports System.Configuration.Confi
Public Class LMDArchive
Shared Sub main()
Dim wa As New WindowsApplication2.LMDArc
wa.MoveFiles(AppSettings("
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("DestinationPa
For Each dir As DirectoryInfo In di.GetDirectories
MoveFilesInDirectory(dest,
Next
End Sub
Public Sub MoveFilesInDirectory(ByVal
' 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(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(monthDir)
File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
End If
Next
For Each dir As DirectoryInfo In di.GetDirectories
MoveFilesInDirectory(Dest,
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?
ASKER
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.
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.
ASKER
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.LMDArc hive
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("DestinationPa th")
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(fInf o.FullName )
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin g
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
-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.LMDArc
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("DestinationPa
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(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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 :)
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 :)
ASKER
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
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
ASKER
thank you.
Glad to help.
Dim modifiedDate As DateTime = File.GetLastWriteTime(fInf
If modifiedDate < Today.AddDays(-40) Then
Dim yearDir As String = Dest & "\" & modifiedDate.Year.ToString
Dim monthDir As String = Dest & "\" & modifiedDate.Month.ToStrin
If Not Directory.Exists(yearDir) Then Directory.CreateDirectory(
If Not Directory.Exists(monthDir)
File.Move(fInfo.FullName, monthDir & "\" & fInfo.Name)
End If
Next