Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

My For Statement help

Posted on 2006-11-06
5
Medium Priority
?
177 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 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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