My scripting question is below, beneath the two empty lines.
But if you care to read what my ultimate objective is, here it is:
Ultimate Objective: I need to bill my time by the hour, but because I have
ADD, I can not keep track of what I do during the day, and this is having a
disastrous impact on my ability to earn a living.
I am attempting to create a script that will enable me to know what files I
opened (and worked on) during each day, and when I opened them. After I
succeed with this script (hopefully), I hope to create:
1. A macro in Microsoft Word that will send to a comma delimited file the
name of each file that I open in Word, the "Start Time" when each file was
the topmost window on the monitor, and the "End Time", i.e., when the file
ceased to be the topmost window on the monitor. (Obviously, a document may
may be opened several times during a day, or become the topmost window
several times a day.) I also hope to eventually create...
2. A macro in Outlook that will send to a comma delimited file some
identifier ("Name") of each window in Outlook that I open, and each Outlook
window's "Start Time" and "End Time".
My scripting question: I cannot figure out why the script below does not
delete the shortcut files in the directory: "C:\Documents and
Settings\marxp\Recent". In 2 different places, the script tries to delete a
shortcut file in "C:\Documents and Settings\marxp\Recent", after the script
successfully copies the shortcut file to another directory, but the attempts
to delete fail. Any ideas would be much appreciated.
Thanks. Marc
Here's the script:
'
' This script was cannibalized as follows: The lines that sense a new file
being added to marxp\Recent are drawn
' from The Portable Script Center, Version 2.0, September 2003
' located in C:\Documents and Settings\marxp\Start
Menu\Programs\scriptcenter.chm.
'
dim strComputer, sHarvey, lenHarvey, iQuotOutRight, iQuoteIn,
sHarveyTrimmed, iSlash
dim sHarveyNamOnly, sHarveyOrigFulNam, sHarveyTransitOnlyFulNam,
sHarveyNuFulNam, sUserRecentDir
Dim SysClip, fsoClip, v, s, Var, sExistsDarnIt
Dim strMonth, strDay, DateInfo, TimeInfo, strDateNdTime, RawDate, FnDate
Dim FSO
Set fsoClip = CreateObject("Scripting.FileSystemObject")
Set SysClip = CreateObject("JSSys3.ops")
strComputer = "."
sHarvey = ""
lenHarvey = 0
HarveyTrimmed = ""
'
sPrgDir = "C:\Apps\Recent.Files\"
sTransitOnlyDir = "C:\Apps\Recent.Files\TransitOnly\"
sTargetDir = "C:\Apps\Recent.Files\Recent.Files\"
sDate = "...(date)...lnk"
sUserRecentDir = "C:\Documents and Settings\marxp\Recent\"
'
'
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\Documents and
Settings\\\\marxp\\\\Recent""'")
Do
'
' The next X lines, cull the shortcut file's Fulname out of something or
other (that I don't understand), and
' assign the Fulname to the sVar "sHarveyOrigFulNam".
'
Set objLatestEvent = colMonitoredEvents.NextEvent
sHarvey = objLatestEvent.TargetInstance.PartComponent
lenHarvey = Len(sHarvey)
sHarvey = Replace(sHarvey, """", Chr(39))
sHarvey = Replace(sHarvey, "\\", "\")
iQuotOutRight = InstrRev(sHarvey, "'", -1, 1)
iQuoteIn = InstrRev(sHarvey, "'", iQuotOutRight - 1, 1)
sHarveyTrimmed = Mid(sHarvey, iQuoteIn + 1, iQuotOutRight - iQuoteIn - 1)
'
'The next few lines create a sVar of the filename only, culling it from the
Fullname: sHarveyTrimmed,
' creating the sVar named: sHarveyNamOnly
lenHarvey = Len(sHarveyTrimmed)
iSlash = InstrRev(sHarveyTrimmed, "\", -1, 1)
sHarveyNamOnly = Mid(sHarveyTrimmed, iSlash + 1, lenHarvey)
'
sHarveyOrigFulNam = sUserRecentDir & sHarveyNamOnly
sHarveyTransitOnlyFulNam = sTransitOnlyDir & sHarveyNamOnly
'
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Verify that sHarveyOrigFulNam exists, and then copy it to TransitDir;
' Otherwise, report that it doesn't exist.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sHarveyOrigFulNam) Then
Set objFolderOrig = objFSO.GetFile(sHarveyOrigFulNam)
objFSO.copyFile sHarveyOrigFulNam, sHarveyTransitOnlyFulNam
Else
Wscript.Echo "File does not exist. You must stop and restart the script."
End If
Loop
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile(sHarveyOrigFulNam)
If objFSO.FileExists(sHarveyTransitOnlyFulNam) Then
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile(sHarveyOrigFulNam)
iDeleteEfforts = 0
sExistsDarnIt = 0
If objFSO.FileExists(sHarveyOrigFulNam) Then
Do While sExistsDarnIt < 1
If iDeleteEfforts >= 1000 Then
Wscipt.echo "Can't delete file: " & sHarveyOrigFulNam
Exit Do
End if
WScript.sleep 50 '--wait a while before trying again
If objFSO.FileExists(sHarveyOrigFulNam) Then
iDeleteEfforts = iDeleteEfforts + 50
Else
sExistsDarnIt = 1
End if
Loop
End If
Else
Wscript.Echo "The transit file " & sHarveyTransitOnlyFulNam & "does not
exist. You must stop and restart the script."
End If
'
strStartDate = Now
sUsableDate(strStartDate)
StartDate = FnDate
'
'
sHarveyNuFulNam = sTargetDir & sHarveyNamOnly & StartDate & ".lnk"
objFSO.copyFile sHarveyTransitOnlyFulNam, sHarveyNuFulNam
objFSO.DeleteFile(sHarveyTransitOnlyFulNam)
'
'
'******************************************************************************
Function Snd2ClpBrd(Var)
s = Var
v = SysClip.SendTextCB(s)
WScript.sleep 800
End Function
'******************************************************************************
'
'******************************************************************************
Function sUsableDate(RawDate) ' = FnDate
' FnDate is the variable that this function produces.
FnDate = FormatDateTime(RawDate, vbShortDate)
intFirstSlash = InStr(FnDate, "/")
strMonth = Left(FnDate, intFirstSlash - 1)
If Abs(strMonth) < 10 Then strMonth = "0" & strMonth End If
int2dSlash = InStr(intFirstSlash + 1, FnDate, "/")
strDay = Mid(FnDate, intFirstSlash + 1, int2dSlash - intFirstSlash - 1)
If Abs(strDay) < 10 Then strDay = "0" & strDay End If
'
strTime = FormatDateTime(RawDate, vbLongTime)
'' The next line ascertains whether it's day or night (A or P)
strDayOrNite = mid(strTime, 9, 9)
'' The next line removes the " AM" or " PM"
strTime = Left(strTime, Len(strTime) - 3)
'' The next line determines where the hour ends, bc there's a colon there.
intFirstColon = InStr(strTime, ":")
'' The next line returns the hour
strHour = Left(strTime, intFirstColon - 1)
strMinEtSec = Right(strTime, Len(strTime) + 1 - intFirstColon)
'
If Abs(strHour) < 10 Then strHour = "0" & strHour End If
'' wscript.echo "strHour<10, therefore=" & strHour
If strDayOrNite <> "P" Then strHour = strHour + 12 End If
'' wscript.echo "strDayOrNite <> P, therefore=" & strHour
''
strDateNdTime = Year(Now) & "." & strMonth & "." & strDay & ".." & strHour &
strMinEtSec
FnDate = Replace(strDateNdTime, ":", ".")
End Function
'******************************************************************************