How to verify files exist... kinda...

Hello,

I'm trying to verify that each file in a list exist. I can successfully read the text file (which contains the list of files) into an array. (Each containing one filepath.) Each path has quotes around it and I have easily removed them. I then created an array and put the file path to every file (in the specified directory) into each dimension. My problem now is, I need a way to compare the two lists, verifying that each file that exists is in the other list.

So I suppose that I am not looking for a way to verify files exist but rather that each file that does exist is in my list. Understand?

I have considered multiple ways of doing this but they have either failed or I have found that they are not what I am looking for. Maybe there is a way to check if the specified string (or file path rather) exists in the specified array (which would contain the list of file paths that need to be verified).

... I hope this isn't too confusing...


I am using

Dim DIAs New DirectoryInfo("M:\TMDocs")

Dim FileList()As FileInfo = DI.GetFiles("*.pdf", SearchOption.AllDirectories)

to find all the files in the directory and subdirectories.

[1827 byte] By [Mturco11x] at [2007-12-22]
# 1

For Each f As FileInfo In FileList

If File.Exists(f.FullName) Then

'DoSomething

End If

Next

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

... Wouldn't that just check to see if a existing file exists?

FileList is a array containing the file paths of existing files. (Look in the code I supplied above.) I'm sorry I am very unclear but this is not the answer to my question.

I have two arrays. (FileList and UnverifiedFileList.) FileList contains the file paths of files that exist. UnverifiedFileList contains the file paths that an application (Time Matters) spits out. Time Matters is simply a database front-end each filepath in Time Matters is an 'entry' that is supposed to exist. Now, I have already wrote an app that makes sure that the entries in Time Matters are valid. I need an app that makes sure that every file in the specified directory has an entry in Time Matters.

I am exporting a list of entries from Time Matters and trying to compare it to exist files. But thank you very much for your support. Hopefully you understand me... (it's not easy).

Mturco11x at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

So if I'm right on what your asking.

You have two arrays (FileList and UnverifiedFileList).

FileList Contains files that exists
UnverifiedFileList contains a list the application spits out.

Are you just trying to confirm that the entries in UnverifiedFileList exist in FileList ? In which case this really has nothing to do with files - its just verifying items exist in arrays.

spotty at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Mturco11x wrote:
I need an app that makes sure that every file in the specified directory has an entry in Time Matters.

It is still the same principle....Get a list of Strings out put from Time matters and then do your comparison:

Dim TimeMatters As List(Of String) 'Need to fill with relevant data

For Each f As FileInfo In FileList

If TimeMatters.Contains(f.FullName) Then

'DoSomething

Else

'DoSomething Else

End If

Next

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

Thank you very much, I believe this will do.

I do have one more minor question however, did you use a 'List(Of String)' because you needed the '.Contains' method? I just want to know so I can apply it to future apps. Thanks again!

Edit:

Sorry, spotty I didn't see your post until now. Well yes. This doesn't have anything to do with making sure files exist. If it did I'd simple put a File.Exists() in a For... Next loop. However I'd like to do the opposite.

Though, you said "Are you just trying to confirm that the entries in UnverifiedFileList exist in FileList ?", the answer to that is no. Other wya around. I want to confirm that the files in FileList exist in UnverifiedFileList. But thanks anyway.


2nd Edit:

Okay, the problem with using a list is that the method of reading the file paths (from Time Matters) into an array was File.ReadAllLines(). But, that will not work with a list...

Original code:

TimeMatters = File.ReadAllLines("File path of export file")

That will no longer work, do I need to use a loop to individually add file paths to the list? (I'm sorry I have very, very little knowledge of lists.... in fact I've never used one before but then again I've only been programming for 2 weeks.)

Mturco11x at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

Mturco11x wrote:
I do have one more minor question however, did you use a 'List(Of String)' because you needed the '.Contains' method? I just want to know so I can apply it to future apps.

The contains method is one good reason...but another is the managability of the list as compared to the array. You do not have to worry about explicitly stating the bounds of a list... I personally like working with "list" and "collections" (which includes hashtables and dictionaries as well as a generic collection to inherit from) over the array

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
I see... I suppose I should learn about lists... (I am familiar with collections). Well thanks. I guess you posted before I edited my previous post for the 2nd time. Meaning you didn't catch my next question? Lol, sorry.
Mturco11x at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
Mturco11x wrote:

Okay, the problem with using a list is that the method of reading the file paths (from Time Matters) into an array was File.ReadAllLines(). But, that will not work with a list...

Original code: TimeMatters = File.ReadAllLines("File path of export file")

That will no longer work, do I need to use a loop to individually add file paths to the list? (I'm sorry I have very, very little knowledge of lists.... in fact I've never used one before but then again I've only been programming for 2 weeks.)

Dim DI As New DirectoryInfo("M:\TMDocs")

Dim FileList() As FileInfo = DI.GetFiles("*.pdf", SearchOption.AllDirectories)

Dim TimeMatters As New List(Of String)

Dim tFr As StreamReader

tFr = My.Computer.FileSystem.OpenTextFileReader("fileasstring")

Do While Not tFr.EndOfStream

TimeMatters.Add(tFr.ReadLine)

Loop

tFr.Close()

For Each f As FileInfo In FileList

If TimeMatters.Contains(f.FullName) Then

'DoSomething

Else

'DoSomething Else

End If

Next

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

Okay, thank you very, very much. I believe this is the final answer. Thank you to everyone who helped.

Mturco11x at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

For the record, one of the the List(Of String) constructor overloads would allow you to pass in you source array from ReadAllLines, this would save you the hassel of populating it manually. Pseudo (don't have editor and intellisense handy) code:

Dim myList As List(Of String)

myList = New List(Of String)(ReadAllLines)

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
Interesting... thanks for the tip, I'll keep it in mind.
Mturco11x at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...