Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

My For Statement help

Posted on 2006-11-06
5
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 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