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?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
Bill PrewCommented:
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
 
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
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.