How to set folder permissions with VBScript

Discussion in 'Web Design, HTML And CSS' started by molemenacer, Nov 14, 2006.

  1. molemenacer

    molemenacer New Member

    Joined:
    Nov 14, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    I am trying to set folder permissions for "everyone" on a folder and am struggling to find out how to do it in VBScript.

    I have found a few articles but because i am new to this i do not really understand them.

    Any help would be greatly appreciated.

    Thanks in advance
     
  2. molemenacer

    molemenacer New Member

    Joined:
    Nov 14, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have been able to find the code and implement it, however it wipes out all those that have permission already and just leaves the one that i am creating. Does anyone know why?
    Code:
    Function SetPermissions()
    	Dim strHomeFolder, strHome, strUser
    	Dim intRunError, objShell, objFSO
    
    	strHomeFolder = "C:\Test"
    
    	Set objShell = CreateObject("Wscript.Shell")
    	Set objFSO = CreateObject("Scripting.FileSystemObject")
    	If objFSO.FolderExists(strHomeFolder) Then
    		intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
    		& strHomeFolder & " /t /c /g everyone:F ", 2, True)
    		
    		If intRunError <> 0 Then
    			Wscript.Echo "Error assigning permissions for user " _
    			& strUser & " to home folder " & strHomeFolder
    		End If
    	End If
    End Function
     
    Last edited by a moderator: Nov 14, 2006
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Trying using the /E switch insted of /T switch for cacls. Good Luck!
     
  4. molemenacer

    molemenacer New Member

    Joined:
    Nov 14, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thanks pradeep that worked.
     
  5. molemenacer

    molemenacer New Member

    Joined:
    Nov 14, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have come accross another problem, in that i need to be able to execute an sql script from a vbscript, is this possible and how easy is it?
     
  6. RTorres9

    RTorres9 New Member

    Joined:
    Jul 21, 2008
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0
    Hello Guys,

    I too needed a VBscript to set folder permissions on workstations. So thanks for the information.
    However in my code I could never get the variable of strfolder path to get recognized.

    Here is what I'm referring to:
    Option Explicit
    Dim strFolder

    strFolder = "C:\Test\Growing Palms"

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run("%COMSPEC% /k cacls & stfFolder & /E /G WhiteCastle\Sarah:F")
    WScript.Quit

    I just could not get the directory to get recognized!

    I ended up change the script line to the following.
    objShell.Run("%COMSPEC% /k cacls ""c:\test\Growing Palms"" /E /G WhiteCastle\Sarah:F")

    Any Idea as to what I was doing wrong is attempting to use the Variable.
    I know that once your sent the %comspec% comand you are very much in at a dos prompt.
    Perhaps if there was a way to pass over the variable to the shell command envirnment.

    Please advise,
    RTorres9
     
    shabbir likes this.
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Check this line
    Code:
    objShell.Run("%COMSPEC% /k cacls & stfFolder & /E /G WhiteCastle\Sarah:F")
    
    Should be,
    Code:
    objShell.Run("%COMSPEC% /k cacls """ & stfFolder & """ /E /G WhiteCastle\Sarah:F")
    
     
    shabbir likes this.
  8. RTorres9

    RTorres9 New Member

    Joined:
    Jul 21, 2008
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0
    Thanks for the information.

    Looks like the lack of the double double quotes is the problem.

    I'll give this a try.

    Have a nice day!
     
  9. SreejithRajan

    SreejithRajan New Member

    Joined:
    Jan 19, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,I need to give " only write" permission to a folder through VB Script.Please let me know how to achieve this.I used the below VB script(Please let me know what modification needs to be made):Function SetPermissions() Dim strHomeFolder, strHome, strUser Dim intRunError, objShell, objFSO strHomeFolder = "C:\Demo" Set objShell = CreateObject("Wscript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FolderExists(strHomeFolder) Then intRunError = objShell.Run("%COMSPEC% /k cacls """ & strHomeFolder & """ /E /G fareast\Sreejith:F") If intRunError 0 Then Wscript.Echo "Error assigning permissions for user " _ & strUser & " to home folder " & strHomeFolder End If End IfEnd Function
     
  10. SANUJ

    SANUJ New Member

    Joined:
    Jun 9, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    From the Above code i am not able to set permission to a folder inside "C:\Program files" ,
    Please do the nessessary help
     
  11. tairjam

    tairjam New Member

    Joined:
    Apr 25, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How would you use CACLS with Variables for UserName? For instance, I want to elevate permissions for the users of their home directories. The user name I want to be elevated to Full Control is the same name as the folder. How can I script it to use that same foldername (username)?
     
  12. sugavanam

    sugavanam New Member

    Joined:
    Aug 2, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Please explain the below,

    Giving the folder access for individual person from the admin person
    Can you please explain gow to do the same?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice