在 %temp% 中创建一个文本文件,并使用 vbs 将内容写入其中
Create a text file in %temp% and write content in it using vbs
基本上,我想创建一个新文件并将其写入 PC 上的目录中,由 %TEMP%
变量指向。但是,下面修改后的代码不起作用:
Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close
错误信息:
run time error
line no: 3
object required
旧代码
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)
如果我使用文件名 "C:\myfile.txt" 它工作正常。
错误信息:
Path not found
您可以使用 Environ("temp")
写入 C:\Users\[username]\AppData\Local\Temp
在 VBA 中,您可以只使用 Environ("TEMP")
来扩展环境变量 - 如果这在 VBScript 中不起作用,您可能需要绑定 WScript.Shell
对象并使用 ExpandEnvironmentStrings
属性 相反,像这样:
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing
根据下面的评论
这是一个 "fully fixed" 代码:
'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String
'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need
'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need
基本上,我想创建一个新文件并将其写入 PC 上的目录中,由 %TEMP%
变量指向。但是,下面修改后的代码不起作用:
Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close
错误信息:
run time error
line no: 3
object required
旧代码
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)
如果我使用文件名 "C:\myfile.txt" 它工作正常。
错误信息:
Path not found
您可以使用 Environ("temp")
写入 C:\Users\[username]\AppData\Local\Temp
在 VBA 中,您可以只使用 Environ("TEMP")
来扩展环境变量 - 如果这在 VBScript 中不起作用,您可能需要绑定 WScript.Shell
对象并使用 ExpandEnvironmentStrings
属性 相反,像这样:
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing
根据下面的评论
这是一个 "fully fixed" 代码:
'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String
'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need
'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need