Can anyone give me advice on how to attack this problem?

I have files in a directory that are named in a convention dayMonthrandomstuff. I need to sort these files into folders based on month then date. So example, "15e23dr" would go into the "May" folder since "e" is the 5th letter in the alphabet and within that folder, it would go into folder 15, since that's the day. "02a28db" would go into January --> 02.

Now, I have blurry logic on what I want to do: Pull all the filenames from the directory using "IO.Directory.GetFiles" then loop through each file(storing them in an array?) and have If statements doing the work of moving the files where they need to go. But I'm lost on how to put this all together. I was hoping a more experienced VB.net programmer could help me strengthen the logic on this. If I can get a solid picture on what I need to do, then I can use the APIs.


Edited by SpeakerKeyboard: typo

1 Year
Discussion Span
Last Post by SpeakerKeyboard

One option would be to use the MonthName function of the DateAndTime module, to decode the letter to the month name then build the destination directory name directly from the filename:

Function MakeDirectoryPath(rootFolder As String, fileName As String) As String
    Dim monthdirectory As String = DateAndTime.MonthName(Asc(fileName(2)) - 96)
    Dim dayDirectory As String = fileName.Substring(0, 2)
    Return IO.Path.Combine(rootFolder, monthdirectory, dayDirectory)
End Function

This will eliminate your If statements, however, you'll still have to verify that the directories exist and that you have write permission.

A simple iteration can then move the files where you want:

Sub MoveFiles(rootSource As String, rootDestination As String)
    For Each s As String In IO.Directory.GetFiles(rootSource)
        Dim fileName As String = IO.Path.GetFileName(s)
        Dim destinationDirectory As String = MakeDirectoryPath(rootDestination, fileName)
        If Not IO.Directory.Exists(destinationDirectory) Then
        End If
        IO.File.Move(s, destinationDirectory)
End Sub

Edited by tinstaafl


Thanks for helping out once again tinstaafl but I'm having trouble following some of this code and therefore can't troubleshoot the error I'm getting.

Executing the code results in the month and day folder being created but the file is not being moved, instead a System.IO.IO Exception is being produced pointing to line 8. Specifically, "Cannot create a file when that file already exists". Now this to me says "I just need a simple If Not Exist" to check directories but isn't that already handled at line 5 and 6 of your move Sub?

Apologize if this is a very basic question but I'm still getting my bearings in VB.NET.


Identified my problem. IO.File.Move creates the directory where the file is being moved into, causing that exception. Going to try more work arounds as just letting IO.File.Move try to create the directories throws a DirectoryNotFound Exception. Trying IO.File.Copy to try and get around Move creating the directory failed.

Current idea: Allow For loop to only create the directories and then perharps another loop to push the files into them.


The code in my last post shows how to check if the directory exists and creating it if it doesn't.


Finally was able to realize my issue, I was not stating the filename in the IO.File.Move command.

By shifting "IO.File.Move(s, destinationDirectory)" to "IO.File.Move(s, IO.Path.Combine(destinationDirectory, fileName))", I was able to get to program to do exactly what I needed. Learned a great deal from this.

Thank you very much for helping me out tinstaafl.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.