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?
Also, i would like to set all the files in a folder as hidden, is this possible without doing them individually?
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
Also, i would like to set all the files in a folder as hidden, is this possible without doing them individually?
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!
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again - great job as usual!
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").