Setting a File's Read Only Bit using Windows Scripting
02/04/07
Setting a File's Read Only Bit using Windows Scripting
I was looking at the process of setting file attributes using the attrib command in a batch file and I thought it would be much better if I could do this using a wsh script instead.
Here is a quick sample of setting a file's read only attribute using vbs.
'--------------------------------------------------------------------
'-- script to flip file read only bit
'--------------------------------------------------------------------dim args, argCount
dim fileName, roFlag
dim fso, objFile'--------------------------------------------------------------------
'-- get the command line arguments if there are any
'--------------------------------------------------------------------
set args = wscript.arguments
argCount = args.count'--------------------------------------------------------------------
'-- make sure there are enough command line args, otherwise display
'-- the usage information and exit
'--------------------------------------------------------------------
if argCount < 2 then
showUsage
wscript.quit( 1 )
else
fileName = args(0)
roFlag = args(1)
end if'--------------------------------------------------------------------
'-- make sure we have a valid flag
'--------------------------------------------------------------------
if roFlag <> "-mr" and roFlag <> "/mr" and roFlag <> "-rr" and roFlag <> "/rr" then
wscript.echo "Invalid flag: " & roFlag
showUsage
wscript.quit( 1 )
end if'--------------------------------------------------------------------
'-- try to grab the file that we need to process
'--------------------------------------------------------------------
on error resume nextSet fso = createobject("Scripting.FileSystemObject")
Set objFile = fso.getfile( fileName )if err <> 0 then
'houston we have a problem
wscript.echo "Unable to process the request - Error: " & err & " - " & err.description
wscript.quit( 1 )
end if'--------------------------------------------------------------------
'-- try to update the read only flag
'--------------------------------------------------------------------
if roFlag = "-mr" or roFlag = "/mr" then
' try to set attribute
if objFile.attributes = objFile.attributes AND 1 Then
objFile.attributes = objFile.attributes XOR 1
end if
else
' try to unset attribute
if objFile.attributes AND 1 Then
objFile.attributes = objFile.attributes XOR 1
end if
end ifif err <> 0 then
'houston we have a problem
wscript.echo "Unable to process the request - Error: " & err & " - " & err.description
wscript.quit( 1 )
end ifset objFile = nothing
set fso = nothingwscript.quit()
sub showUsage
wscript.echo "Usage: cscript " & wscript.scriptname & " [file name] [flag]" _
& vbcrlf & "Available Flags: " _
& vbcrlf & " -mr or /mr to make the file read only" _
& vbcrlf & " -rr or /rr to make the file writeable"end sub
I hope this post helped you out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.
Pingbacks:
No Pingbacks for this post yet...
Scripts
This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.
Search
Follow Me:
Categories
- All
- Web Technologies (2)
- PHP (1)
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- December 2009 (2)
- March 2009 (2)
- February 2009 (3)
- January 2009 (2)
- December 2008 (4)
- November 2008 (1)
- October 2008 (1)
- February 2008 (1)
- December 2007 (3)
- July 2007 (1)
- April 2007 (1)
- February 2007 (2)
- More...
Misc
Who's Online?
- Guest Users: 1




