Solved

My For Statement help

Posted on 2006-11-06
5
169 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
Comment Utility
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
Comment Utility
can you show me a valid example?
0
 
LVL 5

Expert Comment

by:DEEPESH
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

8 Experts available now in Live!

Get 1:1 Help Now