Hey-
Is there a way to have MS save a file in Read Only after it copys it?
Save in Read Only
Moderators: Dorian (MJT support), JRL
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).
Happy scripting.
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%
Hey-
Thanks for the reply. I tryed your code and got an error saying:
"Micosoft VBScript compilation error :1006
Expected ")"
Line 9, Column 23"
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%
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%
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%
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
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