开发者

VB.NET Create folders based on filenames

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 na开发者_JS百科med 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