This script creates a text file with a randomly generated name. I want its attribute to be Hidden System, so I applied it with File.Attributes
.
Anyway, while running this script, text file gets created, but when it comes to applying its attributes, it throws the following error:
Type Mismatch: 'GetFile' (Line: 7)
Here is the code:
Dim FSO, TList, WshShell
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set TList = FSO.GetFolder(WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "/Temp").CreateTextFile(FSO.GetTempName)
FSO.GetFile(TList).Attributes = 22
I can't think what is causing the problem because TList
is set using the Function CreateTextFile
. As I think, the function CreateTextFile
does not return Scripting.File
, it might be TextStream
or something. Am I correct?
What is causing this error here?
The CreateTextFile method returns a TextStream object. The error occurs because GetFile expects a string as an input parameter. A TextStream can't set a file's attributes, so building a string pathname is one way to set a new file's attributes:
Dim FSO, WshShell, TempFolder, TempFilename
Const HiddenAttribute = 2
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
TempFolder = WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "\Temp"
If Not FSO.FolderExists(TempFolder) Then
FSO.CreateFolder(TempFolder)
End If
TempFilename = TempFolder + "\" + FSO.GetTempName
CreateNewFile(TempFilename)
FSO.GetFile(TempFilename).Attributes = HiddenAttribute
Sub CreateNewFile(filename)
Dim TS
Set TS = FSO.CreateTextFile(filename, True)
TS.Close
End Sub