Solved

My For Statement help

Posted on 2006-11-06
5
172 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Show/Hide ASP.NET controls with JQuery best practices 3 90
Help with error when uploading excel file 3 37
location of a form 2 25
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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