Pages

Friday 15 June 2012

Change File Attributes - VBScript


use following script to change attributes of any file or folder.
options are normal, read-only, hidden and system.
any combination of attributes may be set.


Dim FSO, fil, f, attr, i, Arg
Set FSO = CreateObject("Scripting.FileSystemObject")


  '--check for drag-and-drop:
   
     If WScript.arguments.count = 0 Then
           Arg = InputBox("Enter the path of the file or folder you want to change the attributes of.", "Change File Attributes")
     Else
           Arg = WScript.arguments.item(0)
     End If


If Arg = "" Then WScript.quit




  '--check for existence of either file or folder and keep track of which with i :


If FSO.FileExists(Arg) = True Then   
  f = "File" 
  i = 1
ElseIf FSO.FolderExists(Arg) = True Then
  f = "Folder"
  i = 2  
Else
  MsgBox "Path is wrong. No such file or folder.", 16, "Wrong Path"
  WScript.quit
End If


attr = InputBox("Enter the attributes number. Add numbers to get the desired combination of attributes." & VBCrLf & "Normal is 0" & VBCrLf & "ReadOnly is 1" & VBCrLf & "Hidden is 2" & VBCrLf & "System is 4", "Choose Attributes Setting", "0")


'--make sure that input is a number from 0 to 7:


If isNumeric(attr) = False Then
    MsgBox "Wrong entry. Must be a number from 0 to 7.", 16, "Wrong Number"
    WScript.quit
End If
If cInt(attr) > 7 or cInt(attr) < 0 Then
    MsgBox "Wrong entry. Only 0 to 7 are possible.", 16, "Wrong Number"
    WScript.quit
End If


  If i = 1 Then
     Set fil = FSO.GetFile(Arg)
     fil.attributes = attr
     Set fil = Nothing
     MsgBox f & " attributes changed.", 0, "All Done"
  End If
  If i = 2 Then
     Set fil = FSO.GetFolder(Arg)
     fil.attributes = attr
     Set fil = Nothing
    MsgBox f & " attributes changed.", 0, "All Done"
 End If




No comments:

Post a Comment