1

こんにちは、

私はVB.netを初めて使用し、VB.Net WindowsフォームプロジェクトにVisual Studio Express 2012を使用しています。

やりたいことを実行する方法がわからないため、コードの助けが必要です。

シナリオは次のとおりです。

ユーザーはフォーム上でディレクトリを選択し、ボタンを押します。次に、アプリケーションはいくつかのファイルを探して、それらを別のディレクトリに移動します。その新しいディレクトリで、3 文字のコードを含むファイル名が検索されます。

次にアプリケーションは、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\files

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

このコードを完成させるのを手伝ってくれる人はいますか?

敬具、A

4

1 に答える 1