如何使用 vb.net 移动多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27281034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:32:50  来源:igfitidea点击:

How to move multiple files using vb.net

vb.netvisual-studio-2010

提问by HymanSparrow

Is it possible to move all files/folders in one directory that contain some words and move them to another? So for example all files/folders called 'Testing this' and moving them to another folder? I try the following but its not working? The words 'testing this' could be shown anywhere in the file name.

是否可以将一个目录中包含一些单词的所有文件/文件夹移动到另一个目录中?例如,所有名为“Testing this”的文件/文件夹并将它们移动到另一个文件夹?我尝试了以下但它不起作用?“testing this”这个词可以显示在文件名的任何地方。

   Dim directory = "C:\Test\"
        For Each filename As String In IO.Directory.GetFiles(directory, "testing this", IO.SearchOption.AllDirectories)
            My.Computer.FileSystem.MoveFile(filename, "C:\Test\Old\" & "testing this")
        Next

回答by Brandon

You can use wildcards in the GetFiles method.

您可以在 GetFiles 方法中使用通配符。

So:

所以:

Dim directory = "C:\Test\"
        For Each filename As String In IO.Directory.GetFiles(directory, "*testing this*", IO.SearchOption.AllDirectories)
            My.Computer.FileSystem.MoveFile(filename, "C:\Test\Old\" & "testing this")
        Next

回答by Steve

No, the My.Computer.FIlesSystem.MoveFile can move only one file for each call.
You need to build a loop around your source directory and move each file one by one

不可以,My.Computer.FIlesSystem.MoveFile 每次调用只能移动一个文件。
你需要在你的源目录周围建立一个循环并一个一个地移动每个文件

Dim sourceDir = "C:\test"
For Each file As String In My.Computer.FileSystem.GetFiles( sourceDir, _

    Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*testing this*")

    Dim destPath = file.Substring(sourceDir.Length + 1)
    Dim destFile = System.IO.Path.GetFileName(file)

    My.Computer.FileSystem.MoveFile(file, _
               System.IO.Path.Combine("C:\Test\Old", destPath, destFile ))
Next