gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:25:33 25 4
gpt4 key购买 nike

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

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

<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 不起作用——文件没有被删除;只是推迟到程序退出:

<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 程序退出 - 此时文件最终会自行删除。

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

编辑

我的实际用例!在尝试生成可用的 MVCE 时,我没有意识到会找到一个不符合我的特定要求的解决方案。

我删除文件的原因是我可以用其他东西替换它,所以我需要文件在函数结束前消失。 调用 window.location.reload() 绝对有效,但文件在函数结束时消失。

我实际上想做的是这样的:

<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">

添加一个点击打开文件的按钮

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

添加文件对话框对象

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

添加一个 sub 以获取此对象的返回值并将其放入您的文本框中。

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 变量。

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

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

<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>

关于vbscript - 在 hta 文件中使用文件输入元素可防止删除所选文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37583532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com