M
Mike
Anthony said:The below was my reply to Peter Duniho. In light of this information do we
still think that the delete is the issue? The way I am reading the code we
are bombing at the first attempt to open the file, and we aren't getting
past that point. Should I still put in a delay.
The code has been modified as follows based on the advisement of another
posting.
If File.Exist(FullPathName) Then
Dim sr As StreamReader
Dim sw As StreamWriter
try
sr = File.OpenText(FullPathName)
try
sw = File.CreateText(NewFullPathName)
... do your work ..
catch ex as exception
msgbox("! OPEN FOR WRITING ERROR : "+ex.Message)
finally
sw.close()
end try
catch ex as exception
msgbox("! OPEN FOR READING ERROR: "+ex.Message)
finally
sr.Close()
end try
try
File.Delete(FullPathName)
try
File.Move(NewFullPathName, FullPathName)
catch ex as exception
msgbox("! ERROR MOVING FILE: "+ex.Message)
end try
catch ex as exception
msgbox("! ERROR DELETING FILE: "+ex.Message)
end try
End If
The message box displayed was indeed the
"! OPEN FOR READING ERROR: The process cannot access the file <filename>
because it is being used by another process"
As can be seen, we did not make it to the point were we would attempt to
delete and/or move the file. For that matter we did not even make it to the
next step were we create a new text file.
Thats correct. Now, technically, you would put an RETURN FALSE in the
catch so that it doesn't go to the next block outside the catch block.
You should make a function out of this like so:
Function WorkOnFile(src as string, tar as string) as BOOLEAN
If not File.Exists(src) Then
return FALSE
end if
Dim sr As StreamReader
Dim sw As StreamWriter
try
sr = File.OpenText(src)
try
sw = File.CreateText(tar)
'... do your work ..
catch ex as exception
msgbox("! OPEN FOR WRITING ERROR : "+ex.Message)
return FALSE
finally
sw.close()
end try
catch ex as exception
msgbox("! OPEN FOR READING ERROR: "+ex.Message)
return FALSE
finally
sr.Close()
end try
try
File.Delete(src)
try
File.Move(tar, src)
catch ex as exception
msgbox("! ERROR MOVING FILE: "+ex.Message)
return FALSE
end try
catch ex as exception
msgbox("! ERROR DELETING FILE: "+ex.Message)
return FALSE
end try
return TRUE
End Function