【发布时间】:2013-06-19 16:04:48
【问题描述】:
下午好,
我是 VB.net 的新手,我正在为我的 VB.Net windows 窗体项目使用 Visual Studio Express 2012。
我的代码需要一些帮助,因为我不确定如何做我想做的事。
场景是这样的:
用户在表单上选择一个目录并按下一个按钮。然后应用程序将查找一些文件并将它们移动到另一个目录。在该新目录中,它将找到包含三个字符代码的文件名。
然后,应用程序将为 xml 文件中的每个代码分配适当的 docgroup、doctype 和 docsubtype 值。然后将其输出到文本文件。
让应用程序知道要使用的 xml 文件中的哪些 docgroup、doctype 和 docsubtype 值取决于文件文件名,这让我不知所措。
我的 xml 文件结构如下。请注意,这些值不是静态的,用户可以随时在我的设置表单中更改。
<Settings>
<ApplicationSettings>
<code>FTO</code>
<docgroup>Operations</docgroup>
<doctype>Funds Transfer</doctype>
<docsubtype>Out</docsubtype>
<code>FTI</code>
<docgroup>null</docgroup>
<doctype>null</doctype>
<docsubtype>null</docsubtype>
<code>ACL</code>
<docgroup>Documentation</docgroup>
<doctype>Client Documentation</doctype>
<docsubtype>Termination</docsubtype>
<code>TBA</code>
<docgroup>Operations</docgroup>
<doctype>Funds Transfer Credit</doctype>
<docsubtype>Reversed</docsubtype>
</ApplicationSettings>
</Settings>
例如:
用户选择\ServerA\ITDept\files 目录,该目录中的所有文件将始终具有以下命名约定:
AccNum-YYYYMMDD-code 示例:123456-20130610-FTO
\ServerA\ITDept\文件
12345-20130610-FTO 审核扫描.pdf
54265-20130512-FTI A1.pdf
45752-20121204-TBA.pdf
因此,如果我能弄清楚如何编写此代码,这些文件到文本文件的输出将如下所示:
\\ServerA\ITDept\files\12345-20130610-FTO Reviewed and scanned.pdf|12345|_||Operations| Funds Transfer|Out|swfoi6848484|06/10/2013|
\\ServerA\ITDept\files\54265-20130512-FTI A1.pdf|54265|_||NULL| NULL|NULL|swfoi15157|05/12/2013|
\\ServerA\ITDept\files\45752-20121204-TBA.pdf|45752|_||Operations|Funds Transfer|Reversed|swfoi54572258|12/04/2012|
目录中会有其他文件有其他代码,如果这些代码不存在于 xml 文件中,则应忽略这些代码。
我的代码如下。除了将 xml 文件中的特定 docgroup、doctype 和 docsubtype 值合并到输出文件中的最后一步,一切都“除了”。
Imports System
Imports System.Xml
Imports System.Text.RegularExpressions
Public Class Userform
Dim xmlfile As String = "\\ServerA\ITDept\XML\Settings.xml"
Private Sub Userform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Check if Setting.xml exists, if not show message box and close application.
If IO.File.Exists(xmlfile) = False Then
MessageBox.Show("Cannot locate Settings.xml file. Please contact IT Department for assistance.", "ERROR")
Me.Close()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub filebtn_Click(sender As Object, e As EventArgs) Handles filebtn.Click
'New thread will run main tasks of program
BackgroundWorker1.RunWorkerAsync()
End Sub
'Function used to get date in file name and use value as MM/DD/YYYY in output file
Private Function GetFormattedDateFromFileName(ByVal fileName As String) As String
Dim parts() As String = fileName.Split("-")
If parts.Length = 3 Then
Dim dt As DateTime
If DateTime.TryParseExact(parts(1), "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
Return dt.ToString("MM/dd/yyyy")
End If
End If
Return ""
End Function
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'create directory in input folder with timestamp as the directory name.
Dim destdir As String = [String].Format("\\ServerA\ITDept\files\{0}", DateTime.Now.ToString("MMddyyyyhhmmss"))
System.IO.Directory.CreateDirectory(destdir)
'read directory and look for filenames that match pattern and have code elements from xml file
Dim regElemName As New Regex("^code")
Dim root = XElement.Load(xmlfile)
Dim codeElements = root.Element("ApplicationSettings").Elements().Where(Function(xe) regElemName.IsMatch(xe.Name.LocalName)).Select(Function(xe) xe.Value)
Dim codes = String.Join("|", codeElements.ToArray())
Dim regFileName As New Regex(String.Format("^\d+\-(?<Year>(19|20)[0-9][0-9])(?<Month>0[1-9]|12|11|10)(?<Day>[12]\d|0[1-9]|3[01])\-{0}$", codes))
Dim files = IO.Directory.GetFiles(TextBox1.Text, "*.pdf", IO.SearchOption.TopDirectoryOnly).Where(Function(path) regFileName.IsMatch(IO.Path.GetFileName(path)))
For Each file As String In files
System.IO.File.Move(file, System.IO.Path.Combine(destdir, System.IO.Path.GetFileName(file)))
Next
'Define random numbers
Dim randomclass As New System.Random()
Dim randomnumber As Integer
'create txt file from destdir of all files for output.
Dim str As String = String.Empty
For Each rfiles As String In System.IO.Directory.GetFiles(destdir)
randomnumber = randomclass.Next(10000, 99999)
Dim formattedDate As String = GetFormattedDateFromFileName(rfiles)
str = str & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & "docgroup_value" & "|" & "doctype_value" & "|" & "docsubtype_value" & "|" & "swfoi" & randomnumber & "|" & formattedDate & "|" & Environment.NewLine
Next
Dim outputname As String = [String].Format("\\ServerA\ITDept\Index\swfoi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
System.IO.File.WriteAllText(outputname, str)
End Sub
End Class
谁能帮我完成这段代码?
亲切的问候, 一个
【问题讨论】:
-
不应该
docgroup、doctype和docsubtype是code的孩子,而不是兄弟姐妹? -
@Tim 不知道。正如我之前所说,我对 VB.net 很陌生。
-
您已经非常接近您想要做的事情了 - 我建议您更改几件事(除了 XML 格式),并且您缺少获得docgroup、doctype 和 dosubtype 值(我认为这是您真正需要帮助的内容)。如果其他人没有先解决这个问题,我会尝试今晚(西海岸时间)回来并提出一些建议。
-
@Tim Yep 这正是我在“获取 docgroup、doctype 和 dosubtype 值”方面寻求帮助的原因。抱歉,如果我在解释中到处都是,只是不知道如何解释。感谢您的帮助或其他任何想参与其中的人。
标签: vb.net