所以我正在开发一个桌面清洁工,它根据扩展名将项目移动到特定目录(正在清理的目录)
用户定义将放置项目的文件夹的名称。
但移动文件的代码无法正常运行。
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
=使用“文件夹”对话框选择的目录E.G:“C:\ directory”
options.TextBox8.Text
=斜线to separate =“C:\ directory \”
options.TextBox2.Text
=用户确定的文件夹名称E.G Images =“C:\ directory \ images”
我还希望代码检查文件夹是否存在,如果没有创建它。
感谢任何帮助表示赞赏
答案 0 :(得分:0)
帮助您整理/简化代码的一些想法:
TextBox
。如果你不能这样做,请声明有意义的局部变量并在代码中使用它。这将大大提高可读性。C:\directory
和Images
。摆脱TextBox8
。System.IO.Path.GetFileName(file)
部分,应该只能将目录用作复制目标。File.Copy
期望文件名作为第二个参数。您需要使用接受3个参数的Path.Combine
重载,并组合基本路径+用户指定的文件夹+文件名。Overwriting a file of the same name is not allowed
。答案 1 :(得分:0)
在文件夹中迭代文件的另一个好方法是使用DirectoryInfo Dim di as DirectoryInfo
di = My.Computer.FileSystem.GetDirectoryInfo(“目录路径”) 对于每个f作为FileInfo in di.GetFiles(“* .txt”) '你在FileInfo对象上有各种选项,包括Copy。 下一步
DirectoryInfo还提供了Exists属性来确定你的目录是否真的存在!
答案 2 :(得分: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