By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.