0

EDIT: Ok, it's because I'm using 2.0 Framework. Any ideas how I can modify that area or alternatives? I have to, unfortunately, stick with 2.0

Dim dir2 As New DirectoryInfo("d:\input")
    Dim sw2 As New StreamWriter("d:\input\reportCond.txt")
    For Each fi2 As FileInfo In dir2.GetFiles("report.txt")
        Dim sr2 As New StreamReader(fi2.FullName)
        While Not sr2.EndOfStream
            Dim sLine As String = sr2.ReadLine
            Dim myPath As String = sLine
            Dim fileName As String =System.IO.Path.GetFileNameWithoutExtension(myPath)
            Dim letters As String = fileName.Where(Function(c) Char.IsLetter(c)).ToArray
            Dim comp As String = sLine.Substring(28)
            sw2.WriteLine(letters)
        End While
    Next

The Code above was working fine yesterday, today it doesn't and I can't figure out why. The only difference was yesterday I ran it on VS2013, today it doesn't work on VS2010.

I get an error on Function saying "expression expected"

And another one on sw2.WriteLine(letters) saying "Name 'letters' is not declared.

Gmac
  • 169
  • 2
  • 14

3 Answers3

3

Here is how the linq Where part can be replaced to get the letters variable - which contains characters from the file name.

dim c as char
Dim letters As String 
for each c in s
    if char.IsLetter(c)
        letters += c
    end if
next
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
1

I would use the StringBuilder class to build the string:

    Dim dir2 As New DirectoryInfo("d:\input")
    Dim sw2 As New StreamWriter("d:\input\reportCond.txt")
    For Each fi2 As FileInfo In dir2.GetFiles("report.txt")
        Dim sr2 As New StreamReader(fi2.FullName)
        While Not sr2.EndOfStream
            Dim sLine As String = sr2.ReadLine
            Dim myPath As String = sLine
            Dim fileName As String =System.IO.Path.GetFileNameWithoutExtension(myPath)

            ' Build string
            Dim sb As New System.Text.StringBuilder()
            For Each c As Char In fileName
                If Char.IsLetter(c) Then
                    sb.Append(c)
                End If
            Next

            Dim comp As String = sLine.Substring(28)

            ' Here's the string you built
            Dim letters As String = sb.ToString()
            sw2.WriteLine(letters)
        End While
    Next
Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
1

A .Net 2.0 framework alternative to the Where extension method could be:

Dim letters As String = Array.FindAll(fileName.ToCharArray(), AddressOf Char.IsLetter)

This will create a new array of chars whose elementes will be all chars of fileName.ToCharArray for wich Char.IsLetter is True

Josh Part
  • 2,154
  • 12
  • 15