Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

lock file

suppose i create text /dll file with vbs script. can i lock it so that unauthorised person can not open it . pls provide code.
0
ankur3020
Asked:
ankur3020
  • 13
  • 9
2 Solutions
 
RobSampsonCommented:
The only way you can do so with a text file is to apply NTFS security settings to the file.

For example, if you right-click the file, and go to the security tab, and apply permissions to only the Administrators group, then any user that is not a member of that group will not be able to read it.

Regards,

Rob.
0
 
ankur3020Author Commented:
u means there is no way to do it by coding?
0
 
RobSampsonCommented:
Well, it *can* be done, but it still requires that you modify the NTFS permissions with a script, using a tool like SetACL.exe
http://setacl.sourceforge.net/html/examples.html

where for a specific file, you could run a command something like:

C:\Tools\SetACL.exe -on "C:\my dir\my text file.txt" -ot file -actn ace -ace "n:administrators;p:full"

Regards,

Rob.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ankur3020Author Commented:
i think the code mentioned in link u gave are not for vbs. they are for batch file. m i right?
0
 
RobSampsonCommented:
Yes, that's right.  These commands are run on a command line.

If you wanted to use my example command in a VBS, you'd use the below.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSetACL = "C:\Tools\SetACL.exe"
strFileToSecure = "C:\My Dir\My Text File.txt"
strGroup = "administrators"
 
strSetACL = objFSO.GetFile(strSetACL).ShortPath
strFileToSecure = objFSO.GetFile(strFileToSecure).ShortPath
strCommand = strSetACL & " -on """ & strFileToSecure & """ -ot file -actn ace -ace ""n:" & strGroup & ";p:full"""
objShell.Run strCommand, 1, True

Open in new window

0
 
ankur3020Author Commented:
i was trying ur code. it was giving error of path not found for c:\tools\setacl.exe, do i need to download it from above link u gave before using code.? how to use this
0
 
RobSampsonCommented:
Yes, download setacl.exe CmdLine tool from
http://sourceforge.net/project/showfiles.php?group_id=69165

and then change the path in the code to where-ever you place the SetACL.exe file.

Regards,

Rob.
0
 
ankur3020Author Commented:
i tested ur code it is giving error in last line pls check it.

objShell.Run strCommand, 1, True
0
 
RobSampsonCommented:
Hi, the above code should work as long as these two lines

strSetACL = "C:\Tools\SetACL.exe"
strFileToSecure = "C:\My Dir\My Text File.txt"

point to file paths that already exist.  Adjust the paths so that they point to the location of the SetACL.exe file, and the path of the file whose permissions you want to modify.

If that still doesn't work, please post the description and line number of the error that you get.

Regards,

Rob.
0
 
ankur3020Author Commented:
still getting same error. error attached.
error.jpg
0
 
RobSampsonCommented:
I do not get any error, and the permissions apply successfully....please try this, again, changing the paths to SetACL.exe and the file to secure.

This time you should see a DOS box that shows you the result of the SetACL.exe command.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSetACL = "C:\Temp\Temp\Test Script\SetACL.exe"
strFileToSecure = "C:\Temp\Temp\Test Script\Computers.txt"
strGroup = "Power Users"
 
strSetACL = objFSO.GetFile(strSetACL).ShortPath
strFileToSecure = objFSO.GetFile(strFileToSecure).ShortPath
strCommand = "cmd /k " & strSetACL & " -on """ & strFileToSecure & """ -ot file -actn ace -ace ""n:" & strGroup & ";p:full"""
objShell.Run strCommand, 1, True
MsgBox "Done"

Open in new window

SetACL-1.jpg
0
 
ankur3020Author Commented:
i tried and got following attached message . i tested on vista
0
 
ankur3020Author Commented:
find message received
message.jpg
0
 
ankur3020Author Commented:
the message is saying , exe it not compatible
0
 
RobSampsonCommented:
Do you have 32-bit or 64-bit Vista?  Are you using the 32-bit or 64-bit Exe of SetAcl.exe?
0
 
ankur3020Author Commented:
i am using 32 bit and i do not know about setacl.exe.how can i check setacl.exe. i am using the downloaded file available at you specified location.
0
 
RobSampsonCommented:
When you look at this location:
http://sourceforge.net/project/showfiles.php?group_id=69165&package_id=68032&release_id=597837

there's a
....x64.zip
file, and a
....x86.zip
file.

The x86 is the 32-bit version, so make sure you're using that one.

Is it possible for you to test this on an XP machine?  That's what I tested on....

Regards,

Rob.
0
 
ankur3020Author Commented:
i download as u suggested and run the attached code.i got following error.

setacl finished with error,  setacl error message : the sid for a trustee could not found. pls guide.

in the mean time i try it on xp too.
0
 
ankur3020Author Commented:
code tested as attached.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSetACL = "C:\SetACL.exe"
strFileToSecure = "C:\TEST.vbs"
strGroup = "Power Users"
 
strSetACL = objFSO.GetFile(strSetACL).ShortPath
strFileToSecure = objFSO.GetFile(strFileToSecure).ShortPath
strCommand = "cmd /k " & strSetACL & " -on """ & strFileToSecure & """ -ot file -actn ace -ace ""n:" & strGroup & ";p:full"""
objShell.Run strCommand, 1, True
MsgBox "Done"

Open in new window

0
 
RobSampsonCommented:
Hi, if you're trying to add the "local" (not domain) group, called Power Users, try using the well-known SID instead...
The SIDs for common groups are here:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q243330

and the SID for Power Users is S-1-5-32-547

I have now added the following flag:
strMode = "SID"

which can be "SID" or "NAME", so the scirpt knows whether you are entering a sid or group name.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSetACL = "C:\SetACL.exe"
strFileToSecure = "C:\TEST.vbs"
'strGroup = "Power Users"
strGroup = "S-1-5-32-547"
' Set strMode to "SID" or "NAME" depending on whether you're specifying a
' SID for the account / group name, or the name itself
strMode = "SID"
'strMode = "NAME"
 
strSetACL = objFSO.GetFile(strSetACL).ShortPath
strFileToSecure = objFSO.GetFile(strFileToSecure).ShortPath
If UCase(strMode) = "SID" Then
	strCommand = "cmd /k " & strSetACL & " -on """ & strFileToSecure & """ -ot file -actn ace -ace ""n:" & strGroup & ";p:full;s:y"""
Else
	strCommand = "cmd /k " & strSetACL & " -on """ & strFileToSecure & """ -ot file -actn ace -ace ""n:" & strGroup & ";p:full"""
End If
objShell.Run strCommand, 1, True

Open in new window

0
 
ankur3020Author Commented:
thanks it is working fine.
0
 
ankur3020Author Commented:
thnks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now