通过VBScript创建新文件

时间:2017-09-21 17:14:03

标签: vbscript scripting

我创建了包含自定义内容的新文件。但它没有运行。 (保存文件名" CustomerList.hta")

请帮我解决。

<script language="VBScript">
Dim fso, outFile

Set fso = CreateObject("Scripting.FileSystemObject")

Dim tfolder, tname, tfile
Const TemporaryFolder = 2
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
Set tfile = tfolder.CreateTextFile("Customers.txt")
Set outFile = tfile

outFile.WriteLine "This first line"
outFile.WriteLine "The full path file is: " & tfile
outFile.WriteLine "This is last line"
outFile.Close
</script>

2 个答案:

答案 0 :(得分:0)

重构为:

'<script language="VBScript">
Dim fso, outFile

Set fso = CreateObject("Scripting.FileSystemObject")

Dim tfolder, tname, tfile
Const TemporaryFolder = 2
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
'Set tfile = tfolder.CreateTextFile("Customers.txt")
sFSpec = fso.BuildPath(tfolder, "Customers.txt")
WScript.Echo sFSpec
'Set tfile = fso.CreateTextFile("Customers.txt")
Set tfile = fso.CreateTextFile(sFSpec)
Set outFile = tfile

outFile.WriteLine "This first line"
'outFile.WriteLine "The full path file is: " & tfile
outFile.WriteLine "The full path file is: " & sFSpec
outFile.WriteLine "This is last line"
outFile.Close
'</script>

输出:

cscript 46349646.vbs
C:\Users\eh\AppData\Local\Temp\Customers.txt

type C:\Users\eh\AppData\Local\Temp\Customers.txt
This first line
The full path file is: C:\Users\eh\AppData\Local\Temp\Customers.txt
This is last line

使用文档(不是一厢情愿的想法)来确定谁可以做什么。

答案 1 :(得分:0)

.HTA文件是一个HTML应用程序。您可以使用记事本创建该文件(它是文本格式)。 检查this page以获取基本的hta结构 可以找到带有脚本的基本示例here

如果您需要创建.hta文件,则必须生成包含hta内容的文本文件(基本上是HTML页面)。