Solved

vb.net - use subdirectories

Posted on 2011-03-02
6
201 Views
Last Modified: 2012-05-11
Hello there,
I am using this code to create directories based on the date the picture was taken.
it looks for all of the images in a dir and then it moves them.
I have 5 directories with images and I would like to make the code look for sub-directories..
how exactly can I do that?
Imports System.IO
Imports System.Text
Imports System.Drawing.Imaging
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            ProcessFiles(FolderBrowserDialog1.SelectedPath, "*.jpg")
        End If
    End Sub

    Private Sub ProcessFiles(ByVal Folder As String, ByVal Pattern As String)
        Dim di As New DirectoryInfo(Folder)
        For Each fi As FileInfo In di.GetFiles(Pattern)
            Dim values() As String = FindDateTaken(fi.FullName).Split(":")
            If values.Length >= 2 Then
                values(2) = values(2).Split(" ")(0)
                Try
                    Dim dt As New DateTime(values(0), values(1), values(2))
                    Dim SubDi As New DirectoryInfo(Path.Combine(di.FullName, dt.Year))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If
                    SubDi = New DirectoryInfo(Path.Combine(SubDi.FullName, dt.Month.ToString("00")))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If
                    SubDi = New DirectoryInfo(Path.Combine(SubDi.FullName, dt.Day.ToString("00")))
                    If Not SubDi.Exists() Then
                        SubDi.Create()
                    End If

                    Dim NewFileName As String = Path.Combine(SubDi.FullName, fi.Name)
                    fi.MoveTo(NewFileName)
                Catch ex As Exception

                End Try
            End If
        Next
    End Sub

    Private Function FindDateTaken(ByVal strPicture As String) As String
        Const DATE_TAKEN As Integer = &H9003
        Dim dateTaken As String = ""
        Using img As Image = Image.FromFile(strPicture)
            If img.PropertyIdList.Contains(DATE_TAKEN) Then
                Dim pitem As PropertyItem = img.GetPropertyItem(DATE_TAKEN)
                dateTaken = Encoding.UTF8.GetString(pitem.Value, 0, pitem.Value.Length)
            End If
        End Using
        Return dateTaken
    End Function

End Class

Open in new window

0
Comment
Question by:XK8ER
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 35022842
Change line 14 to:
For Each fi As FileInfo In di.GetFiles(Pattern, SearchOption.AllDirectories)

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35022899
Can you explain more about the other folders?  Are they INSIDE the folder that is selected by the user?

WHERE should the created folders go now?  Should each subfolder get its own structure?...or should just the "root" folder get the new directories and the files from the subdirectories should go there as well?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35022989
yes they are inside the directory selected by the user..this is what I have

C:\pics\Sep
C:\pics\Nov
C:\pics\Dec
C:\pics\Jan
C:\pics\Feb

it should look for the images in sub-directories and place them in the correct format.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35023122
K...but should the CREATED folders be like this?

C:\pics\Sep\2011\03\02
C:\pics\Sep\2011\01\15
C:\pics\Nov\2010\07\09
C:\pics\Nov\2009\11\32

Or like this?

C:\pics\2011\03\02
C:\pics\2011\01\15
C:\pics\2010\07\09
C:\pics\2009\11\32
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35023160
it should be like this

C:\pics\2011\03\02
C:\pics\2011\01\15
C:\pics\2010\07\09
C:\pics\2009\11\32
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 35023499
Cool...then the change by kaufmed is perfect!

*If you needed the folders to be created within each subdirectory then the code would have to be changed.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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