Save in Read Only

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
Jmac2501
Pro Scripter
Posts: 76
Joined: Tue Nov 15, 2005 8:11 pm

Save in Read Only

Post by Jmac2501 » Mon Mar 12, 2007 5:19 am

Hey-

Is there a way to have MS save a file in Read Only after it copys it?

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Mon Mar 12, 2007 7:04 am

Certainly. One of many ways to do this is to use VBScript. You could also execute command.com or cmd.com with ATTRIB +R, or create and run a .bat file.

You could even code a script to launch windows explorer, browse to the appropriate path, select the file, hit alt+enter to bring up the properties window, click on the readonly checkbox, hit apply/ok.

I like the vbscript method because it is self contained (it does not launch or depend on any external applications)

Here I have created a sample function called "SetReadOnly". It requires one parameter which is the full path+filename.
With the VBRun command, you can execute the function and give it the filename;
VBRUN>SetReadOnly,c:\fish\test.jpg

It could easilly be improved to;
- Let you specify if you want the readonly flag on or off, and pass 0 or 1, or True/False as a second parameter.
- Return a result of success or failure (with an error, such as file did not exist, etc).

Code: Select all

VBSTART

Function SetReadOnly (fullfilename)
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")

  Rem ' Check if the file exists.
  If (fso.FileExists(fullfilename)) Then

	Rem ' The file exists. Set "f" as an object... our file!
    Set f = fso.GetFile(fullfilename)

    Rem ' If the file attribute does NOT contain the read only attribute, set it so
	Rem ' Normal=0, ReadOnly=1, Hidden=2, System=4 (and a few more)
    If (not (f.attributes and 1)) Then
	  f.attributes = f.attributes + 1
	End If

  End If
End Function

VBEND

Let>MyFile=c:\test\filename.txt
VBRUN>SetReadOnly,%MyFile%
Happy scripting.

Jmac2501
Pro Scripter
Posts: 76
Joined: Tue Nov 15, 2005 8:11 pm

Post by Jmac2501 » Mon Mar 12, 2007 11:43 am

Hey-
Thanks for the reply. I tryed your code and got an error saying:

"Micosoft VBScript compilation error :1006
Expected ")"
Line 9, Column 23"

Code: Select all

VBSTART

Function SetReadOnly (M:\Passdown\FEnight\FEN Scripts\Peform1.xls)
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")

  Rem ' Check if the file exists.
  If (fso.FileExists(M:\Passdown\FEnight\FEN Scripts\Peform1.xls)) Then

	Rem ' The file exists. Set "f" as an object... our file!
    Set f = fso.GetFile(M:\Passdown\FEnight\FEN Scripts\Peform1.xls)

    Rem ' If the file attribute does NOT contain the read only attribute, set it so
	Rem ' Normal=0, ReadOnly=1, Hidden=2, System=4 (and a few more)
    If (not (f.attributes and 1)) Then
	  f.attributes = f.attributes + 1
	End If

  End If
End Function

VBEND

Let>filename=M:\Passdown\FEnight\FEN Scripts\Peform1.xls
VBRUN>SetReadOnly,%filename%

Jmac2501
Pro Scripter
Posts: 76
Joined: Tue Nov 15, 2005 8:11 pm

Post by Jmac2501 » Mon Mar 12, 2007 1:05 pm

Nm I found out what i did wrong. this is my correction.
i just added fullfilename = filelocation and it works.





[/code]

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Thu Mar 15, 2007 4:33 pm

Your edited code did not post. Would you try again?

Jmac2501
Pro Scripter
Posts: 76
Joined: Tue Nov 15, 2005 8:11 pm

Post by Jmac2501 » Mon Mar 19, 2007 9:08 am

Sorry.


VBSTART

fullfilename = c:\test\filename.txt

Function SetReadOnly (fullfilename)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")

Rem ' Check if the file exists.
If (fso.FileExists(fullfilename)) Then

Rem ' The file exists. Set "f" as an object... our file!
Set f = fso.GetFile(fullfilename)

Rem ' If the file attribute does NOT contain the read only attribute, set it so
Rem ' Normal=0, ReadOnly=1, Hidden=2, System=4 (and a few more)
If (not (f.attributes and 1)) Then
f.attributes = f.attributes + 1
End If

End If
End Function

VBEND

Let>MyFile=c:\test\filename.txt
VBRUN>SetReadOnly,%MyFile%

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Mon Mar 19, 2007 6:58 pm

You don't need this line line your vbscript section;
fullfilename = c:\test\filename.txt

The filename is given as the first parameter in this line;
VBRUN>SetReadOnly,c:\blah.txt

And then the first parameter is put in to the first variable of the function declaration;
Function SetReadOnly (fullfilename)

(See the helpfile on "VBRun" for some nice examples showing this.)

I could do this;
VBRUN>SetReadOnly,c:\file.txt

or

Let>sFilename=c:\otherfile.txt
VBRun>SetReadOnly,%sFilename%

The advantage of "giving" the filename to the funtion each time, is that you can give the same function a different filename later on to the same function. If I wanted to set read only to multiple files, I could do something like;

VBRun>SetReadOnly,C:\filename1.txt
VBRun>SetReadOnly,C:\filename2.txt

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts