• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

How to loop through the directory root and subdirectories

I am wanting to loop through the directory root (C:\Temp1\) and subdirectories of my choice and manipulate tif files. My program only manipulates the files in the root, but it processes them twice!  And it doesn't address the files in the subdirectories.  I'm using Visual Studio 2003 on WinXP Pro and this is a normal Windows application.

I used the VS2003 Help (ms-help://MS.VSCC.2003/MS.MSDNQTR.2006JAN.1033/cpref/html/frlrfSystemIODirectoryInfoClassGetDirectoriesTopic.htm).  I would expect it to work properly but it doesn't.  There's something I'm missing somewhere.  Can you see it?

If I comment out what I use to loop thru the directories and subdirectories - "For Each diNext In dirs" and the matching "Next diNext", then it manipulates the files in the root only (C:\Temp1\) which is what I expect and is proper.  But I need to include all the subdirectories as well.

By the way, the variable "theDirectory" is set early in the program and is always C:\Temp1\ in this test.

Here is my directory structure which all contain tif files (and a few other formats which I won't manipulate):
C:\Temp1\
C:\Temp1\DELETE\
C:\Temp1\DELETE\00A485\
C:\Temp1\testW2\


Dim filenameWithoutExtension As String
'FROM: ms-help://MS.VSCC.2003/MS.MSDNQTR.2006JAN.1033/cpref/html/frlrfSystemIODirectoryInfoClassGetDirectoriesTopic.htm
Dim di As DirectoryInfo = New DirectoryInfo(theDirectory)
Dim dirs As DirectoryInfo() = di.GetDirectories()
Dim diNext As DirectoryInfo
Dim objStreamWriter As StreamWriter     'For log file.
objStreamWriter = New StreamWriter(theDirectory & "\log.txt")
objStreamWriter.Write(ControlChars.CrLf & "Log Entry : ")
objStreamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString() & vbCrLf)
For Each diNext In dirs
    For Each Fi As System.IO.FileInfo In di.GetFiles()
    Dim Doc As New PDFDocument
    Dim bitMap As Bitmap
    Dim frameDimension As FrameDimension
        If LCase(Fi.Extension) = ".tif" Then        'Set to lowercase so that all cases will be addressed.
        filenameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(Fi.FullName)   'Filename without extension.
            bitMap = New Bitmap(Fi.FullName)
            frameDimension = New FrameDimension(bitMap.FrameDimensionsList(0))
            framesCount = bitMap.GetFrameCount(frameDimension)
                For i = 0 To framesCount - 1
                    ' Create a new page
                    Dim page As PDFPage = Doc.AddPage()
                    ' draw the current tiff frame on the page
                    page.Canvas.DrawImage(Fi.FullName, 0, 0, page.Width, page.Height, 0, KeepAspectRatio.KeepNone, 1, i)
                Next i
            newPdfFilePathAndName = theDirectory & filenameWithoutExtension & ".pdf"
            Doc.SaveToFile(newPdfFilePathAndName)
            txtConvertedFiles.Text &= newPdfFilePathAndName & vbCrLf
            txtConvertedFiles.Refresh()     'Forces the textbox to redraw itself each time so progress is shown.
            ctrNumberOfFiles = ctrNumberOfFiles + 1     'Keep track of the number of files converted.
            lblConvertedFiles.Text = "Converted Files: " & ctrNumberOfFiles.ToString
            lblConvertedFiles.Refresh()     'Forces the textbox to redraw itself each time so progress is shown.
            'Now write to the log file:
                'objStreamWriter.WriteLine("  :")
                objStreamWriter.WriteLine(newPdfFilePathAndName)     'fullPathAndPDF
        End If

    Next Fi
Next diNext
End Sub

Thank you for your help!
0
psmithphil
Asked:
psmithphil
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to move your code into a method that receives a DirectoryInfo instance and a reference to your StreamWriter.  This way you can call the method recursively for each subfolder:

Imports System.IO

Public Class Form1

    Private theDirectory As String = "C:\Temp1\"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim di As DirectoryInfo = New DirectoryInfo(theDirectory)

        Dim objStreamWriter As StreamWriter     'For log file.
        objStreamWriter = New StreamWriter(theDirectory & "\log.txt")
        objStreamWriter.Write(ControlChars.CrLf & "Log Entry : ")
        objStreamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString() & vbCrLf)

        ProcessFiles(di, objStreamWriter)

        objStreamWriter.Close()
    End Sub

    Public Sub ProcessFiles(ByVal di As DirectoryInfo, ByVal sw As StreamWriter)
        ' process each file in the current directory
        Dim filenameWithoutExtension As String
        For Each Fi As FileInfo In di.GetFiles()
            Dim Doc As New PDFDocument
            Dim bitMap As Bitmap
            Dim frameDimension As FrameDimension
            If LCase(Fi.Extension) = ".tif" Then        'Set to lowercase so that all cases will be addressed.
                filenameWithoutExtension = Path.GetFileNameWithoutExtension(Fi.FullName)   'Filename without extension.
                bitMap = New Bitmap(Fi.FullName)
                frameDimension = New FrameDimension(bitMap.FrameDimensionsList(0))
                framesCount = bitMap.GetFrameCount(frameDimension)
                For i = 0 To framesCount - 1
                    ' Create a new page
                    Dim page As PDFPage = Doc.AddPage()
                    ' draw the current tiff frame on the page
                    page.Canvas.DrawImage(Fi.FullName, 0, 0, page.Width, page.Height, 0, KeepAspectRatio.KeepNone, 1, i)
                Next i

                newPdfFilePathAndName = theDirectory & filenameWithoutExtension & ".pdf"
                Doc.SaveToFile(newPdfFilePathAndName)
                txtConvertedFiles.Text &= newPdfFilePathAndName & vbCrLf
                txtConvertedFiles.Refresh()     'Forces the textbox to redraw itself each time so progress is shown.
                ctrNumberOfFiles = ctrNumberOfFiles + 1     'Keep track of the number of files converted.
                lblConvertedFiles.Text = "Converted Files: " & ctrNumberOfFiles.ToString
                lblConvertedFiles.Refresh()     'Forces the textbox to redraw itself each time so progress is shown.

                'Now write to the log file:
                'objStreamWriter.WriteLine("  :")
                sw.WriteLine(newPdfFilePathAndName)     'fullPathAndPDF
            End If

        Next Fi

        ' recurse into the subfolders
        For Each diNext As DirectoryInfo In di.GetDirectories()
            ProcessFiles(diNext, sw)
        Next diNext
    End Sub

End Class

0
 
psmithphilAuthor Commented:
I'm going to try this in the morning as I'm beat!   It looks right to me so I gladly award you the points.

I'll let you know how it goes asap.  Thank you!
0
 
psmithphilAuthor Commented:
Yup, works great!

You da' man!   Thank you!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now