Results 1 to 5 of 5

Thread: Force Delete A file

  1. #1

    Thread Starter
    Hyperactive Member razzaj's Avatar
    Join Date
    Oct 1999
    Location
    jounieh
    Posts
    261
    Is there a Way to delete files even if they are open without giving Sharing Violation Error

    By the way 'Kill' Doesnt Work ... I tried it

    [Edited by razzaj on 09-26-2000 at 09:34 AM]
    - regards -
    - razzaj -

  2. #2
    Guest
    I suspect this will not help you much, as it sounds like you are trying to replace a working file with another.

    Presumably, you are trying to close a file inadvertantly left open by another process, in use but needs to be replaced, or are cleaning up after finising a testing run and trying to restart.

    Since you cannot delete an open file, you will have to grab the file that is open and close it.

    I would assume that if you had a network, and your network priveleges include "administrator", you could overrule (???) the current holder and close the file, deleting it on the next line.

    If you cannot overrule the user then you can't delete the file. If its a matter of creating a temporary file for intermediate use, you can always use freefile (at least that's what is was called prior to windows) to get a guaranteed kosher, free name.



    Good Luck
    DerFarm

  3. #3
    Guest
    You cannot delete a file that is in use. But you can close the exe first:

    Code:
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
        End Type
    
    
    Public Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    Usage:
    
    Call KillApp("C:\program.exe")
    And then you can simply delete it:

    Code:
    Call Kill("C:\program.exe")

  4. #4

    Thread Starter
    Hyperactive Member razzaj's Avatar
    Join Date
    Oct 1999
    Location
    jounieh
    Posts
    261

    The true Problem

    Here is the problem, Sometimes on windowsNT or others the system goes nuts and refuses to let you delete a File might it be a simple Picture to a complex ASP File you get this error : "Sharing Violation ... etc..."

    What I would like is either to force delete this file or maybe force close it then Delete it (it is not an executable ). Now that I think of it ... I am going to see how to know which process is using the file and kill it then delete the file ... but I have no idea how
    - regards -
    - razzaj -

  5. #5
    Guest
    Mathew has the right of it. If you have a rogue process, you are going to have to close it. On my machine, I sometimes have to go the length of a re-boot.

    There is undoubtedly a way to do this without such drastic measures, but I don't have the time to mess with it.

    As a first measure, could you call the friendly folks at MS for help?

    Alternatively,
    http://www.perryland.com/cgi-bin/Ult...rder=&Session=

    seems to be a good place to start.

    Good Luck
    DerFarm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width