2

i currently have a vbscript that creates a txt file in a directory and opens it, but id like to make it so that the file is hidden, currtly i have this code:

Set objFSO=CreateObject("Scripting.FileSystemObject")

outFile="C:\Users\User\Desktop\New map"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test line 1" & vbCrLf
objFile.Write "test line 2" & vbCrLf
objFile.Close

CreateObject("WScript.Shell").Run("""C:\Users\User\Desktop\New map""")
Madmagic
  • 65
  • 6

1 Answers1

2

You can set the attribute like this

Const cHidden = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")

outFile = "C:\Users\User\Desktop\New map"
Set objFile = objFSO.CreateTextFile(outFile, True)
objFile.Write "test line 1" & vbCrLf
objFile.Write "test line 2" & vbCrLf
objFile.Close

Set mapFile = objFSO.GetFile(outFile)
mapFile.Attributes = cHidden

CreateObject("WScript.Shell").Run Chr(34) & outFile & Chr(34)

Quick Reference --> https://www.thevbprogrammer.com/ch06/06-09-fso.htm https://ss64.com/vb/filesystemobject.html

Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41
  • 1
    The arg to .Run needs quotes. – Ekkehard.Horner Dec 05 '18 at 19:25
  • Don't use [magic numbers](https://stackoverflow.com/a/47902/692942), define the values as Named Constants. In this case `Const Hidden = 2` and `Const ReadOnly = 1` and use them like this `mapFile.Atributes = Hidden + ReadOnly`. – user692942 Dec 05 '18 at 22:02
  • @Ekkehard.Horner - I don't know what kind of file OP is creating. If its something like a batch file, the variable will not need quotes. – Pankaj Jaju Dec 06 '18 at 02:36
  • The fact the filename contains a space is enough for it to require double quotes encapsulating it. – user692942 Dec 06 '18 at 09:24