?
Solved

Move files to new subdirectories based on filename

Posted on 2010-01-08
9
Medium Priority
?
923 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:justearth
  • 4
  • 3
  • 2
9 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 26212205
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
 

Author Comment

by:justearth
ID: 26212445
Excuse my ignorance.  I got an error stating I need "RegEx"

Please advise,
JE
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 26212490
The above solution is VBScript; did you copy it into a *.vbs file and then double-click the file?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:justearth
ID: 26212514
Yes, that is what I did.

Please advise,
JE

error-je-regex.bmp
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1520 total points
ID: 26212619
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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 480 total points
ID: 26212698
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
 

Author Comment

by:justearth
ID: 26214365
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26214390
Oops, that should have been %%A instead of %A in that expression...

~bp
0
 

Author Closing Comment

by:justearth
ID: 31674681
thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question