Link to home
Start Free TrialLog in
Avatar of Bill Warren
Bill WarrenFlag for United States of America

asked on

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

Avatar of Surone1
Surone1
Flag of Suriname image


if Left(Right("objFile.FileName", 16), 2) = "01" then
  strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
sorry:
if Left(Right(objFile.FileName, 16), 2) = "01" then
  strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
        "." & objFile.Extension
    objFile.Copy(strNewFile)
end if
Avatar of Bill Warren

ASKER

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?
Avatar of Bill Prew
Bill Prew

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
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.
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
if you only want to delete the copied files then place the line with "objFile.Delete"
inside the if statement
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
ASKER CERTIFIED SOLUTION
Avatar of Surone1
Surone1
Flag of Suriname image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
I changed the 16 to 12 and it started copying the files.. Thanks