下午好,
我是 VB.net 的新手,正在为我的 VB.Net windows 窗体项目使用 Visual Studio Express 2012。
我需要一些关于我的代码的帮助,因为我不确定如何做我想做的事情。
场景是这样的:
用户在表单上选择一个目录并按下一个按钮。然后应用程序将查找一些文件并将它们移动到另一个目录。在该新目录中,它将找到包含三个字符代码的文件名。
然后,应用程序将为 xml 文件中的每个代码分配适当的 docgroup、doctype 和 dosubtype 值。然后将其输出到文本文件。
让应用程序知道要根据文件文件名使用的 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
谁能帮我完成这段代码?
亲切的问候,