Filename is not deleted?

Windows specific forum
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Filename is not deleted?

Post by stmdbe2019 »

why the file is not deleted?

Code: Select all

executedFilename.s  = ProgramFilename()
executedFromFile.s = GetFilePart(executedFilename, #PB_FileSystem_NoExtension)

Procedure msgbox(input.s)
  MessageRequester("Notice" , input)
EndProcedure

; Command line arguments
userID.s = ""
If FindString(executedFromFile, "_")
  userID.s = StringField(executedFromFile, 2, "_")
  msgbox(userID)
Else 
  msgbox("Invalid filename")
EndIf

DeleteFile(executedFilename)
-----
Registered PureBasic Coder.
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Filename is not deleted?

Post by infratec »

Hi,

1. Maybe your file does not exist.
You want tto delete the running program filename without extension.

2. If you want to delete the running program file...
this is not possible, because it is in use.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Filename is not deleted?

Post by stmdbe2019 »

2. If you want to delete the running program file. this is not possible, because it is in use.
> I need to remove the program, because it has to run once (after the program is exited is also OK if i can delete it,
does not has to be deleted while its running)

Code: Select all

executedFilename.s  = ProgramFilename()
executedFromFile.s = GetFilePart(executedFilename, #PB_FileSystem_NoExtension)

Procedure msgbox(input.s)
  MessageRequester("Notice" , input)
EndProcedure

; Command line arguments
userID.s = ""
If FindString(executedFromFile, "_")
  userID.s = StringField(executedFromFile, 2, "_")
  msgbox(userID)
Else 
  msgbox("Invalid")
EndIf

rm = DeleteFile(executedFilename)
If rm 
  msgbox( "Deleted" )
Else
  msgbox( "Not deleted" )
EndIf
-----
Registered PureBasic Coder.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Filename is not deleted?

Post by stmdbe2019 »

[SOLVED]

Code: Select all


Procedure removeExeOnExit()  
  ; Executed File
  executedFilename.s  = ProgramFilename()
  executedPath.s  = GetPathPart(executedFilename)
  executedFromFile.s = GetFilePart(executedFilename, #PB_FileSystem_NoExtension)
  executedFromFileWithExtension.s = GetFilePart(executedFilename)
  executeBatfile.s =  executedPath + executedFromFile + ".bat"
  renameFilename.s = executedFilename + ".removed"
  userID.s = ""
  If FindString(executedFromFile, "_")
    userID.s = StringField(executedFromFile, 2, "_")
    ;msgbox(userID)
  Else 
    ;msgbox("Invalid")  
  EndIf
  
  rm = DeleteFile(executedFilename)
  If rm 
    ;msgbox("Deleted")
  Else  
    RenameFile(executedFilename, renameFilename)  
    If CreateFile(0, executeBatfile)         
      WriteStringN(0, "taskkill /f /im " + executedFromFileWithExtension) 
      WriteStringN(0, "del /s /q " + #DQUOTE$ + renameFilename  +  #DQUOTE$ )
      CloseFile(0)                           
      RunProgram(executeBatfile)
    Else
      ;msgbox("Permission denied")
    EndIf      
  EndIf

EndProcedure

removeExeOnExit()


-----
Registered PureBasic Coder.
Post Reply