VB.NET 根据文件名创建文件夹

发布于 2024-11-05 04:57:19 字数 989 浏览 0 评论 0原文

下面的代码根据不同的文件名创建多个文件夹,并将这些文件移动到新文件夹中。例如,文件 ABC 和 XYZ 被移动到新创建的名为 ABC 和 XYZ 的文件夹中。我想改进代码以允许稍微不同的文件名进入一个公共文件夹。例如,我希望将文件 ABC_rock、ABC_soil 和 ABC_water 放入名为 ABC still 的文件夹中,并将文件 XYZ_rock、XYZ_soil 和 XYZ_water 放入名为 XYZ 的文件夹中。我不想为 ABC_rock、ABC_soil 和 ABC_water 创建单独的文件夹。非常感谢任何建议。感谢您的帮助。

        Dim strOutputLocation As String = "C:\Temp"
        Dim rootPath As String = strOutputLocation

        For Each filepath As String In IO.Directory.GetFiles(rootPath)

            Dim folderName As String = IO.Path.GetFileNameWithoutExtension(filepath)
            Dim folderPath As String = IO.Path.Combine(rootPath, folderName)

            If Not IO.Directory.Exists(folderPath) Then
                IO.Directory.CreateDirectory(folderPath)
            End If

            Dim fileName2 As String = IO.Path.GetFileName(filepath)
            Dim newFilePath As String = IO.Path.Combine(folderPath, fileName2)

            File.Move(filepath, newFilePath)

        Next

The code below creates multiple folders based on different file names and moves those files into the new folders. For example, files ABC and XYZ are moved into a newly created folder named ABC and XYZ. I want to advance the code to allow slightly different file names to go into a common folder. Example, I want file ABC_rock, ABC_soil, and ABC_water to be put into a folder named ABC still, AND file XYZ_rock, XYZ_soil, and XYZ_water to be put into a folder named XYZ. I do not want a separate folder created for ABC_rock, ABC_soil, and ABC_water. Any suggestions are greatly appreciated. Thank you for your help.

        Dim strOutputLocation As String = "C:\Temp"
        Dim rootPath As String = strOutputLocation

        For Each filepath As String In IO.Directory.GetFiles(rootPath)

            Dim folderName As String = IO.Path.GetFileNameWithoutExtension(filepath)
            Dim folderPath As String = IO.Path.Combine(rootPath, folderName)

            If Not IO.Directory.Exists(folderPath) Then
                IO.Directory.CreateDirectory(folderPath)
            End If

            Dim fileName2 As String = IO.Path.GetFileName(filepath)
            Dim newFilePath As String = IO.Path.Combine(folderPath, fileName2)

            File.Move(filepath, newFilePath)

        Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-11-12 04:57:20

从一开始就将文件名分开 - 我在这里使用下划线,但您可以根据需要更改它。拆分将在所有情况下创建至少一个项目,如果您只对第一部分感兴趣,您可以通过更改一行来处理此问题:

Dim folderName As String = IO.Path.GetFileNameWithoutExtension(filepath).Split("_")(0)

如果有下划线,它将使用第一个部分之前的所有内容。如果没有下划线,它将使用整个文件名。

Break the filename apart right from the start - I'm using an underscore here, but you can change it however you need. Split will create at least one item in all cases, and if you're only interested in the first part, you can handle this by altering one line:

Dim folderName As String = IO.Path.GetFileNameWithoutExtension(filepath).Split("_")(0)

If there's an underscore, it uses everything before the first one. If there are no underscores it will use the whole filename.

偏闹i 2024-11-12 04:57:20

如果您知道分隔符是什么,那么您可以在提取文件名后使用诸如 .Split() 之类的简单字符。如果文件名可能比您需要使用RegEx更复杂。

var fileName = "ABC_rock";
var parts = fileName.Split(new String[] { "_" }, System.StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 1) {
    var folderName = parts[0];

    // Move fileName here...
}

请注意,上面的代码假设您的分隔符是下划线字符。

If you know what your separator character will be then you can use something simple like .Split() after you extract the file name. If the files names can be more complex than that you'll need to use RegEx.

var fileName = "ABC_rock";
var parts = fileName.Split(new String[] { "_" }, System.StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 1) {
    var folderName = parts[0];

    // Move fileName here...
}

Note the code above assumes your separator is the underscore character.

扬花落满肩 2024-11-12 04:57:20

如果所有文件名都具有相同的格式 PREFIX_somethingElse,您可以对每个文件名使用 Split() 函数来查找公共前缀。获得所有前缀的列表后,您可以使用此前缀创建文件夹,然后将每个文件移动到以其前缀命名的文件夹中。

为此,您需要知道文件名上的分隔符才能使用 Split() 函数。

该过程将是这样的:

  1. 声明一个字符串列表
  2. 使用以下命令迭代文件名
    split() 获取前缀并添加
  3. 在列表中找到的每个新前缀迭代列表并创建
    每个前缀的文件夹
  4. 迭代文件名并移动
    每个文件到名为的文件夹
    该文件名的前缀。

如果您使用更复杂的数据结构以避免多次迭代,则可以极大地优化该过程,但这是基本思想。

If all your filenames have the same format PREFIX_somethingElse you can use the Split() function on each filename to find the common prefixes. Once you have a list of all the prefixes you can create the folders using this prefixes and then move each file to the folder named as his prefix.

To do this you need to know the separator character on the filename in order to use the Split() function.

The process will be something like this:

  1. Declare a list of strings
  2. Iterate through filenames using
    split() to obtain the prefix and add
    each new prefix you find to the list
  3. Iterate through the list and create
    a folder for each prefix
  4. Iterate through filenames and move
    each file to the folder named as the
    prefix of that filename.

The process can be greatly optimized if you use more complex data sctructures in order to avoid multiple iterations, but this is the basic idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文