Move files to new subdirectories based on filename

Hello,
I have file with following filenames:

ppt1988_001 ... ppt1988_500 ... ppt2004_500
tmin1988_001 ... tmin1988_500 ... tmin2004_500
tmax1988_001 ... tmax1988_500 ... tmax2004_500

So they have the base in front ppt, tmax, or tmin. Then this is followed by a year 1988 (through 2004) and then a three digit number 001 ~ 500 with some missing values.  There are 7 files of the same base name each with a different extension.

I would like a script to move the files into new subdirectories (they need to be created) for each of the year/ppt, tmax, or tmin combos.

For example, one new subdirectory (ppt1988) would contain all files with the name ppt1988 (7 extensions * ~500). etc

Thanks,
JE
justearthAsked:
Who is Participating?
 
Patrick MatthewsConnect With a Mentor Commented:
This seems to be working...




Dim fso, fld, sf, fil, RegX, Mats

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("c:\new folder")
Set RegX = New RegExp
With RegX
    .IgnoreCase = True
    .Pattern = "^(ppt|tmax|tmin)\d{4}"
    .Global = False
End With

For Each fil In fld.Files
    Set Mats = RegX.Execute(fil.Name)
    If Mats.Count > 0 Then
        If Not fso.FolderExists(fld.Path & "\" & Mats(0)) Then
            Set sf = fso.CreateFolder(fld.Path & "\" & Mats(0))
        Else
            Set sf = fso.GetFolder(fld.Path & "\" & Mats(0))
            If fso.FileExists(sf.Path & "\" & Mats(0)) Then
                fso.DeleteFile sf.Path & "\" & Mats(0), True
            End If
        End If
        fil.Move sf.Path & "\" & fil.Name
    End If
Next

Set RegX = Nothing
Set sf = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

MsgBox "Done"
0
 
Patrick MatthewsCommented:
Dim fso, fld, sf, fil, RegX, Mats

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("c:\folder")
Set RegX = New RegExp
With RegEx
    .IgnoreCase = True
    .Pattern = "^(pp1|tmax|tmin)\d{4}"
    .Global = False
End With

For Each fil In fld.Files
    Set Mats = RegX.Execute(fil.Name)
    If Not Mats Is Noting Then
        If Not fso.FolderExists(fld.Path & "\" & Mats(0)) Then
            Set sf = fso.CreateFolder(fld.Path & "\" & Mats(0))
        Else
            Set sf = fso.GetFolder(fld.Path & "\" & Mats(0))
            If fso.FileExists(sf.Path & "\" & Mats(0)) Then
                fso.DeleteFile sf.Path & "\" & Mats(0), True
            End If
        End If
        fil.Move sf.Path & "\" & fil.Name
    End If
Next

Set RegX = Nothing
Set sf = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

MsgBox "Done"
0
 
justearthAuthor Commented:
Excuse my ignorance.  I got an error stating I need "RegEx"

Please advise,
JE
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Patrick MatthewsCommented:
The above solution is VBScript; did you copy it into a *.vbs file and then double-click the file?
0
 
justearthAuthor Commented:
Yes, that is what I did.

Please advise,
JE

error-je-regex.bmp
0
 
Bill PrewConnect With a Mentor Commented:
Here's a batch script, just save it as a BAT file in the directory containing the files and run it.  Notice the actual MOVE command is preceeded by an ECHO right now.  That will allow you to run the script and it will display the MOVE command it would have executed, but not actually do it.  If everything looks good, remove the ECHO and run it for real.

@echo off
setlocal EnableDelayedExpansion
REM Process all matching files in this directory, each filename in %%A
for %%A in (ppt.* tmin.* tmax.*) do (
  REM Spilt the filename at the underscore, left part into %%B
  for /f "tokens=1 delims=_" %%B in ("%~nA") do (
    REM Create the subdirectory if needed
    if not exist "%%B" mkdir "%%B"
    REM Move the file to the subdirectory
    ECHO move "%%A" "%%B"
  )
)

~bp
0
 
justearthAuthor Commented:
billprew:
Thanks for the script, however I get the following error:

The following usage of the path operator in batch-parameter
substitution is invalid: %~nA") do (

For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.

matthewspatrick:
thanks this worked.

Cheers,
JE

0
 
Bill PrewCommented:
Oops, that should have been %%A instead of %A in that expression...

~bp
0
 
justearthAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.