2017-05-24 57 views
1

下午好,如何從文件名中提取客戶端號碼

我正在爲此奮鬥(VB.Net)並需要一些幫助。

我在一個目錄中有大約800個文件,我想獲取文件名並從中提取客戶端號碼。

的文件是什麼樣子目錄中的一個例子是如下:

Client 11111 Proposal 47241 inputted.msg Client 22222 Proposal 88754 inputted.msg Interest Portfolio 5514720 inputted for Client 33333.msg Investment Proposal 0987654 inputted for Client 34343.msg Investment Proposal 1234567 inputted for Client 33333.msg Investment Proposal 7456781 inputted for Client 66666.msg

當運行代碼,它應該得到下面的結果:

11111 22222 33333 34343 33333 66666

下面是我正在使用的代碼:

Dim path = txtWatchPath.Text 
    Dim files = Directory.GetFiles(path, "*.msg") 

    Dim expr = New Regex("\bClient\b\s\d{5}") 

    For Each file In files 
     If expr.IsMatch(file) Then 
      lsbxLog.Items.Add(file) 
     End If 
    Next 

但是從這段代碼的輸出顯示如下:

C:\Temp\Client 11111 Proposal 47241 inputted.msg 
C:\Temp\Client 22222 Proposal 88754 inputted.msg 
C:\Temp\Interest Portfolio 5514720 inputted for Client 33333.msg 
C:\Temp\Investment Proposal 0987654 inputted for Client 34343.msg 
C:\Temp\Investment Proposal 1234567 inputted for Client 33333.msg 
C:\Temp\Investment Proposal 7456781 inputted for Client 66666.msg 

有人可以幫我嗎,我只需要在文件名中的客戶端編號。

親切的問候,一個

回答

0

假設客戶總是5長度:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    Dim filenames As New List(Of String) 
    filenames.Add("Client 11111 Proposal 47241 inputted.msg") 
    filenames.Add("Client 22222 Proposal 88754 inputted.msg") 
    filenames.Add("Interest Portfolio 5514720 inputted for Client 33333.msg") 
    filenames.Add("Investment Proposal 0987654 inputted for Client 34343.msg") 



    For Each fileName In filenames 
     MessageBox.Show(GetClientName(fileName)) 
    Next fileName 


End Sub 

Private Function GetClientName(fileName As String) As String 
    Dim leadingString = "Client " 
    Dim startPos = fileName.IndexOf(leadingString) + leadingString.Length 
    Dim parsedString = fileName.Substring(startPos, 5) 

    Return parsedString 


End Function 
+0

非常感謝你。 – Andrea