Solved

My For Statement help

Posted on 2006-11-06
5
170 Views
Last Modified: 2010-04-23
I have folders and subfolders to search for a particular file,
1. if a match is found stop and write the node. -this is done.

I need help on 2 and 3?

2. after searching all folders, if folder is empty write node "Not Analyzed"
                        newChildNode1 = doc.CreateElement("file_mask")
                        newChildNode1.InnerText = "Not Analyzed"
                        ndData.AppendChild(newChildNode1)
                        ndMeta.AppendChild(ndData)
3. after searching all folders, if file has no match write
                        newChildNode1 = doc.CreateElement("file_mask")
                        newChildNode1.InnerText = "Not Analyzed"
                        ndData.AppendChild(newChildNode1)
                        ndMeta.AppendChild(ndData)

so my problem is that i have a for statement that checks files for match but i need a way to produced the above for every time there is no match or the folder is empty.  if there is a match then do the code below.



                For Each fInfo As FileInfo In sFiles
                    sMatch = fInfo.Name Like CaseNo & "*"
                    If sMatch = True Then
                        newChildNode1 = doc.CreateElement("file_mask")
                        newChildNode1.InnerText = fInfo.DirectoryName & "\" & CaseNo & ".*"
                        ndData.AppendChild(newChildNode1)
                        ndMeta.AppendChild(ndData)
                        Exit For
                    End If

                Next
                For Each dir As DirectoryInfo In dirs
                    CreateListModeData(ndMeta, dir.FullName)
                Next
0
Comment
Question by:Padre_Corleone
  • 2
5 Comments
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17887488
Looking on the above code I suggest why dont you use File Exists command such as
System.IO.File.Exists()
It takes path, so it will be easy for you to search the code
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17887491
can you show me a valid example?
0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17887642
Something like this

        Dim directory As String
        Dim arr() As String
        arr = IO.Directory.GetDirectories("c:\")
        For Each directory In arr
            If System.IO.File.Exists(directory & "\my.txt") Then
                MsgBox("File Found")
            End If
        Next

this will navigate to all directries on c: and promt a message if file is found, this will go for only one level if you want you can loop to sub directories
0
 
LVL 9

Accepted Solution

by:
DjDezmond earned 500 total points
ID: 17887778
If you add Deepish's code to a sub, and call the sub from within the code, it will search for all directories... It will probably freeze your app while its searching, so you may need to execute it on a seperate thread, but it depends on your app...

Sub SearchDirs(DirectoryToSearch)
        Dim directory As String
        Dim arr() As String
        arr = IO.Directory.GetDirectories(DirectoryToSearch)
        For Each directory In arr
            If System.IO.File.Exists(directory & "\my.txt") Then
                DoIfFound(Directory)
            Else
                DoIfNotFound(Directory)
            End If
          SearchDirs(Directory)
        Next
End Sub

Sub DoIfFound(DirFoundIn)
  'This will be done everytime the file is found in a directory.
  DirFoundIn = 'This will be directory it was found in.
End Sub

Sub DoIfNotFound(DirNotFoundIn)
 'This will be done everytime a directory does not contain the file.
  DirNotFoundIn = 'The directory it wasn't found in.
End Sub

I havn't tested this, but im sure Deepish will be able to spot any mistakes if there are any
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now