I have an AutoIt3 function that returns the "base name" of a path. For example, _FileBaseName("C:\MyFolder\MyFile.txt") returns "MyFile"
You could use it to put the file into the proper folder
Whatever programming language you decide to use should either have a canned function that does the same, or have library functions you can use to roll your own without too much difficulty
Basically it just returns the characters after the last backslash up to but not including the first extension dot
; return the basename part of a file path
; works only for files with extension
; e.g. returns "test" for c:\folder\test.exe path
If $path = "" Then Return ""
If StringLen($path) < 3 Then Return ""
Local $pos = StringInStr($path, "\", 0, -1)
$pos += 1
Local $tmp = StringMid($path, $pos)
Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
So in AutoIt3 you would do something like
$basename = _FileBaseName($path)
FileMove($basename & ".*", $basename & "\*.*)
The "&" is string concatenation operator. So it becomes "move files with base name x inside folder named x"
Should be pretty simple if the folders are all on the same level.