Script to Move Files Based on Filename

I have found this script to move files based on the filename (See attached or below).  Here is an example of the filenames
Daily_11-01-2011_0800.txt
Daily_11-02-2011_0800.txt

These files get created everyday, I want to only move the first one of the month so the objRegEx.Pattern = "\d{4}" I thought I could change from "\d{4}" to  "-01-". But that is not correct. Please advise what I could do to achieve this desired result.  

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")

Set objRegEx = CreateObject("VBScript.RegExp")

For Each objFile in colFiles
    objRegEx.Global = True   
    objRegEx.Pattern = "\d{4}"

    strSearchString = objFile.FileName
    Set colMatches = objRegEx.Execute(strSearchString)

    strYear = colMatches(0).Value

    strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
    objFile.Delete
Next

Open in new window

Bill WarrenIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Surone1Commented:

if Left(Right("objFile.FileName", 16), 2) = "01" then
  strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
0
Surone1Commented:
sorry:
if Left(Right(objFile.FileName, 16), 2) = "01" then
  strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
0
Bill WarrenIT ManagerAuthor Commented:
will "01" not detect the 01 in 2011  would I put in  "-01-" instead? also does that code replace all of the code above or just a certain portion of it what would the entire peice of code look like or is that it?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Bill PrewIT / Software Engineering ConsultantCommented:
The approach provided doesn't use a regex, just character compares.

Let's say your file name is "Daily_11-01-2011_0800.txt".

Then Right("Daily_11-01-2011_0800.txt", 16) will return "01-2011_0800.txt"

The Left("01-2011_0800.txt", 2) will return just "01", the day.

Hope that helps.

~bp
0
Bill WarrenIT ManagerAuthor Commented:
ok So I changed the code to be

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:Test'} Where " _
        & "ResultClass = CIM_DataFile")

if Left(Right("objFile.FileName", 16), 2) = "01" then
  strNewFile = "C:\Test\Archive\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if

and it does not error, however it does not move any of the files either.
0
Surone1Commented:
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")

Set objRegEx = CreateObject("VBScript.RegExp")

For Each objFile in colFiles
    objRegEx.Global = True  
    objRegEx.Pattern = "\d{4}"

    strSearchString = objFile.FileName
    Set colMatches = objRegEx.Execute(strSearchString)

    strYear = colMatches(0).Value

  if Left(Right("objFile.FileName", 16), 2) = "01" then
  strNewFile = "C:\Test\Archive\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
    objFile.Delete
Next
0
Surone1Commented:
if you only want to delete the copied files then place the line with "objFile.Delete"
inside the if statement
0
Surone1Commented:
the lines containing regex can probably be removed as well.

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")

'Set objRegEx = CreateObject("VBScript.RegExp")

For Each objFile in colFiles
  '  objRegEx.Global = True  
   ' objRegEx.Pattern = "\d{4}"

  '  strSearchString = objFile.FileName
'   Set colMatches = objRegEx.Execute(strSearchString)

  '  strYear = colMatches(0).Value

  if Left(Right("objFile.FileName", 16), 2) = "01" then
  strNewFile = "C:\Test\Archive\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
    objFile.Delete
Next
0
Surone1Commented:
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")


For Each objFile in colFiles
  if Left(Right(objFile.FileName, 16), 2) = "01" then
  strNewFile = "C:\Test\Archive\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
    objFile.Delete
Next
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill WarrenIT ManagerAuthor Commented:
Hi Surone1 the last code

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")


For Each objFile in colFiles
  if Left(Right(objFile.FileName, 16), 2) = "01" then
  strNewFile = "C:\Test\Archive\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
    objFile.Delete
Next

is not copying the file to the destination, yet not erroring
0
Bill WarrenIT ManagerAuthor Commented:
I changed the 16 to 12 and it started copying the files.. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.