Bill Warren
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.
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
sorry:
if Left(Right(objFile.FileNam e, 16), 2) = "01" then
strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if
if Left(Right(objFile.FileNam
strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if
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?
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_08 00.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
Let's say your file name is "Daily_11-01-2011_0800.txt
Then Right("Daily_11-01-2011_08
The Left("01-2011_0800.txt", 2) will return just "01", the day.
Hope that helps.
~bp
ASKER
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:T est'} Where " _
& "ResultClass = CIM_DataFile")
if Left(Right("objFile.FileNa me", 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:T
& "ResultClass = CIM_DataFile")
if Left(Right("objFile.FileNa
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.Reg Exp")
For Each objFile in colFiles
objRegEx.Global = True
objRegEx.Pattern = "\d{4}"
strSearchString = objFile.FileName
Set colMatches = objRegEx.Execute(strSearch String)
strYear = colMatches(0).Value
if Left(Right("objFile.FileNa me", 16), 2) = "01" then
strNewFile = "C:\Test\Archive\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if
objFile.Delete
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\
& "ResultClass = CIM_DataFile")
Set objRegEx = CreateObject("VBScript.Reg
For Each objFile in colFiles
objRegEx.Global = True
objRegEx.Pattern = "\d{4}"
strSearchString = objFile.FileName
Set colMatches = objRegEx.Execute(strSearch
strYear = colMatches(0).Value
if Left(Right("objFile.FileNa
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
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.Reg Exp")
For Each objFile in colFiles
' objRegEx.Global = True
' objRegEx.Pattern = "\d{4}"
' strSearchString = objFile.FileName
' Set colMatches = objRegEx.Execute(strSearch String)
' strYear = colMatches(0).Value
if Left(Right("objFile.FileNa me", 16), 2) = "01" then
strNewFile = "C:\Test\Archive\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if
objFile.Delete
Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\
& "ResultClass = CIM_DataFile")
'Set objRegEx = CreateObject("VBScript.Reg
For Each objFile in colFiles
' objRegEx.Global = True
' objRegEx.Pattern = "\d{4}"
' strSearchString = objFile.FileName
' Set colMatches = objRegEx.Execute(strSearch
' strYear = colMatches(0).Value
if Left(Right("objFile.FileNa
strNewFile = "C:\Test\Archive\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if
objFile.Delete
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.FileNam e, 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
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\
& "ResultClass = CIM_DataFile")
For Each objFile in colFiles
if Left(Right(objFile.FileNam
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
ASKER
I changed the 16 to 12 and it started copying the files.. Thanks
if Left(Right("objFile.FileNa
strNewFile = "C:\Test\" & strYear & "\" & objFile.FileName & _
"." & objFile.Extension
objFile.Copy(strNewFile)
end if