Advertisement

05.14.2008 at 01:42PM PDT, ID: 23403064
[x]
Attachment Details

Rename files based on SOURCE directory

Asked by judsonhh in .NET, Microsoft Visual Basic.Net

Tags: rename, folder, fileinfo

I am searching a root folder with many subfolders for the newest file over a given size. The found files are then mored to a new location. Problem is, during the re-move the possibility of overwritting a files is great. I need to add the first 7 characters of the source folder to the file name .
i.e.

C:\test\folder1\day1.text     renamed to       D:\log\folder1_day1.text

                   Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
Imports System.IO
 
Public Class Form1
 
    '' select source 1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Label1.Text = FolderBrowserDialog.SelectedPath
        End If
    End Sub
 
    ''' select source 2
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Label2.Text = FolderBrowserDialog.SelectedPath
        End If
    End Sub
 
    ''' select source 3
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Label3.Text = FolderBrowserDialog.SelectedPath
        End If
    End Sub
 
    ''' load timers
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Timer1.Enabled = True
        Timer2.Enabled = True
        REM TextBox1.Text = Format(TimeOfDay, "Long Time")
    End Sub
 
    ''  display clock in program
    Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
        REM Me.Text = TimeOfDay & "      As Run Process"
        Label4.Text = CStr(TimeOfDay)
    End Sub
 
    '' run action at displayed time
    Private Sub Timer2_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer2.Tick
        If TextBox1.Text = Label4.Text Then
            Dim fldrs As ArrayList = GetFolders(Label1.Text)
            Dim FileList As New System.Collections.Generic.List(Of String)
            Dim targetfolder As String = Label2.Text
            For Each folder As String In fldrs
                Dim filename As String = GetNewestFile(folder, 150, True)
                If Not String.IsNullOrEmpty(filename) Then FileList.Add(filename)
            Next folder
            Try
                CopyFiles(FileList, targetfolder)
 
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim fldrs As ArrayList = GetFolders(Label1.Text)
        Dim FileList As New System.Collections.Generic.List(Of String)
        Dim targetfolder As String = Label2.Text
        For Each folder As String In fldrs
            Dim filename As String = GetNewestFile(folder, 150, True)
            If Not String.IsNullOrEmpty(filename) Then FileList.Add(filename)
        Next folder
 
        Try
            CopyFiles(FileList, targetfolder)
 
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
 
 
    End Sub
 
    Sub CopyFiles(ByVal FileList As IEnumerable, ByVal TargetFolder As String)
 
        If Not Directory.Exists(TargetFolder) Then Directory.CreateDirectory(TargetFolder)
 
        For Each FileName As String In FileList
            File.Copy(FileName, TargetFolder & "\" & Path.GetFileName(FileName), True)
            MessageBox.Show((FileName))
        Next FileName
    End Sub
 
 
    Private Function GetFolders(ByVal path As String) As ArrayList
        Dim categories() As String = Directory.GetDirectories(Label1.Text)
        Dim i As Integer
        Dim al As New ArrayList
        For i = 0 To categories.Length - 1
            al.Add(categories(i))
        Next
        Return (al)
    End Function
 
    ''' <summary>
    ''' Get the name of the file in the specified folder that is the newest over specified size.
    ''' </summary>
    ''' <param name="folderPath">The full path to the folder we want to search</param>
    ''' <param name="minSizeKb">The size (in kilobyes, 1024 bytes) the file must be in order to be considered.</param>
    ''' <param name="includeSubFolders">search subfolders as well</param>
    ''' <returns>Full path to newest file</returns>
    ''' <remarks>Uses last modified date, not created date</remarks>
    Public Function GetNewestFile(ByVal folderPath As String, ByVal minSizeKb As Long, ByVal includeSubFolders As Boolean) As String
 
        Dim latestFileDate As Date = New Date(1901, 1, 1)
        Dim latestFilePath As String = ""
        Dim di As New DirectoryInfo(folderPath)
        If di.Exists Then   ' continue if directory exists
            ' search the current folder first
            searchFolder(di, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
            If includeSubFolders Then       ' search subfolders if required
                Dim subDirs As DirectoryInfo() = di.GetDirectories()
                For Each subdi In subDirs
                    searchFolder(subdi, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
                Next
            End If
        End If ' If di.Exists then
        Return latestFilePath
    End Function
 
    ''' <summary>
    ''' Helper function called recursively for subdirectories.  Do not call this function directly, call 'GetNewestFile'
    ''' </summary>
    ''' <param name="di"></param>
    ''' <param name="minSizeKb"></param>
    ''' <param name="includeSubFolders"></param>
    ''' <param name="latestFileDate">used for returning matches in subdirectories</param>
    ''' <param name="latestFilePath">used for returning matches in subdirectories</param>
    ''' <remarks></remarks>
    Private Sub searchFolder(ByVal di As DirectoryInfo, ByVal minSizeKb As Long, ByVal includeSubFolders As Boolean, _
                                  ByRef latestFileDate As Date, ByRef latestFilePath As String)
 
        Const KBSIZE As Long = 1024
 
        Dim fi As FileInfo() = di.GetFiles()        ' get list of all files in directory
 
        For i = fi.GetLowerBound(0) To fi.GetUpperBound(0)
            If (fi(i).Length / KBSIZE) > minSizeKb Then     ' make sure file meets min size requirement
                If fi(i).LastWriteTimeUtc > latestFileDate Then ' found newer file, save it
                    latestFileDate = fi(i).LastWriteTimeUtc
                    latestFilePath = fi(i).FullName
                End If
            End If
        Next
 
        If includeSubFolders Then
            Dim subDirs As DirectoryInfo() = di.GetDirectories()
 
            For Each subdi In subDirs
 
                searchFolder(subdi, minSizeKb, includeSubFolders, latestFileDate, latestFilePath)
 
            Next
 
        End If
 
    End Sub
 
End Class
[+][-]05.15.2008 at 12:31AM PDT, ID: 21571287

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 06:12AM PDT, ID: 21573173

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 06:14AM PDT, ID: 21573190

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 09:30AM PDT, ID: 21575459

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:05AM PDT, ID: 21575864

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:56AM PDT, ID: 21576439

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 01:05PM PDT, ID: 21577490

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 02:14PM PDT, ID: 21578028

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:57PM PDT, ID: 21580318

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, Microsoft Visual Basic.Net
Tags: rename, folder, fileinfo
Sign Up Now!
Solution Provided By: Dhaest
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906