Check if file exists - then delete

Hi There,

I am trying to check to see if a file is there before deleteing it and re-creating it. How would I do this?

It works fine as long as they have a signature file, but if its the first time they have ran the agent, they will get an error. Anyone have any ideas as to how to check before deleting?

Thanks

Jamie.

Code :

Sub Click(Source As Button)

'Standard variables

Dim s As New NotesSession

Dim ws As New NotesUIWorkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim ProfDoc As NotesDocument

Dim AddBkdb As NotesDatabase

Dim AddBkV As NotesView

Dim entry As NotesViewEntry

Dim nam As NotesName

Dim result As Integer



'Variables for the data we will get back from the person's address book entry

Dim strName As String

Dim strJobTitle As String

Dim strStreet As String

Dim strCity As String

Dim strState As String

Dim strPostCode As String

Dim strCountry As String

Dim strPhoneNo As String

Dim strFaxNo As String

Dim strMobile As String

Dim strEmail As String

Dim strWeb As String

Dim directory As String

Dim fileName As String



'Get the user's data directory - to save the signature in.

directory = s.GetEnvironmentString( "Directory", True )



'Setting and getting the required databases (current and Corporate Address book)

Set db = s.CurrentDatabase

Set AddBkdb = s.GetDatabase("SERVER","ADDBOOK.NSF",False)



'Get the profile doc in the mail file which sets the signature

Set ProfDoc = db.GetProfileDocument("CalendarProfile")



'Setting the name for the Signature

Set nam = s.CreateName(s.EffectiveUserName)

strName = Cstr(nam.Common)





'Set view to lookup person's record in the Address Book - using ViewEntry so its efficient and doesn't open every document

Set AddBkV = AddBkdb.GetView("SigLkp")

Set entry = AddBkV.GetEntryByKey(strName)



If entry Is Nothing Then

	Msgbox "Cannot locate Address Book record, or Record is incomplete - Please contact I.T. Service Desk"

	Exit Sub

End If



'Set the document to the one found in the address book

Set doc = entry.Document



'Populate the strings with the info from the person's record

strJobTitle = doc.JobTitle(0)

strPhoneNo = "T: " & doc.OfficePhoneNumber(0)

strFaxNo = "F: " & doc.OfficeFAXPhoneNumber(0)

strMobile = "M: " & doc.CellPhoneNumber(0)

strEmail = "E: " & doc.MailAddress(0)

strStreet = doc.OfficeStreetAddress(0)

strCity = doc.OfficeCity(0)

strState = doc.OfficeState(0)

strPostCode = doc.OfficeZip(0)

strCountry = doc.OfficeCountry(0)

strWeb = "W: http://www.company.com"



'Open/Create the signature file in the person's data directory

If directory = "C:\Program Files\lotus\notes\data" Then

	fileName$ = "C:\Program Files\lotus\notes\data\"& strName & "'s signature.html"

	Kill fileName$

	Exit Sub

	Open "C:\Program Files\lotus\notes\data\" & strName & "'s signature.html" For Output As #1

Else

	fileName$ = "C:\Lotus\notes\data\" & strName & "'s signature.html"

	Kill fileName$

	Exit Sub

	Open "C:\Lotus\notes\data\" & strName & "'s signature.html" For Output As #1

End If



'Print the HTML and user's detail into the HTML file and save



Print #1, "<html>"

Print #1, "<Body>"

Print #1, "<br /><font size=2><font face=""Arial"">_________________________________________________"

Print #1, "<br>"

Print #1, "<br />" & strName

Print #1, "<br />" & strJobTitle

Print #1, "<br />Clyde Pumps Ltd"

Print #1, "<br /></font></font><font size=1><font face=""Arial"">Incorporating WEIR PUMPS Glasgow"

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strStreet 

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strCity

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strState

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strCountry

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strPostCode

Print #1, "<br />" & strPhoneNo

If strFaxNo <> "F: " Then

	Print #1, "<br />" & strFaxNo

End If

If strMobile <> "M: " Then

	Print #1, "<br />" & strMobile

End If

Print #1, "<br />" & strEmail

Print #1, "<br />" & strWeb

Print #1, "<br /><font size=2><font face=""Arial"">_________________________________________________"

Print #1, "<br /></font></font>"

Print #1, "</body>"

Print #1, "</HTML>"

Close #1



'Update the profile doc to use the new signature

ProfDoc.SignatureOption = "2"

If directory = "C:\Program Files\lotus\notes\data" Then

	ProfDoc.Signature =  "C:\Program Files\lotus\notes\data\" & strName & "'s signature.html"

Else

	ProfDoc.Signature_2 = "C:\lotus\notes\data\" & strName & "'s signature.html"

End If



'Save the profile Doc

Call ProfDoc.Save(True,True)



'Run a Lotus agent to apply the change

Call UpdateAltfields(ProfDoc)

End Sub

Subject: Check if file exists - then delete

Look at the Dir statement, there’s a good example in the help of checking files in a folder so you could check that the file exists or not

Dan

Subject: File/Path access error!

Thanks for the suggestion, I have used the Dir statement, but i am now getting a File/Path access error!

any suggestions? (I have searched and tried a sleep 2)

Thanks

Jamie

Code :

Sub UpdateSig

'Standard variables

Dim s As New NotesSession

Dim ws As New NotesUIWorkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim ProfDoc As NotesDocument

Dim AddBkdb As NotesDatabase

Dim AddBkV As NotesView

Dim entry As NotesViewEntry

Dim nam As NotesName

