如何将所有文件和文件夹从当前目录移动到另一个目录,不包括vb中名为“我的文件夹”的文件夹?

时间:2014-10-21 11:00:19

标签: vb.net visual-studio visual-studio-2013

我想将所有文件和文件夹从当前目录移动到另一个目录,不包括名为"我的文件夹"的文件夹。我想知道如何用vb做到这一点?

1 个答案:

答案 0 :(得分:0)

尚未经过测试,但无论如何可能会有所帮助:

Dim currentDir = Directory.GetCurrentDirectory() ' or whatever you call current
Dim targetDir = Path.Combine(currentDir, "Target-Directory")
Directory.CreateDirectory(targetDir)

Dim allDirs = From dir In Directory.EnumerateDirectories(currentDir, "*.*", SearchOption.AllDirectories)
              Where Not Path.GetFileName(dir).Equals("My Folder", StringComparison.InvariantCultureIgnoreCase)
For Each dir As String In allDirs
    Try
        Dim targetPath As String = Path.Combine(targetDir, Path.GetFileName(dir))
        Directory.Move(dir, targetPath)
    Catch ex As Exception
        ' log or whatever, here just ignore and continue ...
    End Try
Next

'now move the rest of the files in the troot folder
Dim filesInRoot = From file In Directory.EnumerateFiles(currentDir, "*.*", SearchOption.TopDirectoryOnly)
For Each file As String In filesInRoot
    Try
        Dim targetPath As String = Path.Combine(targetDir, Path.GetFileName(file))
        IO.File.Move(file, targetPath)
    Catch ex As Exception
        ' log or whatever, here just ignore and continue ...
    End Try
Next