Setting a File's Read Only Bit using Windows Scripting

02/04/07

Permalink 08:04:20 am, by dave Email , 584 words, 258 views   English (US)
Categories: Windows Scripting, Wsh Scripts

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.

[More:]

'--------------------------------------------------------------------
'-- 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 next

Set 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 if

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

set objFile = nothing
set fso = nothing

wscript.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.



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



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:

Misc

Who's Online?

  • Guest Users: 2

powered by b2evolution free blog software