Dim result As Integer

Dim intCheck As Integer



'Variables for the data we will get back from the person's address book entry

Dim strName As String

Dim strJobTitle As String

Dim strStreet As String

Dim strCity As String

Dim strState As String

Dim strPostCode As String

Dim strCountry As String

Dim strPhoneNo As String

Dim strFaxNo As String

Dim strMobile As String

Dim strEmail As String

Dim strWeb As String

Dim directory As String

Dim PathName As String

Dim fileName As String



intCheck = 0



'Get the user's data directory - to save the signature in.

directory = s.GetEnvironmentString( "Directory", True )



'Setting and getting the required databases (current and Corporate Address book)

Set db = s.CurrentDatabase

Set AddBkdb = s.GetDatabase("server","ADDBOOK.nsf",False)



'Get the profile doc in the mail file which sets the signature

Set ProfDoc = db.GetProfileDocument("CalendarProfile")



'Setting the name for the Signature

Set nam = s.CreateName(s.EffectiveUserName)

strName = Cstr(nam.Common)





'Set view to lookup person's record in the Address Book - using ViewEntry so its efficient and doesn't open every document

Set AddBkV = AddBkdb.GetView("SigLkp")

Set entry = AddBkV.GetEntryByKey(strName)



If entry Is Nothing Then

	Msgbox "Cannot locate Address Book record, or Record is incomplete - Please contact I.T. Service Desk"

	Exit Sub

End If



'Set the document to the one found in the address book

Set doc = entry.Document



'Populate the strings with the info from the person's record

strJobTitle = doc.JobTitle(0)

strPhoneNo = "T: " & doc.OfficePhoneNumber(0)

strFaxNo = "F: " & doc.OfficeFAXPhoneNumber(0)

strMobile = "M: " & doc.CellPhoneNumber(0)

strEmail = "E: " & doc.MailAddress(0)

strStreet = doc.OfficeStreetAddress(0)

strCity = doc.OfficeCity(0)

strState = doc.OfficeState(0)

strPostCode = doc.OfficeZip(0)

strCountry = doc.OfficeCountry(0)

strWeb = "W: http://www.company.com"	



'Open/Create the signature file in the person's data directory

If directory = "C:\Program Files\lotus\notes\data" Then

	pathName$ = "C:\Program Files\lotus\notes\data\*.html"

	fileName$ = Dir$(pathName$,0)

	Do While intCheck <> 1 Or fileName$ <> ""

		If fileName$ = strName & "'s signature.html" Then

			intCheck = 1

			Sleep 1

			Kill fileName$

		End If

	Loop

	Open "C:\Program Files\lotus\notes\data\" & strName & "'s signature.html" For Output As #1

Else

	pathName$ = "C:\Program Files\lotus\notes\data\*.html"

	fileName$ = Dir$(pathName$,0)

	Do While intCheck <> 1 Or fileName$ <> ""

		If fileName$ = strName & "'s signature.html" Then

			intCheck = 1

			Sleep 1

			Kill fileName$

		End If

	Loop

	Open "C:\Lotus\notes\data\" & strName & "'s signature.html" For Output As #1

End If



'Print the HTML and user's detail into the HTML file and save



Print #1, "<html>"

Print #1, "<Body>"

Print #1, "<br /><font size=2><font face=""Arial"">_________________________________________________"

Print #1, "<br>"

Print #1, "<br />" & strName

Print #1, "<br />" & strJobTitle

Print #1, "<br />Clyde Pumps Ltd"

Print #1, "<br /></font></font><font size=1><font face=""Arial"">Incorporating WEIR PUMPS Glasgow"

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strStreet 

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strCity

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strState

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strCountry

Print #1, "<br /></font></font><font size=2><font face=""Arial"">" & strPostCode

Print #1, "<br />" & strPhoneNo

If strFaxNo <> "F: " Then

	Print #1, "<br />" & strFaxNo

End If

If strMobile <> "M: " Then

	Print #1, "<br />" & strMobile

End If

Print #1, "<br />" & strEmail

Print #1, "<br />" & strWeb

Print #1, "<br /><font size=2><font face=""Arial"">_________________________________________________"

Print #1, "<br /></font></font>"

Print #1, "</body>"

Print #1, "</HTML>"

Close #1



'Update the profile doc to use the new signature

ProfDoc.SignatureOption = "2"

If directory = "C:\Program Files\lotus\notes\data" Then

	ProfDoc.Signature =  "C:\Program Files\lotus\notes\data\" & strName & "'s signature.html"

Else

	ProfDoc.Signature_2 = "C:\lotus\notes\data\" & strName & "'s signature.html"

End If



'Save the profile Doc

Call ProfDoc.Save(True,True)



'Run a Lotus agent to apply the change

Call UpdateAltfields(ProfDoc)

End Sub

Subject: RE: File/Path access error!

You check for directory = c:\program files\lotus\notes\data, but if it isn’t that you still set pathname to c:\program files\lotus\notes\data.

I’m not quite sure what you’re trying to check with that - surely you just need to look up the directory and set pathname to that directory.

You need to say at which line you get the error though (use the debugger), as I can’t tell for sure obviously.

What are the sleep statements for?

Dan

Subject: Working…For me!

Hi,

I changed the code, but it seems to be generating the same error (file/path access error) for some users and not others, any ideas?

Thanks

Jamie

Subject: RE: Working…For me!

Not without knowing what your new code is, and where the error occurs.

Is there a pattern as to where the notes data directory is on the PCs that it works and doesn’t work on?