VB.Net如何通过文件夹和复制文件进行迭代(How to iterate through folders and copy files)

编程入门 行业动态 更新时间:2024-10-26 00:23:37
VB.Net如何通过文件夹和复制文件进行迭代(How to iterate through folders and copy files)

我有以下代码将所有内容复制到正确的位置,文件1,文件2和文件3除外。

来源 :“C:\ dir” 目标 :“C:\ tmp \ Backup”

Public Sub CopyDirectory(source As DirectoryInfo, target As DirectoryInfo) 'Recursively call the DeepCopy Method for each Directory For Each MyDir As DirectoryInfo In source.GetDirectories() CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name)) Next ' Go ahead and copy each file in "source" to the "target" directory For Each MyFile As FileInfo In source.GetFiles() If Not (File.Exists(Path.Combine(target.FullName, MyFile.Name))) Then MyFile.CopyTo(Path.Combine(target.FullName, MyFile.Name)) Else MessageBox.Show("File already exist") End If Next End Sub

文件1,2和3应该进入“Dir”文件夹,但它会一直显示在“备份”文件夹中。

C:\ |_ tmp |_ Backup |_ Dir |_ Folder 1 |_ File 11 |_ File 12 |_ File 13 |_ Folder 2 |_ File 21 |_ File 22 |_ File 23 |_ Folder 3 |_ File 31 |_ File 32 |_ File 33 |_ File 1 |_ File 2 |_ File 3

如何确保将文件1,2和3复制到“Dir”文件夹中?

I have the following code which copies everything in to the right place except File 1, File 2 & File 3.

Source: "C:\dir" Target: "C:\tmp\Backup"

Public Sub CopyDirectory(source As DirectoryInfo, target As DirectoryInfo) 'Recursively call the DeepCopy Method for each Directory For Each MyDir As DirectoryInfo In source.GetDirectories() CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name)) Next ' Go ahead and copy each file in "source" to the "target" directory For Each MyFile As FileInfo In source.GetFiles() If Not (File.Exists(Path.Combine(target.FullName, MyFile.Name))) Then MyFile.CopyTo(Path.Combine(target.FullName, MyFile.Name)) Else MessageBox.Show("File already exist") End If Next End Sub

File 1, 2 & 3 should go inside the "Dir" folder, but it keeps showing up in the "Backup" folder.

C:\ |_ tmp |_ Backup |_ Dir |_ Folder 1 |_ File 11 |_ File 12 |_ File 13 |_ Folder 2 |_ File 21 |_ File 22 |_ File 23 |_ Folder 3 |_ File 31 |_ File 32 |_ File 33 |_ File 1 |_ File 2 |_ File 3

How do I make sure that File 1, 2, and 3 gets copied inside the "Dir" folder?

最满意答案

我认为你的问题在于这一行:

CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name))

我认为你想在目标上创建新文件夹。 要做到这一点,你这样做:

CopyDirectory(MyDir, target.CreateSubdirectory(MyDir.Name))

嗯,对。 错过了这一行:

来源:“C:\ dir”目标:“C:\ tmp \ Backup”

你的程序正在完成你告诉它要做的事情。 获取dir中的文件并将其置于备份中。 如果要更改,则需要在启动第一次递归时将目标设置为C:\ temp \ Backup \ dir。

I think your problem lies in this line:

CopyDirectory(MyDir, target.CreateSubdirectory(projectName & "\" & MyDir.Name))

I take it you want to create the new folder at target. For that to happen you do this:

CopyDirectory(MyDir, target.CreateSubdirectory(MyDir.Name))

Ehm right. Missed this line:

Source: "C:\dir" Target: "C:\tmp\Backup"

Your program is doing exactly what you are telling it to do. Take the files in dir and put it in backup. If you want that to change you need to set target to C:\temp\Backup\dir when you start the first recursion.

更多推荐

本文发布于:2023-07-30 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   迭代   文件   Net   VB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!