Subject: Something you should think about…
What if the file names are not unique?
Create a naming convention. Is it important to track by user?
Here is some code:
…
pstrFullPath$=Cstr({c:\inbound\} & yrfile & {\}& categ & {\})
'Function createPath(pstrFullPath As String) As Boolean
If createPath(pstrFullPath$) = False Then
Msgbox "failed to create directory!: " & pstrFullPath$
Exit Sub
Else
Call createPath(pstrFullPath$)
End If
Set rtitem = doc.GetFirstItem( "Body" )
fileCount = 0
plainText=""
If ( rtitem.Type = RICHTEXT ) Then
'stop
If Isempty(rtitem.EmbeddedObjects)=False Then
Forall o In rtitem.EmbeddedObjects
REM Note how many files we have processed
fileCount = fileCount + 1
'Presuming the doc has an attachment, now the below line
'has an error, probably because dbug isnt Dim and set
agentLog.LogAction("file count:"+Cstr(fileCount))
If ( o.Type = EMBED_ATTACHMENT ) Then
FileName=datestring & o.Source
'stop
If FileExists(pstrFullPath$ & FileName)=True Then
Call agentLog.LogAction("File exists: " & FileName)
'stop
Call GetUniqueFileName(pstrFullPath$, FileName)
End If
txtFile= GetUniqueFileName(pstrFullPath$, FileName)
Call o.ExtractFile(pstrFullPath$ & txtFile)
End If
End Forall
Else
plainText = rtitem.GetFormattedText( False, 0 )
FileName=datestring & categ & ".txt"
txtFile = pstrFullPath$ & FileName
If FileExists(pstrFullPath$ & FileName)=True Then
'stop
Call agentLog.LogAction("File exists: " & FileName)
FileName= GetUniqueFileName(pstrFullPath$, FileName)
txtFile = pstrFullPath$ & FileName
End If
fnum = Freefile
Open txtFile For Output As fnum
Print #fnum, rtitem.GetFormattedText(False, 0);
Close fnum
End If
End If
Set doc=dc.getnextdocument(doc)
Wend
…
Function FileExists (FileName As String) As Variant
On Error 53 Resume Next
Dim FileDir As String
FileExists = False
FileDir = Dir$(FileName, 0)
If Len(FileDir) > 0 Then
FileExists = True
End If
End Function
Function StringReplaceSubstring(_
SourceString As String,_
StringToReplace As String,_
StringReplaceWith As String) As String
'END FUNCTION DECLARATION*
Dim StringLeft,StringRight,StringWhole As String
Dim intPosition,intRepLen As Integer
StringReplaceSubstring = ""
intRepLen = Len(StringToReplace)
intPosition = Instr(1,SourceString,StringToReplace,1)
While intPosition <> 0
StringReplaceSubstring = StringReplaceSubstring & Left$(SourceString,intPosition-1) & StringReplaceWith
SourceString = Mid$(SourceString,intPosition+intRepLen,32000)
intPosition = Instr(1,SourceString,StringToReplace,1)
Wend
StringReplaceSubstring = StringReplaceSubstring & SourceString
End Function
Function createPath(pstrFullPath As String) As Boolean
'// GLOBAL VARIABLES:
'//
'//
'// 02/22/2000 - Dallas Gimpel
'//
'// DESCRIPTION:
'// Given a path as input, this function will attempt to traverse the path and create the
'// necessary directory structure on the local hard drive assuming OS to be Win32.
'//
'// OUTPUT:
'// Function returns true if it is able to create the given directory structure, otherwise
'// it returns false
On Error Goto errorHandler
Const MB_ICONEXCLAMATION = 48
Const MB_OK = 0
Const ATTR_DIRECTORY = 16
Dim i As Integer
Dim strRoot As String
Dim strDirStruct As String
Dim varAllDirs As Variant
createPath = False
If Not(Instr(pstrFullPath$, ":\") = 2) Then
Msgbox "The path could not be interpreted - unable to continue.", MB_OK + MB_ICONEXCLAMATION, "Unable to continue . . ."
Goto functionExit
End If
strRoot$ = Left(pstrFullPath$, 1) & ":\"
If Dir$(strRoot$, ATTR_DIRECTORY) = "" Then '// the root directory is invalid
Msgbox "The root directory is invalid - unable to continue.", MB_OK + MB_ICONEXCLAMATION, "Unable to continue . . ."
Goto functionExit
End If
strDirStruct$ = strRoot$
varAllDirs = Split(pstrFullPath$, "\")
For i = (Lbound(varAllDirs) + 1) To Ubound(varAllDirs)
strDirStruct$ = strDirStruct & varAllDirs(i)
If Dir$(strDirStruct$, ATTR_DIRECTORY) = "" Then '// directory doesn't exist - create it
Mkdir strDirStruct$
End If
strDirStruct$ = strDirStruct & "\"
Next i
createPath = True
functionExit:
Exit Function
errorHandler:
Msgbox "Error " & Err & ": " & Error$ & " encountered at line " & Erl & " of " & Getthreadinfo(1) & ".", , "Error encountered . . ."
Print "Error " & Err & ": " & Error$ & " encountered at line " & Erl & " of " & Getthreadinfo(1) & " . . ."
Stop
Resume functionExit
End Function
Function GetUniqueFileName (filePath As String, fileName As String) As String
Stop
Dim splitName As Variant
Dim fileExistsString As String
Dim fileNum As Integer
Dim uniqueName As String
fileNum = 0
uniqueName = fileName
CheckName:
’ fileExistsString = Dir$( filePath & uniqueName, 0)
’ If FileExists(pstrFullPath$ & uniqueName)=False Then
If FileExists(filePath & uniqueName)=False Then
’ If Len(fileExistsString) = 0 Then
Goto ExitFunction
Else
fileNum = fileNum + 1
splitName = Split(fileName, ".", 2)
uniqueName = splitName(0) + " (" + Cstr(fileNum) + ")." + splitName(1)
Goto CheckName
End If
ExitFunction:
GetUniqueFileName = uniqueName
End Function