472,122 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Can not delete a file that is read-only after file.copy

I have a function to copare two files. It will first copy the original file
form a different server to a local temp path and then compare that version to
a version that has been restored form tape. Once the compare is complete the
file that was copied to a temp location needs to be deleted. I am using the
method file.copy(sourcePath, tempPath, true) to copy the file and then
file.delete(tempPath) to delete the file. On some of the files that I am
comparing I am getting

Access to the path "tempPath" is denied.

when I try and delete the file. The only thing that I can find different is
that any file I can not delete is READ-ONLY. The app is running under an
account that is local admin and has full rights to all the directories. Here
is my compare function:
Function FileByteCompareWithCopy(ByVal file1 As String, ByVal file2 As
String) As Integer

Dim file1byte As Integer
Dim file2byte As Integer
Dim fs1 As FileStream
Dim fs2 As FileStream
Dim filePath() As String
Dim copiedFileName As String
Dim errorDescription As String
Dim errorNumber As Integer
Dim moveFileLog As StreamWriter = New StreamWriter(g_sReportDataPath
& "CopyReport_" & g_sTestTime & ".txt", True)

On Error Resume Next
Err.Clear()

'* Get the file name and then append the temp loaction to copy the
file to.
filePath = Split(file1, "\")
copiedFileName = g_tempPath & filePath(UBound(filePath))

'* Copy file to temp locatoin
File.Copy(file1, copiedFileName, True)

If Err.Number > 0 Then
moveFileLog.WriteLine("Copy: " & Err.Description & " " &
copiedFileName)
moveFileLog.Close()
Return 2
End If

'* Open the original file
fs1 = New FileStream(copiedFileName, FileMode.Open, FileAccess.Read,
FileShare.Read)

'* Open the restored file
fs2 = New FileStream(file2, FileMode.Open, FileAccess.Read,
FileShare.Read)

'* Read and compare byte for byte file1 and file2
Do
file1byte = fs1.ReadByte()
file2byte = fs2.ReadByte
Loop While ((file1byte = file2byte) And (file1byte <> -1))

'* Close Files
fs1.Close()
fs2.Close()

Err.Clear()
'* Delete File from temp location
File.Delete(copiedFileName)

If Err.Number > 0 Then
moveFileLog.WriteLine("Delete: " & Err.Description & " " &
copiedFileName)
End If

moveFileLog.Close()

'* Return the result of the comparison
Return ((file1byte - file2byte) = 0)

End Function
Jul 21 '05 #1
0 1956

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Douglas Peterson | last post: by
4 posts views Thread by Elliot M. Rodriguez | last post: by
19 posts views Thread by Xavier Décoret | last post: by
5 posts views Thread by mkaushik | last post: by
12 posts views Thread by yufufi | last post: by
7 posts views Thread by czechboy | last post: by
4 posts views Thread by mail.dsp | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.