Solved

vb.net - use subdirectories

Posted on 2011-03-02
6
198 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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

17 Experts available now in Live!

Get 1:1 Help Now