Link to home
Start Free TrialLog in
Avatar of psmithphil
psmithphilFlag for United States of America

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of psmithphil

ASKER

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!
Yup, works great!

You da' man!   Thank you!