Page 1 of 1

Filename is not deleted?

Posted: Wed Dec 27, 2017 10:06 am
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)

Re: Filename is not deleted?

Posted: Wed Dec 27, 2017 10:11 am
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.

Re: Filename is not deleted?

Posted: Wed Dec 27, 2017 10:15 am
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

Re: Filename is not deleted?

Posted: Wed Dec 27, 2017 12:19 pm
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()