I use the same method then mehow, as I've worked out the detail, I give here:
Function entDirExists(ByVal strDir)
'
Dim fso
'
Set fso = CreateObject("Scripting.FileSystemObject")
entDirExists = fso.FolderExists(strDir)
'
Set fso = Nothing
End Function
'
' i: counter
' idxDir: index of the directory name in the split array
'
' strFilename: current Filename
' strSrcDir: source directory path
' strDstDirPrefix: destination directory path prefix
' strDstDir: destination directory for a category
'
' xarr: split array of filename
'
Function entMoveFiles(ByVal strSrcDir)
'
Dim i, idxDir
'
Dim strFilename, strDstDirPrefix, strDstDir
Dim xarr
'
idxDir = 2
'
' strSrcDir = "D:\users\tmp"
'
strDstDirPrefix = strSrcDir
'
' get the first image file:
'
strFilename = Dir(strSrcDir & "\*.jpeg", vbNormal)
'
' loop over the source directory for images:
'
i = 0
Do While (strFilename <> "")
'
' split filename:
'
xarr = Split(strFilename, "-")
'
' get the destination directory full path like prefix\catetory:
'
strDstDir = xarr(idxDir)
strDstDir = strDstDirPrefix & "\" & Trim(strDstDir)
'
' create the destination directory if nonexistent:
'
If (Not entDirExists(strDstDir)) Then
MkDir strDstDir
End If
'
' move the current file: [source path] as [destination path]
'
Name strSrcDir & "\" & strFilename As strDstDir & "\" & strFilename
'
' get next file:
'
strFilename = Dir()
i = i + 1
Loop
'
entMoveFiles = i
'
End Function
To handle a directory name "D:\myjpegs", one uses this:
entMoveFiles "D:\myjpegs"
It will create subdirectories for each category, such as suspension, machine under D:\myjpegs if they don't already exist.