Vb.net 读取5到10位帐号的文件名,这可能吗?
我试图在VB.net中编写一个程序,将一些值输出到文本文件中。请耐心等待我,因为我对VB.net非常陌生 到目前为止,我掌握的情况如下:Vb.net 读取5到10位帐号的文件名,这可能吗?,vb.net,visual-studio-2012,visual-studio-express,Vb.net,Visual Studio 2012,Visual Studio Express,我试图在VB.net中编写一个程序,将一些值输出到文本文件中。请耐心等待我,因为我对VB.net非常陌生 到目前为止,我掌握的情况如下: Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dim str As String For Each File As String In System.IO.Directory.GetFiles(TextBox1.Text)
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim str As String
For Each File As String In System.IO.Directory.GetFiles(TextBox1.Text)
str = str & File & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "||" & DateTimePicker1.Text & "|" & Environment.NewLine
Next
System.IO.File.WriteAllText("C:\output\output.txt", str)
End Sub
单击按钮3时输出文件(output.txt)的结果如下:
C:\DirectoryTest\Clients\2356851-Kathy Winkler - Family Investments.pdf|2356851|2356851||04/10/2013|
C:\DirectoryTest\Clients\58736 -Katrina Armon - Sandlewood Homes Co.pdf|58736|58736||04/10/2013|
C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf|Karen Cooper|Karen Cooper||04/10/2013|
到目前为止,我的代码完全符合我的要求,唯一的问题是我想让代码更智能,但不知道如何做到。更聪明的是,有没有一种方法可以让下面的代码只拾取文件名中显示的5到10位数的帐号,如果文件名中不存在帐号,则会弹出一个消息框
System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim
从输出的最后一行可以看到
C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf|Karen Cooper|Karen Cooper||04/10/2013|
…客户名称“Karen Cooper”显示在应显示帐号的两个区域中。这就是为什么我需要让这段代码变得更智能,让它在文件名中搜索5到10位数的帐号,然后显示在文件名后面,如其他2个示例所示
如果可能的话,请告诉我。如果您有任何问题,请务必告诉我。您的文件名如何
C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf
公平地说,应该是这样
C:\DirectoryTest\Clients\001548 - Karen Cooper - Famtime.pdf
你应该试试这个。我还没有机会测试它,但它应该可以工作
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim str As String
For Each File As String In System.IO.Directory.GetFiles(TextBox1.Text)
Dim strs as String() = System.IO.Path.GetFileNameWithoutExtension(File).Split("-")
Dim AccountNum as int = 0
For each section in strs()
' Loop through each section separated by - and try to cast it to an int
' you may want to use cLong instead
Try
AccountNum = cInt(section.trim())
exit for
Catch
End Try
Next
' DO LOGIC HERE TO BUILD OUTPUT with the account num now known
Next
System.IO.File.WriteAllText("C:\output\output.txt", str)
End Sub
我建议使用RegEx提取帐号。使用RegEx的一个附带好处是,您可以将RegEx模式存储在代码之外,例如在配置文件中,因此,如果您需要修改该模式,则无需重新编译应用程序即可轻松完成
Function GetAccountNumber(fileName As String) As String
Dim pattern As String = ".*?(?<acct>\d{5,10}).*?"
Dim regEx As New Regex(pattern)
Dim match As Match = regEx.Match(fileName)
Dim accountNumber As String = Nothing
If match.Success Then
Dim group As Group = match.Groups("acct")
If group.Success Then
accountNumber = group.Value
End If
End If
Return accountNumber
End Function
函数GetAccountNumber(文件名为字符串)为字符串
Dim模式为字符串=“*?(?\d{5,10})。*?”
Dim正则表达式作为新正则表达式(模式)
Dim match As match=regEx.match(文件名)
将accountNumber设置为字符串=无
如果匹配,那么成功
Dim group As group=match.Groups(“账户”)
如果是团体,那就成功吧
accountNumber=组值
如果结束
如果结束
返回帐号
端函数
在上面的示例中,我使用以下正则表达式模式查找字符串中的五到十位数:
*?(?\d{5,10})。*?
模式开头和结尾的*?
表示任意字符,任意次数。问号表示它不贪婪。换句话说,它只匹配所需数量的字符。通过使其非贪婪,它不会从帐号中窃取任何数字
括号围绕着我们要查找的字符串部分(帐号)。括号组开头的
?
是一个名称,我们可以通过它来引用它。在本例中,我将组命名为acct
。\d
表示任何数字字符。{5,10}
表示重复5到10次。这里是一些简单的逻辑。。。。当然,你可以先找到文件名,但现在你可以了
Dim returnval As String = ""
Dim s As String = "C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf|Karen Cooper|Karen Cooper||04/10/2013|"
For Each p As String In s
If IsNumeric(p) Then
returnval += p
Else
'MsgBox("no")
End If
Next
msgbox(returnval)将保存您的所有数字5-10,这取决于您希望从这里获得的具体信息
拆分文件名的步骤
'This will extract and return the filename from the specified path and filename.
'
Dim filePath As String = "c:\MyDirectory\MYFile.txt"
Dim slashPosition As Integer = filePath.LastIndexOf("\")
Dim filenameOnly As String = filePAth.Substring(slashPosition + 1)
MsgBox(filenameOnly)
*FOUND AT LINK http://www.vbforfree.com/274/extract-and-retrieve-the-filename-from-a-path/*
然后根据需要从那里操作字符串是的,修复文件名可以解决此问题。但是我不能依靠我的用户来确保文件名总是这样。这就是为什么我需要一些智能代码来检查这一点。有可能完成我的要求吗?@Andrea:我的2c-用户总是比你的程序更聪明,所以通常都有培训来帮助工作流程。@Andrea:所以,你必须制作一个解析/拆分文件名的函数。。他们的名字里没有数字谢谢Dan的回复但我不确定如何通过你的调整为我的输出建立逻辑。我有这个,但它不起作用:('code'str=str()&File&“|”&AccountNum&“|”&AccountNum&“| |”&DataGridView1.SelectedRows(0).Cells(0).Value&“|”&DataGridView1.SelectedRows(0).Cells(2).Value&“|”&DateTimePicker1.Text&”&Environment.NewLine'code'抱歉,使用Str使用上面修改的代码(将Str更改为section)我在生成代码时遇到问题。我收到一个错误,不确定为什么在“strs()中的每个section”上出现错误,错误是“索引数小于索引数组的维数”。不知道这意味着什么。我构建了我的逻辑,即'str=str&File&“|”&AccountNum&“|”&“&u124;”&“&u124;”&“&DataGridView1.SelectedRows(0).Cells(0).Value&“&DataGridView1.SelectedRows(0).Cells(1).Value&“&DataGridView1.SelectedRows(0).Cells(2).Value&“&”&“&DateTimePicker1.Text&&”&Environment.NewLine'有什么想法吗?