使用VB创建将集合文件从一个位置移动到另一个位置的应用程序

时间:2013-08-09 15:00:49

标签: vb.net search copy unzip

所以这是我必须创建的一个应用程序,它将文件从一个目录移动到另一个目录,这两个目录都是由用户指定的。这看起来很容易,但问题是需要搜索数百万个文件。这些文件都以8位数命名,例如“98938495.crt”。所有这些文件所在的目录都有多个文件夹。主要文件夹中的这些文件夹以文件夹中所有文件的前两位数命名。然后在该文件夹中大约有十个压缩文件夹,每个文件夹包含100,000个文件。这些文件夹的名称是文件的最小名称和最大名称。例如,我进入主文件夹,然后单击“90xx”文件夹。在那一个有10个压缩文件夹,以文件的最小和最大名称命名,如“90000000_90099999.zip”。该zip文件夹包含100000个文件。现在,这个应用程序应该找到用户输入的所有文件,然后将它们移动到用户指定的文件夹。我到目前为止已经发布了我的代码,任何帮助都非常感谢!!

仅供参考:STID是文件的名称。

GUI for the app

编辑:我意识到没有办法回答这个问题,因为确实没有问题,只是一个破碎的应用程序。基本上,我如何搜索目录中的项目,然后将它们复制到新目录?

Imports System
Imports System.IO


Public Class CertFinder

Private Sub SourceDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceDirectoryTB.TextChanged

End Sub

Private Sub DestinationDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationDirectoryTB.TextChanged

End Sub

Private Sub SearchTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchTB.TextChanged


End Sub

Private Sub SourceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceButton.Click

    Dim fbd As New FolderBrowserDialog
    fbd.RootFolder = Environment.SpecialFolder.MyComputer
    If fbd.ShowDialog = DialogResult.OK Then
        SourceDirectoryTB.Text = fbd.SelectedPath

    End If

End Sub

Private Sub DestinationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationButton.Click

    Dim fbd As New FolderBrowserDialog
    fbd.RootFolder = Environment.SpecialFolder.MyComputer
    If fbd.ShowDialog = DialogResult.OK Then
        DestinationDirectoryTB.Text = fbd.SelectedPath
    End If

End Sub




Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click

    'Array of stids
    Dim stidsToFind As String()

    'get text in the searchTB textbox
    Dim searchTBText As String = Me.SearchTB.Text.Trim()

    'splits stids into seperate lines
    stidsToFind = searchTBText.Split(vbCrLf)

    'gets text from source directory text box
    Dim sourceDirectory As String = Me.SourceDirectoryTB.Text.Trim()

    'gets text from destination directory text box
    Dim destinationDirectory As String = Me.DestinationDirectoryTB.Text.Trim()

    Dim fullPathToFile As String = sourceDirectory

    'Go through each stid in the search text box and continue if nothing
    For Each stidToFind As String In stidsToFind

        If String.IsNullOrWhiteSpace(stidToFind) Then
            Continue For
        End If


        'Find the first two digits of the stid
        Dim firstTwoDigitsOfSTID As String = stidToFind.Substring(0, 2)

        'In the specified directory, find the folder with the first two digits and "xx"
        fullPathToFile = fullPathToFile & "\" & firstTwoDigitsOfSTID & "xx"



        Dim allFileNames As String() = Nothing
        allFileNames = Directory.GetFiles(sourceDirectory, "*.crt*", SearchOption.AllDirectories)

    Next

    '-------------------------------------------------------------------------------

    Try
        If File.Exists(fullPathToFile) = False Then
            Dim FS As FileStream = File.Create(fullPathToFile)
            FS.Close()
        End If


        File.Move(fullPathToFile, destinationDirectory)
        Console.WriteLine("{0} moved to {1}", fullPathToFile, destinationDirectory)

    Catch ex As Exception

        MessageBox.Show("File does not exist")

    End Try




    Dim sc As New Shell32.Shell()

    'Declare the folder where the files will be extracted
    Dim output As Shell32.Folder = sc.NameSpace(destinationDirectory)
    'Declare your input zip file as folder  .
    Dim input As Shell32.Folder = sc.NameSpace(stidsToFind)
    'Extract the files from the zip file using the CopyHere command .
    output.CopyHere(input.Items, 4)

    '-------------------------------------------------------------------------------



    ' 1. Get the names of each .zip file in the folder.
    ' 2. Assume that the .zip files are named as such <minimum STID>_<maximum STID>.zip
    ' 3. For each .zip file name, get the Minimum STID and Maximum STID values.
    ' 4. Check the range of 'stidToFind' against the STID ranges of each .zip file.
    ' 5. Once the .zip file is located, 
    '   a. Copy the .zip file to the local computer OR
    '   b. Just leave it where it is.
    ' 6. Unzip the file.
    ' 7. Create the full file name path. ../<stidToFind>.crt
    ' 8. Copy the file to the destination directory.
    ' 9. Delete the unzipped folder
    ' 10. Delete the .zip file (IF you've copied it to your local computer).








End Sub

结束班

1 个答案:

答案 0 :(得分:0)

  1. 在回答.ZIP解包时,请使用System.IO.Packaging(更多信息可在CodeProject - Zip Files Easy找到)
  2. 您是否认为使用数据库会更容易存储该数据?