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

'******************************************************************************

Re: How to delete a file in "C:\Documents and Settings\marxp\Recent". by Al

Al
Sun Feb 20 18:36:13 CST 2005


"Marc B. Hankin" <mbh2010@aol.com> wrote in message
news:OAbkGo3FFHA.1396@tk2msftngp13.phx.gbl...
> 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".

This seems a rather dubious and tricky way to produce a log showing time of
work done, even if all of your work is done in Word (and not, say, excel).
You would, for example, have to condition yourself to give the focus to a
"null" window (i.e. one representing "unbillable time") just before each
period where you are not actually doing the work. I don't have ADD, and yet,
knowing in advance when I am about to stop being productive on a task (to
say nothing of recording this event) is even more of a task than simply
noting down the times when I am just starting to do something productive.
And, as you say, doing *that* is difficult enough, ADD or not.

A well as working on your own home-grown timelog system, I would recommend
looking for an off-the-shelf product. I've no experience with the field, but
I strongly suspect that some such products might even have features that
would help you make sure you are "on task" when there are tasks to be done.
How well they integrate into tools such as word is another matter, but even
one which forced you to use its menus for creating/opening/closing documents
(ideally with check-out/check-in logic common in version control packages)
would, in my opinion, be better than a homegrown system. One that worked
that way would likely be able to help you log time spent in other types of
documents, and even time spent doing paperwork by hand. At the same time,
the reoprting capabilities would likely already exist, and not require you
to code them yourself to do such things as reporting time by client or by
project, applying a rate schedule, and etc...

>
>
>
> 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".

After a cursory read, I cannot figure out how the script runs without
throwing various syntax errors. Your script is a bit hard to follow, a
feature that, more often than not, tends to hide implicit assumptions and
other sources of error. I'll make a few comments below in hopes that this
will help a bit...

> 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")

1) you have a number of variables to which you assign the file system
object, some of them more than once. You need only one such variable and one
such instance, and you need only instantiate it once.

> 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 = "."

2) this was already assigned earlier in the script.

> 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

3) I'd suggest that you display these calculated names here just to make
sure that they are formatted exactly as expected.

> '
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")

4a) see my earlier comment on the file system object.

>
>
> 'Verify that sHarveyOrigFulNam exists, and then copy it to TransitDir;
>
> ' Otherwise, report that it doesn't exist.
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")


4b) see my earlier comment on the file system object.

>
> 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."

5) why don't you have your script do this for you, with:

wscript.quit

> End If
>
> Loop
>
>
> Set Shell = CreateObject("WScript.Shell")
>
> Set FSO = CreateObject("Scripting.FileSystemObject")
>
> FSO.DeleteFile(sHarveyOrigFulNam)

6) it could be that the file has been locked by not releasing the object
variable created by this statement above:

Set objFolderOrig = objFSO.GetFile(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

7) I do not see which IF the above ELSE belongs to.

> Wscript.Echo "The transit file " & sHarveyTransitOnlyFulNam & "does not
> exist. You must stop and restart the script."
>
> End If
>
> '
>
> strStartDate = Now
>
> sUsableDate(strStartDate)

8) suggest you either use this as a function (zz=sUsableDate(strStartDate)),
or as a sub, with:

call sUsableDate(strStartDate)

or:

sUsableDate strStartDate

> StartDate = FnDate
>
> '
>
> '
>
> sHarveyNuFulNam = sTargetDir & sHarveyNamOnly & StartDate & ".lnk"
>
> objFSO.copyFile sHarveyTransitOnlyFulNam, sHarveyNuFulNam
>
> objFSO.DeleteFile(sHarveyTransitOnlyFulNam)
>
> '
>
> '
>
>
'***************************************************************************
***
>
> Function Snd2ClpBrd(Var)

9) avoid including functions that are not used.

>
> s = Var
>
> v = SysClip.SendTextCB(s)
>
> WScript.sleep 800
>
> End Function
>
>
'***************************************************************************
***
>
> '
>
>
'***************************************************************************
***
>
> Function sUsableDate(RawDate) ' = FnDate

10) No value is ever assigned to the function name, sUsableDate. Suggest
re-writing it as a sub.

<snip>

/Al