Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to act on files after certain date

Status
Not open for further replies.
Oct 9, 2003
174
US
I have been trying to write a script that will run this command for each file created after a certain date:

For example: (In logical terms)

MONTH=(xx)
DAY=(xx)
YEAR=(xxxx)

For each <filename> in y:\<dir1>\<dir2> with the extension .mdf newer then xx/xx/xxxx do;
y:\<dir1>\<action>.exe <filename>



If I knew how to call that .exe against each file, I think this would work but It's still missing two important things:

1) It doesn't specify the y:\<dir1>\<dir2> directory to gather the file names
2) It doesn't just run against .mdf files


**********************************************************

strMonth = InputBox ("Enter a 2 digit month")
strDay = InputBox ("Enter a 2 digit day")
strYear = InputBox ("Enter a 4 digit year")

strDate = strYear & strMonth & strDay & "000000.000000+000"


strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where CreationDate >= '" & strDate & "'")

For Each objFile In colFiles
Do action
Next

***********************************************************


Please Help!!
 
OK, looks like my query is what needs to be changed to include the path and restriction to just .mdf files. But I'm banging my head against the wall with errors..

To recap, Im trying to get the query to search a directory for all .mdf files created after a certain date that has been inputed.

I've tried:

*********************************************
strMonth = "01"
strDay = "01"
strYear = "2008"

strDate = strYear & strMonth & strDay & "000000.000000+000"

intNumberOfFiles = 0
intTotalSize = 0

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='T:\ideas_LIC_FIX'} Where CreationDate >= '" & strDate & "'")

For Each objFile in colFiles
intNumberOfFiles = intNumberOfFiles + 1
Next

Wscript.Echo intNumberOfFiles

******************************************************
Which errors out:

I've tried:
******************************************************
strMonth = "01"
strDay = "01"
strYear = "2008"

strDate = strYear & strMonth & strDay & "000000.000000+000"

intNumberOfFiles = 0
intTotalSize = 0

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='T:\ideas_LIC_FIX'} Where " _
& "ResultClass = CIM_DataFile" AND Where CreationDate >= '" & strDate & "'")

For Each objFile in colFiles
intNumberOfFiles = intNumberOfFiles + 1
Next

Wscript.Echo intNumberOfFiles
**********************************************************
Which says it expects a ')' on the query line. Not sure where that should go.


I've tried a number of other combinations but those have been the closest that I had.

Any help on the query would be greatly appreciated.

Thanks
 
OK, I completely switched it up and I think I got it working. Well almost....

********************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("T:\ideas_LIC_FIX")

dtmOld = DateAdd("d", -30, Now)

AllFolders(objFolder)

runHM objFolder

Sub AllFolders(Folder)
For Each Subfolder in Folder.SubFolders

runHM subfolder
AllFolders Subfolder

Next
End Sub


Sub runHM(subFolder)

Set colFiles = SubFolder.Files

For Each objFile in colFiles
arrSplitName = Split(objFile.Name, ".")
strExtension = arrSplitName(UBound(arrSplitName))
If strExtension = "mdf" and objFile.DateCreated > dtmOld Then
"T:\bin\HM.exe" objFile.Name
End If
Next

End Sub
******************************************************

The one sticky point I have left is I don't know how to call the HH.exe to run against each of the identified file names. Can someone help me with this last piece of the puzzle?
 
The final product.. Thanks PHV for you help!!

*********************************************************
'Set Global Variables
daysold = InputBox ("Number of days back you want HAM_MEDIC to look? "& (chr(13)) &" "& (chr(13)) &" Example: 10, 15, 30", "HAM_MEDIC Script - by TM")
fileext = InputBox ("Type of file extension you want HAM_MEDIC to run against (enter it w/o the period)? "& (chr(13)) &" "& (chr(13)) &" Example: mdf, asm, prt", "HAM_MEDIC Script - by TM")


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("T:\ideas_LIC_FIX")


dtmOld = DateAdd("d", -daysold, Now)
result= Inputbox ("From your input:"& (chr(13)) &" "& (chr(13)) &" HAM_MEDIC will run against all ."& fileext &" files created after "& dtmold &" that reside in the "& objFolder.Name &" Folder "& (chr(13)) &" "& (chr(13)) &" "& (chr(13)) &" "& (chr(13)) &" Enter 1 to continue "& (chr(13)) &" Enter 2 to quit", "HAM_MEDIC Script - by TM")

If result = "2" Then

WScript.Echo "Canceled"
WScript.Quit
Else

AllFolders(objFolder)

runHM objFolder

Sub AllFolders(Folder)
For Each Subfolder in Folder.SubFolders

runHM subfolder
AllFolders Subfolder

Next
End Sub


Sub runHM(subFolder)

Set colFiles = SubFolder.Files

For Each objFile in colFiles
arrSplitName = Split(objFile.Name, ".")
strExtension = arrSplitName(UBound(arrSplitName))
If strExtension = fileext and objFile.DateCreated > dtmOld Then

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "T:\bin\HAM_MEDIC.exe" & objFolder & "\" & objFile.Name

End If
Next

End Sub

End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top