File Reference Finder Console Application - Lookup for references - not the best way, but...
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sampleFiles As String() = File.ReadAllLines("C:\Users\guercheLE\Documents\Visual
Studio 2010\Projects\FileReferenceFinderConsoleApplication\FileReferenceFinderConsoleApplication\sample-nodes.txt")
Console.WriteLine("Looking for references on 'Sample' files...")
LookUpForReferences(sampleFiles.Select(Function(FullFileName) New FileData
With {.FullFileName =
FullFileName, .FileNameWithoutExtension
= Path.GetFileNameWithoutExtension(FullFileName)}), "C:\Users\guercheLE\Documents\Visual
Studio
2010\Projects\FileReferenceFinderConsoleApplication\FileReferenceFinderConsoleApplication\sample-edges.txt")
Console.WriteLine("Press any key to finish...")
Console.ReadKey()
End Sub
Private Sub LookUpForReferences(ByVal WhereToLookAt As IEnumerable(Of FileData),
ByVal WhereToWriteReferencesTo As
String)
LookUpForReferences(WhereToLookAt,
WhereToLookAt, WhereToWriteReferencesTo)
End Sub
Private Sub LookUpForReferences(ByVal WhereToLookAt As IEnumerable(Of FileData),
ByVal WhatToLookFor As IEnumerable(Of FileData),
ByVal WhereToWriteReferencesTo As
String)
Dim WhereToLookAtIndex As Integer
Dim WhereToLookAtCount As Integer
Dim FileContent As String
Dim ReferencesFound As List(Of String)
File.WriteAllText(WhereToWriteReferencesTo, "")
WhereToLookAtIndex = 0
WhereToLookAtCount = WhereToLookAt.Count()
For Each WhereToLookAtItem In WhereToLookAt
WhereToLookAtIndex += 1
Console.WriteLine("Looking at '{0}' [{1} of {2}]...", WhereToLookAtItem.FileNameWithoutExtension, WhereToLookAtIndex, WhereToLookAtCount)
If File.Exists(WhereToLookAtItem.FullFileName) Then
FileContent = File.ReadAllText(WhereToLookAtItem.FullFileName)
Do While
Regex.IsMatch(FileContent, "/\*[^\*]*\*/",
RegexOptions.Singleline)
FileContent = Regex.Replace(FileContent,
"/\*[^\*]*\*/", "", RegexOptions.Singleline)
Loop
Do While
Regex.IsMatch(FileContent, "^[^-]*--.*$",
RegexOptions.Multiline)
FileContent = Regex.Replace(FileContent,
"^([^-]*)--.*$", "$1", RegexOptions.Multiline)
Loop
Do While
Regex.IsMatch(FileContent, "^[^\/]*\/\/.*$",
RegexOptions.Multiline)
FileContent = Regex.Replace(FileContent,
"^([^\/]*)\/\/.*$", "$1", RegexOptions.Multiline)
Loop
ReferencesFound = New
List(Of String)
For Each
WhatLookForItem In WhatToLookFor
If WhereToLookAtItem.FileNameWithoutExtension
<> WhatLookForItem.FileNameWithoutExtension Then
'Console.WriteLine("Looking for
'{0}'...", WhatLookForItem.FileNameWithoutExtension)
If Regex.IsMatch(FileContent, String.Format("\b{0}\b", WhatLookForItem.FileNameWithoutExtension),
RegexOptions.IgnoreCase)
Then
'Console.WriteLine("'{0}'
found at '{1}'...", WhatLookForItem.FileNameWithoutExtension,
WhereToLookAtItem.FullFileName)
ReferencesFound.Add(String.Format("{1}{0}{2}", vbTab,
WhereToLookAtItem.FullFileName, WhatLookForItem.FullFileName))
End If
End If
Next
If ReferencesFound.Count()
> 0 Then
'Console.WriteLine("Appending
references to '{0}'...", WhereToWriteReferencesTo)
File.AppendAllLines(WhereToWriteReferencesTo, ReferencesFound)
End If
End If
Next
End Sub
Private Class FileData
Public Property FullFileName As String
Public Property FileNameWithoutExtension As
String
End Class
End Module