在HTA文件中使用文件输入元素可防止删除所选文件

问题描述:

如果使用type = file的输入html元素来选择文件,则hta程序无法删除该文件。

If an input html element of type=file is used to select a file then that file cannot be deleted by the hta program.

此MVCE有效,但不能t使用文件对话框-您必须手动输入文件名:

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>

但是此MVCE无法正常工作-文件不会被删除;只是推迟到程序退出:

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>

使用文件类型输入html元素以某种方式使其可以从外部手动删除程序,直到DeleteFile函数被调用。 DeleteFile函数实际上并没有删除文件-它只是将删除操作推迟到hta程序退出之前-此时文件最终会自动删除。

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.

我需要删除程序仍在运行时的文件。有什么方法可以在hta文件中使用文件类型输入html元素,并且仍然在hta程序运行时删除该文件?

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 >

我的实际用例!为了产生可用的MVCE,我没有意识到会找到不符合我的特定要求的解决方案。

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.

我删除文件的原因是为了可以将其替换为其他内容,因此我需要在函数结束前将文件消失。 调用window.location.reload()绝对可以,但是文件在函数末尾消失。

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>


使用常规文本输入框

<input type="text" name="FileName" size="30">

添加按钮以单击以打开文件

Add a button to click to open the file

<input type="button" onClick="SelectFile" value="Browse...">

添加文件对话框对象

<OBJECT id=Dlg classid="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B" width=0 height=0>

添加一个子对象以获取该对象的返回值并将其放在文本框中。

Add a sub to take the return value of this object and put it in your text box.

Sub SelectFile
    FileName.value = ""
    strStartPath = "C:\Test"
    strFilter = "Text (*.txt;*.csv)| *.txt;*.csv|VBScript (*.vbs;*.vbc)|*.vbs;*.vbc|HTML (*.htm;*.html;*.hta)|*.htm;*.html;*.hta|All Files (*.*)|*.*|"
    strCaption = "Select a File"
    FileName.value = Dlg.openfiledlg(CStr(strStartPath), , CStr(strFilter), CStr(strCaption))
End Sub

strStartPath,strFilter和strCaption变量可以根据需要排除或自定义。

The strStartPath, strFilter, and strCaption variables can be excluded or customized as needed.

FileName.value将包含文件的路径,并且不会被锁定。

FileName.value will contain the path to the file and it will not be locked.

编辑:
这是整个HTA,不包括删除文件的代码(我已经使用删除代码对其进行了测试):


Here's the entire HTA, excluding code to delete the file (I have tested this with the delete code):

<html>
<HEAD>
<HTA:APPLICATION
  APPLICATIONNAME="Select File"
  ID="SelectFileApplication"
  VERSION="1.0"/>
<SCRIPT Language="VBScript">

Sub SelectFile
    FileName.value = ""
    strStartPath = "C:\Test"
    strFilter = "Text (*.txt;*.csv)| *.txt;*.csv|VBScript (*.vbs;*.vbc)|*.vbs;*.vbc|HTML (*.htm;*.html;*.hta)|*.htm;*.html;*.hta|All Files (*.*)|*.*|"
    strCaption = "Select a File"
    FileName.value = Dlg.openfiledlg(CStr(strStartPath), , CStr(strFilter), CStr(strCaption))
    'The file at FileName.value can be deleted at this point.
End Sub

</SCRIPT>
</HEAD>
<body id="body">
<input type="text" name="FileName" size="30">
<input type="button" onClick="SelectFile" value="Browse...">
<OBJECT id=Dlg classid="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B" width=0 height=0>
</body>
</html>