Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net - use subdirectories

Posted on 2011-03-02
6
Medium Priority
?
212 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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