psmithphil
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.2 006JAN.103 3/cpref/ht ml/frlrfSy stemIODire ctoryInfoC lassGetDir ectoriesTo pic.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.20 06JAN.1033 /cpref/htm l/frlrfSys temIODirec toryInfoCl assGetDire ctoriesTop ic.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(Cont rolChars.C rLf & "Log Entry : ")
objStreamWriter.WriteLine( "{0} {1}", DateTime.Now.ToLongTimeStr ing(), DateTime.Now.ToLongDateStr ing() & 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.GetFileName WithoutExt ension(Fi. FullName) 'Filename without extension.
bitMap = New Bitmap(Fi.FullName)
frameDimension = New FrameDimension(bitMap.Fram eDimension sList(0))
framesCount = bitMap.GetFrameCount(frame Dimension)
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.F ullName, 0, 0, page.Width, page.Height, 0, KeepAspectRatio.KeepNone, 1, i)
Next i
newPdfFilePathAndName = theDirectory & filenameWithoutExtension & ".pdf"
Doc.SaveToFile(newPdfFileP athAndName )
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( newPdfFile PathAndNam e) 'fullPathAndPDF
End If
Next Fi
Next diNext
End Sub
Thank you for your help!
I used the VS2003 Help (ms-help://MS.VSCC.2003/MS
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.
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(Cont
objStreamWriter.WriteLine(
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.GetFileName
bitMap = New Bitmap(Fi.FullName)
frameDimension = New FrameDimension(bitMap.Fram
framesCount = bitMap.GetFrameCount(frame
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.F
Next i
newPdfFilePathAndName = theDirectory & filenameWithoutExtension & ".pdf"
Doc.SaveToFile(newPdfFileP
txtConvertedFiles.Text &= newPdfFilePathAndName & vbCrLf
txtConvertedFiles.Refresh(
ctrNumberOfFiles = ctrNumberOfFiles + 1 'Keep track of the number of files converted.
lblConvertedFiles.Text = "Converted Files: " & ctrNumberOfFiles.ToString
lblConvertedFiles.Refresh(
'Now write to the log file:
'objStreamWriter.WriteLine
objStreamWriter.WriteLine(
End If
Next Fi
Next diNext
End Sub
Thank you for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yup, works great!
You da' man! Thank you!
You da' man! Thank you!
ASKER
I'll let you know how it goes asap. Thank you!