find names in a directory
-
:^)I am very new to vb. I am trying to write an program which searches through all the word docs. in a directory. I have a list of 20 stirngs I am looking for. I am sure only 1 of the strings is found in each file. I have tried to write this several diferent ways but none works. I think I need two arrays and to loop through both and output the found strings, file names, and dates of the files to a spreadsheet. Any help would be greatly appreciated. Public Class Form1 'Create a list of doctors as an array. 'Create a list of files in the remote folder as an array 'Loop thru the “files array” and look for each doctor 'Write the doctor name, filedate, sysdate, and file name to a spreadsheet Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' For Each strFile As String In My.Computer.FileSystem.Getectories("C:\batch") 'Add the item to the list 'ListBox1.Items.Add(strFile) 'If String.Compare(strDoc) Then Dim doc1 As String = "ledford" Dim list As System.Collections.ObjectModel.ReadOnlyCollection(Of String) list = My.Computer.FileSystem.FindInFiles("C:\batch", doc1, True, FileIO.SearchOption.SearchTopLevelOnly) For Each name As String In list ListBox1.Items.Add(name) Next 'End If ''''''Assign the files found in the list above as a variable and replace for file1.log 'Dim information As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("C:\batch\file1.log") 'MsgBox("The file's full name is " & list.FullName & ".") 'MsgBox("Last access time is " & list.LastAccessTime & ".") ' Next End Sub 'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' Dim arrayDocs (of string) ' arrayDocs(0) = "sexton" ' arrayDocs(1) = "wargo" ' arrayDocs(2) = "ledford" ' arrayDocs(3) = "koepke" ' For Each strDoc As String In arrayDocs ' ListBox2.Items.Add(strDoc) ' Next 'End Sub End Class -Kevin
-
:^)I am very new to vb. I am trying to write an program which searches through all the word docs. in a directory. I have a list of 20 stirngs I am looking for. I am sure only 1 of the strings is found in each file. I have tried to write this several diferent ways but none works. I think I need two arrays and to loop through both and output the found strings, file names, and dates of the files to a spreadsheet. Any help would be greatly appreciated. Public Class Form1 'Create a list of doctors as an array. 'Create a list of files in the remote folder as an array 'Loop thru the “files array” and look for each doctor 'Write the doctor name, filedate, sysdate, and file name to a spreadsheet Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' For Each strFile As String In My.Computer.FileSystem.Getectories("C:\batch") 'Add the item to the list 'ListBox1.Items.Add(strFile) 'If String.Compare(strDoc) Then Dim doc1 As String = "ledford" Dim list As System.Collections.ObjectModel.ReadOnlyCollection(Of String) list = My.Computer.FileSystem.FindInFiles("C:\batch", doc1, True, FileIO.SearchOption.SearchTopLevelOnly) For Each name As String In list ListBox1.Items.Add(name) Next 'End If ''''''Assign the files found in the list above as a variable and replace for file1.log 'Dim information As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("C:\batch\file1.log") 'MsgBox("The file's full name is " & list.FullName & ".") 'MsgBox("Last access time is " & list.LastAccessTime & ".") ' Next End Sub 'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' Dim arrayDocs (of string) ' arrayDocs(0) = "sexton" ' arrayDocs(1) = "wargo" ' arrayDocs(2) = "ledford" ' arrayDocs(3) = "koepke" ' For Each strDoc As String In arrayDocs ' ListBox2.Items.Add(strDoc) ' Next 'End Sub End Class -Kevin
This is how I might go about it. Hope it gives you the right idea.
Const directoryPath As String = "C:\\test" ' The directory to look in Dim desiredFiles As New List(Of String) ' List of files we're looking for With desiredFiles ' Note I didn't use file extensions. Change that if you'd like. .Add("File1") ' but you'll need to tweak the code below if you do. .Add("File2") End With ' METHOD 1 ' Create a DirectoryInfo object for the desired directory ' to look in. Dim info As New IO.DirectoryInfo(directoryPath) ' Get all the \*.doc files in the directory For Each file As IO.FileInfo In info.GetFiles("\*.doc") ' The contains method is case sensitive. If you want a case-insentive ' search you can add the desired file names in all lower or uppercase and then use the ' ToLower or ToUpper methods on the file name If desiredFiles.Contains(IO.Path.GetFileNameWithoutExtension(file.Name)) Then ' The file name was found in our list MsgBox("Directory contained file: " & file.Name) End If Next ' METHOD 2 For Each file As String In desiredFiles If IO.File.Exists(IO.Path.Combine(directoryPath, file & ".doc")) Then MsgBox("Directory contained file: " & file) End If Next