Click to See Complete Forum and Search --> : File Export Question?


mp_direct
05-26-2005, 07:03 PM
Hi all,

In my program i have 2 folders. FolderA and FolderB.

In FolderA i have my index file
In FolderB i have my images.

My program reads a line of index data, gets the image and stores it away with its its index data into a document management program and deletes the image and so on until its read the whole index file.

each line in my index file correspond to one image only. i.e. index line 1 would match up to 1.tif, second line would match up to 2.tif and so on........

When i have 10 or more images thats when my i have the problem.

My images are numbered 1.tif, 2tif ........... 100.tif.

so for example if i have 11 images, numberd 1.tif - 11.tif

I have written some code which reads each filename and stores it into an array

Dim listnames As New ArrayList

For Each f As FileInfo In Dir.GetFiles()

listnames.Add(f.Name)

Next

The program reads the first name 1.tif, but instead or reading 2.tif it reads 10.tif, 11.tifand then goes back and reads the names 2.tif, 3.tif ............... This is causing my index data and my images to not match. :mad:


Basically its not picking files inorder i.e. 1,2,3,4,5,6,7,8,9,10,11 .
but picking then in 1,10,11,2,3,4,5,6,7,8,9

Hope i have explained it well.

Any thoughts or comments would be very greatly appreciated.

Phil Weber
05-26-2005, 09:18 PM
It's sorting the names alphabetically, rather than numerically. If you control the filenames, you can add leading zeroes before the single-digit names, e.g., 01.tif, 02.tif, etc. If you cannot change the filenames, you can read them into an integer array and sort the array, then generate filenames from the sorted array, e.g., FileName = CStr(IntArray(I)) & ".tif"

mp_direct
05-27-2005, 05:25 AM
How could i put my filenames into an integer array and sort them? Any ideas

mp_direct
05-27-2005, 06:26 AM
i have an array which contains the names: 1.tif, 10.tif, 11.tif, i.e. not in numerical order.

How could i put this into another array but it being in numerical order i.e. 1.tif, 2.tif ................

Sync
05-27-2005, 07:05 AM
Public Class frmNewFeature
Inherits System.Windows.Forms.Form

Public Class myReverserClass
Implements IComparer

' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(ByVal x As [Object], ByVal y As [Object]) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare

End Class 'myReverserClass
Private Sub frmNewFeature_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Creates and initializes a new Array and a new custom comparer.
Dim myKeys As [String]() = {"red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange"}
Dim myValues As [String]() = {"1.tif", "100.tif", "2.tif", "10.tif", "3.tif", "200.tif", "1000.tif"}
Dim myComparer = New myReverserClass

' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintKeysAndValues(myKeys, myValues)

' Sorts a section of the Array using the default comparer.
Array.Sort(myKeys, myValues, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintKeysAndValues(myKeys, myValues)

' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myKeys, myValues, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintKeysAndValues(myKeys, myValues)

' Sorts the entire Array using the default comparer.
Array.Sort(myKeys, myValues)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintKeysAndValues(myKeys, myValues)

' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myKeys, myValues, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintKeysAndValues(myKeys, myValues)

End Sub
Public Shared Sub PrintKeysAndValues(ByVal myKeys() As [String], ByVal myValues() As [String])

Dim i As Integer
For i = 0 To myKeys.Length - 1
Console.WriteLine(" {0,-10}: {1}", myKeys(i), myValues(i))
Next i
Console.WriteLine()

End Sub 'PrintKeysAndValues
End Class

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemArrayClassSortTopic.asp

What do you say?

mp_direct
05-27-2005, 07:41 AM
cant get it to work, i am not gettin the numbers 2.tif, 2.tif in order

Sync
05-27-2005, 08:23 AM
Yeah. Sorry!
Pls tell me all of file extensions are the same or not?

I'll try it.

Phil Weber
05-27-2005, 08:24 AM
Dim di As New DirectoryInfo("d:\path")
Dim fileNames() As Integer
ReDim fileNames(di.GetFiles.Length - 1)

Dim I As Integer
For Each fi As FileInfo In di.GetFiles()
fileNames(I) = Convert.ToInt32(Val(fi.Name))
I += 1
Next
Array.Sort(fileNames)

Sync
05-27-2005, 08:38 AM
Hi,


Dim myValues As [String]() = {"1.tif", "100.tif", "2.tif", "10.tif", "3.tif", "2.tif", "1000.tif"}
Dim var As Int32()
ReDim var(myValues.Length - 1)
Dim j As Integer
For j = 0 To myValues.Length - 1
Dim v As String() = Split(myValues(j), ".")
var(j) = CType(v(0).Trim, Integer)
Next j
Array.Sort(var)
Dim i As Integer
For i = 0 To var.Length - 1
Console.WriteLine(var(i) & ".tif" & vbCrLf)
Next i


Actually, It's not good code. :(

mp_direct
05-27-2005, 09:20 AM
How can i generate the filenames if i use the Code posted by Phil Webber?

mp_direct
05-27-2005, 09:47 AM
sync, your code works fine, but instead of having:
Dim myValues As [String]() = {"1.tif", "100.tif", "2.tif", "10.tif", "3.tif", "2.tif", "1000.tif"}

i want to read filenames from the directory FolderA as shown below.

Dim Dir As New DirectoryInfo("D:\FolderA\")
Dim listnames As New ArrayList

For Each f As FileInfo In Dir.GetFiles()

listnames.Add(f.Name)
Next

Any thoughts on how i could achieve this?

mp_direct
05-27-2005, 05:12 PM
thanks for all your assistance, i ended up simply adding an extra field onto my indexdata (image path and name) which read "D:\Images\2.tif etc, so i got rid of the problem and it picked the right image.