0

所以我正在开发一个桌面清洁器,它根据扩展名将项目移动到特定目录(正在清理的目录)

用户定义将放置项目的文件夹的名称。

但是移动文件的代码不能正常工作。

Private Sub FlowButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlowButton1.Click
    If CheckBox1.Checked Then
        Dim folderPathTextBox = options.TextBox1.Text
        Dim files() As String
        files = System.IO.Directory.GetFiles(options.FolderBrowserDialog1.SelectedPath, "*.txt")

        For Each file As String In files
            System.IO.File.Copy(file, options.TextBox1.Text & options.TextBox8.Text & options.TextBox2.Text & System.IO.Path.GetFileName(file))
        Next

    Else
    End If
  • options.TextBox1.Text= 使用文件夹对话框选择的目录 EG:“C:\directory”

  • options.TextBox8.Text= 斜线分隔 = "C:\directory\"

  • options.TextBox2.Text= 由用户 EG Images 确定的文件夹名称 = "C:\directory\images"

如果不创建它,我还希望代码检查文件夹是否存在。

感谢任何帮助表示赞赏

4

3 回答 3

0

我正在寻找一种将子文件夹的所有图像移动到新文件夹的方法。这是我想出的工作。

    Dim WantedExtention As String = ".Your Type"
    Dim sourcePath As String
    Dim destinationPath As String

    'Somewhere else in the code to set the path is by paste not folder selection.
    FolderInfo = New DirectoryInfo(txtSelectedPath.Text)
    SelectedFolder = txtSelectedPath.Text
    Try
        'Check if it exits (Why its not False, Not too sure, but this worked)
        If Directory.Exists(SelectedFolder + "Move Folder") = True Then
            Directory.CreateDirectory(SelectedFolder + "Move Folder")
        End If
        destinationPath = Path.Combine(SelectedFolder + "Move Folder")

        For Each subdir In FolderInfo.GetDirectories()
            'Since I am making the sub folder in the Root Folder I had to Skip the folder
            If (subdir.Name = "Move Folder") Then
                Exit For
            End If
            sourcePath = Path.Combine(SelectedFolder, subdir.Name)

            Dim picList As String() = Directory.GetFiles(sourcePath, "*" + WantedExtention)

            For Each f As String In picList
                Dim fname As String = f.Substring(sourcePath.Length + 1)'Don't know why this is hear yet - but ya need it
                FileCopy(Path.Combine(sourcePath, fname), Path.Combine(destinationPath, fname))
            Next

        Next
        lblCompleted.Text = "COMPLETED"
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
于 2012-11-17T16:29:27.503 回答
0

遍历文件夹中文件的另一种好方法是使用 DirectoryInfo Dim di 作为 DirectoryInfo

di = My.Computer.FileSystem.GetDirectoryInfo("path to directory") For Each f as FileInfo In di.GetFiles("*.txt") '您对 FileInfo 对象有各种选项,包括复制。下一个

DirectoryInfo 还提供了 Exists 属性来确定您的目录是否真的存在!

于 2012-10-26T02:50:10.040 回答
0

一些帮助您组织/简化代码的想法:

  1. TextBox正确命名你的。如果您不能这样做,请声明有意义的局部变量并在代码中使用它。它将大大提高可读性。
  2. 使用Path.Combine加入C:\directoryImages一起。摆脱TextBox8.
  3. 我认为您也可以安全地删除System.IO.Path.GetFileName(file)部分,应该能够只使用目录作为复制目标。 File.Copy需要一个文件名作为第二个参数。您需要使用Path.Combine接受 3 个参数的重载,并结合基本路径 + 用户指定的文件夹 + 文件名。
  4. 使用File.Copy时,请确保目标中没有此文件。根据 MSDN Overwriting a file of the same name is not allowed,.
于 2012-10-26T01:24:38.207 回答