If an input html element of type=file is used to select a file then that file cannot be deleted by the hta program.
This MVCE works but doesn't use the file dialog - you have to type the filename in manually:
<html>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Set x = CreateObject("Scripting.FileSystemObject")
MsgBox "this will actually delete "& INIFile.Value
x.DeleteFile INIFile.Value
MsgBox "see? "& INIFile.Value &" is gone"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<body id='body'>
<input type="text" name="INIFile" >
<input type="button" value="Go!" onClick="Process" >
</body>
</html>
But this MVCE doesn't work - the file is not deleted; just deferred until the program exits:
<html>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Set x = CreateObject("Scripting.FileSystemObject")
MsgBox "try to manually delete "& INIFile.Value &" (and then undo it)"
x.DeleteFile INIFile.Value
MsgBox "now try to delete file "& INIFile.Value &" (now it can't be deleted until the app is closed)"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<body id='body'>
<input type="file" name="INIFile" >
<input type="button" value="Go!" onClick="Process" >
</body>
</html>
Somehow using a file type input html element makes it so that the file CAN be manually deleted from outside the program UNTIL the DeleteFile function is called. The DeleteFile function doesn't actually delete the file - it just deferrs the deletion until the hta program exits - at which point the file finally deletes itself.
I need to delete the file while the program is still running. Is there any way to use a file type input html element in an hta file and still delete the file while the hta program is running?
EDIT
My actual use case! In an attempt to produce a usable MVCE I didn't realize a solution would be found that doesn't work with my particular requirements.
The reason I am deleting the file is so that I can replace it with something else, so I need the file to disappear before the end of the function. Call window.location.reload()
absolutely works but the file disappears at the end of the function.
What I am actually trying to do is something like this:
<HTML>
<HEAD>
<SCRIPT Language="VBScript">
Sub Process
Dim file: file = INIFile.Value
Call window.location.reload()
'backup the file to tempfile.tmp
'Now edit tempfile.tmp with all the changes and preview it
'then ask the user whether they are happy with the changes
'delete the original file
'and put the tempfile.tmp in its place
Dim x: Set x = CreateObject("Scripting.FileSystemObject")
x.CopyFile file,"tempfile.tmp"
x.DeleteFile file
MsgBox "why is "& file &" still there?"
x.MoveFile "tempfile.tmp",file ' this produces "file already exists"
Set x = Nothing
End Sub
</SCRIPT>
</HEAD>
<BODY id='body'>
<INPUT type="file" name="INIFile" onChange="Process">
</BODY>
</HTML>