Splitting Files into Subdirectories

I’m guessing this is possible, but I haven’t seen anything that does it yet.

Is there a way to split a directory of files into respective sub-directories? For example, given the files

197101_fileA
197101_fileB
197102_fileC
197102_fileD
197103_fileE
197201_fileF
197301_fileF
197301_fileG

I’d like to move them to a structure like:

1971/01/197101_fileA
1971/01/197101_fileB
1971/02/197102_fileC
1971/02/197102_fileD
1971/03/197103_fileE
1972/02/197201_fileF

and so forth. The sub directories would not necessarily exist.

I’m a recent convert from windows, and this was very easy using Directory Opus and it’s regex rename feature. I haven’t found anything on Mac that will accomplish this but I’m hoping it can be written.

Yes this is doable, but before we delve into it lets figure out a few assumptions… will files always of ######_fileX or can that vary? Do you always want it split as you illustrated? Far too often people work on a solution and not all the details were given.

No, that was an example…ideally I’d like to find a way to make the changes using regex to define the from and two forms, and use applescript to provide the glue.