Link to home
Start Free TrialLog in
Avatar of tomdenton
tomdenton

asked on

VBScript to set file attributes

Hi

I am trying to create a simple script to set the attributes of a file to hidden. This is what I have so far and it seems to work fine IF the file (TEST.docx) is in the app path  but if in a subfolder as below (ie Support\Documents\) then it can't find the file. The file is there of course!

Any suggestions please?

Set objShell = WScript.CreateObject( "WScript.Shell" )
appPath = objShell.CurrentDirectory &"\"

HideDocuments(appPath & "Support\Documents\TEST.docx") 'testing 123

Sub HideDocuments(filespec)
	Dim fs, f, r 
	Set fs = CreateObject("Scripting.FileSystemObject") 
	Set f = fs.GetFile(fs.GetFileName(filespec)) 
	f.attributes = 2 'hidden
End Sub

Open in new window


Also, i would like to set all the files in a folder as hidden, is this possible without doing them individually?
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Change line 9 to simply:
Set f = fs.GetFile(filespec)

Open in new window


To do all files in a folder, use the .Files collection of a Folder object, obtained by fs.GetFolder("Folder Path").
Avatar of tomdenton
tomdenton

ASKER

Thanks Robert, the first part works great now.

I tried calling the following (filespec is the folder path) to set the whole folder contents as hidden but I get an 'Object doesn't support...' error - I am obviously missing something still sorry!
Sub HideFolderFiles(filespec)
	Dim fs, f, r 
	Set fs = CreateObject("Scripting.FileSystemObject") 
	Set f = fs.GetFolder(filespec).Files
	f.attributes = 2 'hidden
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again - great job as usual!