bobbysteven
asked on
Lotus Script for Detaching files
I'm a vba programmer new to lotus notes script. I have a sitution where I am receiving messages with attachments. Currently, I am writing a script to detach the attachment to a directory on the pc.
The problem I am having is when I detach files with the same file name. For example, if I have two messages with the same attachment name, the second file overwrites the first file.
I can't figure out how to have the second file saved in the same directory as a different name. Is there a command or routine that will let me know if the file being detached exists in the directory? The save and the extract command doesn't seem to have any attributes other the path associated with them.
Below is my code. Any suggestion would be greatly appreciated.
Sub Initialize
Dim session As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim vw As NotesView
Dim folderName As String
Dim workspace As New NotesUIWorkspace
Dim view As NotesUIView
Set view = workspace.CurrentView
foldername = view.ViewName
Set session = New NotesSession
Set db = session.CurrentDatabase
Set vw = db.GetView( foldername)
Set doc = vw.getfirstdocument
Do Until doc Is Nothing
If doc.HasEmbedded Then
Dim rtitem As Variant
Set rtitem = doc.GetFirstItem( "Body" )
Forall o In rtitem.EmbeddedObjects
If o.Type = EMBED_ATTACHMENT Then
Call o.ExtractFile( "c:\newfiles\" & o.Source )
Call doc.Save( True, True )
End If
End Forall
End If
Set doc = vw.getnextdocument(doc)
Loop
End Sub
Thanks.
Bob
The problem I am having is when I detach files with the same file name. For example, if I have two messages with the same attachment name, the second file overwrites the first file.
I can't figure out how to have the second file saved in the same directory as a different name. Is there a command or routine that will let me know if the file being detached exists in the directory? The save and the extract command doesn't seem to have any attributes other the path associated with them.
Below is my code. Any suggestion would be greatly appreciated.
Sub Initialize
Dim session As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim vw As NotesView
Dim folderName As String
Dim workspace As New NotesUIWorkspace
Dim view As NotesUIView
Set view = workspace.CurrentView
foldername = view.ViewName
Set session = New NotesSession
Set db = session.CurrentDatabase
Set vw = db.GetView( foldername)
Set doc = vw.getfirstdocument
Do Until doc Is Nothing
If doc.HasEmbedded Then
Dim rtitem As Variant
Set rtitem = doc.GetFirstItem( "Body" )
Forall o In rtitem.EmbeddedObjects
If o.Type = EMBED_ATTACHMENT Then
Call o.ExtractFile( "c:\newfiles\" & o.Source )
Call doc.Save( True, True )
End If
End Forall
End If
Set doc = vw.getnextdocument(doc)
Loop
End Sub
Thanks.
Bob
The Error# returned if file not found is 53, handle this in error trap routine.
In the same vein as GetFileAttr, here is some code:
Function ExistFileDirDrive(FilePath Name As String) As Variant
Dim nTest As String
ExistFileDirDrive = True
nTest = Lcase$(FilePathName)
If Len(nTest) = 1 Then
Select Case Left$(nTest, 1)
Case "a" To "z"
nTest = FilePathName & ":\"
End Select
Elseif Len(nTest) = 2 Then
Select Case Left$(nTest, 2)
Case "a:" To "z:"
nTest = FilePathName & "\"
End Select
End If
On Error Resume Next
If Clng(Getfileattr(nTest)) = 0 Then
ExistFileDirDrive = False
End If
End Function
Function ExistFileDirDrive(FilePath
Dim nTest As String
ExistFileDirDrive = True
nTest = Lcase$(FilePathName)
If Len(nTest) = 1 Then
Select Case Left$(nTest, 1)
Case "a" To "z"
nTest = FilePathName & ":\"
End Select
Elseif Len(nTest) = 2 Then
Select Case Left$(nTest, 2)
Case "a:" To "z:"
nTest = FilePathName & "\"
End Select
End If
On Error Resume Next
If Clng(Getfileattr(nTest)) = 0 Then
ExistFileDirDrive = False
End If
End Function
ASKER
Thanks Hemanth, Capper,
It points me in the right direction. From a vb point it seems straight forward, but as always, implemenation is another problem. Capper, I believe your code checks to validate the path, whereas, I need to validate whether the file already exists. I modified your code as follows where variable apple = filename I am checking, and valuescore, namescore are variables to track the value of the getfileattr command.
I get an error that "the file is not found" on line
namescore =Clng(Getfileattr(apple)). Any ideas on where I'm going wrong? Once again thanks for your help.
Bob
Function ExistFileDirDrive(FilePath Name As String, apple As Variant) As Variant
Dim nTest As String
ExistFileDirDrive = True
nTest = Lcase$(FilePathName)
If Len(nTest) = 1 Then
Select Case Left$(nTest, 1)
Case "a" To "z"
nTest = FilePathName & ":\"
End Select
Elseif Len(nTest) = 2 Then
Select Case Left$(nTest, 2)
Case "a:" To "z:"
nTest = FilePathName & "\"
End Select
End If
valuescore =Clng(Getfileattr(ntest))
Chdir ntest
namescore =Clng(Getfileattr(apple))
On Error Resume Next
If Clng(Getfileattr(nTest)) = 0 Then
ExistFileDirDrive = False
End If
End Function
It points me in the right direction. From a vb point it seems straight forward, but as always, implemenation is another problem. Capper, I believe your code checks to validate the path, whereas, I need to validate whether the file already exists. I modified your code as follows where variable apple = filename I am checking, and valuescore, namescore are variables to track the value of the getfileattr command.
I get an error that "the file is not found" on line
namescore =Clng(Getfileattr(apple)).
Bob
Function ExistFileDirDrive(FilePath
Dim nTest As String
ExistFileDirDrive = True
nTest = Lcase$(FilePathName)
If Len(nTest) = 1 Then
Select Case Left$(nTest, 1)
Case "a" To "z"
nTest = FilePathName & ":\"
End Select
Elseif Len(nTest) = 2 Then
Select Case Left$(nTest, 2)
Case "a:" To "z:"
nTest = FilePathName & "\"
End Select
End If
valuescore =Clng(Getfileattr(ntest))
Chdir ntest
namescore =Clng(Getfileattr(apple))
On Error Resume Next
If Clng(Getfileattr(nTest)) = 0 Then
ExistFileDirDrive = False
End If
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Capper,
The example was really helpful in understanding how to use the command. The second comment really clarified what I was looking for. Sorry for the delay in responding!
Bob
The example was really helpful in understanding how to use the command. The second comment really clarified what I was looking for. Sorry for the delay in responding!
Bob
This will return file attribute if file exists or raises an error if file not found, I don't remember the error #, but this is very useful for your task.
~Hemanth