Solved

vb.net - use subdirectories

Posted on 2011-03-02
6
204 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
[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
  • 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 86

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

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 86

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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