Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I Turn Off the Read-Only Attribute? 1

Status
Not open for further replies.

barrytraver

Programmer
Nov 28, 2000
40
0
0
US
"How can I turn off the read-only attribute?" is actually the shortened version of my question.

The longer version is "How can I turn off the read-only attribute of all of the files in a user-selected directory _and_ in all of its subdirectories?"

Letting the user select a directory and turning off the read-only attribute of a particular file are the easy parts. The hard part is turning off the read-only attribute not only of all of the files in the user-selected directory but also of all of the files in its subdirectories. Is there any relatively straightforward way to do this?

I've tried to do it using repeated calls to Dir, but that gets rather confusing (in my case, impossibly confusing?) when I try to handle the subdirectories (not knowing in advance how many there are or how many levels of subdirectories there are). I further suspect that using recursiion may be the only way to go in this situation, but that's not an aspect of programming with which I've had much experience.

Here's the background for this question, if you're curious. If entire directories (and their subdirectories) are copied from a CD-ROM to a hard drive, all of the files are set as read-only. What I want to be able to do is in a VB program to turn off the file read-only attributes.

Thanks in advance for any suggestions.

Barry Traver

P.S. My guess is that a solution to this problem could be tremendously useful in many other situations (e.g., producing a catalog of a directory and all of its subdirectories).
 
Yes, attrib -r *.* /s _should_ work -- and it does work with earlier versions of Windows -- but it doesn't seem to work with Windows ME (in spite of what is claimed when you do attrib /?, which does indicate that /s is a legitimate switch).

Anyone else have any thoughts on this?

Barry
 
Eric,

I'm not sure I follow. I've done that, using a MS-DOS Window in Windows ME -- and, yes, it does say that "/S Processes all files in all directories in the specified path." And, yes, that switch does work fine in earlier versions of Windows (such as Windows 95).

Here's the problem: although I don't get an error message in Windows ME, the /S switch is apparently ignored, because the read-only attribute in subdirectories is NOT turned off (although it IS turned off in the main directory in the path). What am I missing in what you said? Or is my computer somehow processing the command in a different way from other computers running Windows ME?

Thanks for the post.

Barry
 
you most go to the root dir :

example : the root dir is on C:\test and you have sub dirs as c:\test\test1 etc...

no with the dos promt go to to c:\test

(normal on the dos prompt you see c:\Windows ,tray cd.. and you see c:\ then

cd test (execute the attrib)

now what is now the problem,i have Windows Me and no problems


Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Eric,

Thanks for the additional comments. Here is what was happening for me (and it may or may not have been, say, caused by gremlins, a temporary memory glitch, or whatever):

Using your example, if I go to c:\test and then tell DOS to attrib -r /s, what has happened is that the result is that the read-only attribute was turned off for the files in c:\test BUT NOT for the files in c:\test\test1. In other words, the /s switch was apparently ignored.

I need to test it out again, because -- even though the same problem showed up for several attempts in succession -- I'm not sure whether I rebooted the computer between the attempts (e.g., to correct a temporary memory glitch).

I think your directions are clear. It's just that my computer isn't behaving the way that it should, according to Microsoft's own documentation, and I'm attempting to determine why.

I thought that the problem might have been Windows ME (I can supply other examples of things that used to work with Windows 95/98 and don't work with Windows ME), but if you have Windows ME and it works all right for you, then it would seem that Windows ME is NOT the problem.

Have any other people had trouble getting the /s switch to work properly? (If not, post a note as well: that will confirm Eric's experience and will provide further evidence that the problem I've had is unique to my computer.)

Regards,

Barry
 
Barry,

I ran attrib in a DOS window, and it seemed to work properly in subdirectories. I thought it might matter what order the command line parameters were in, so I tried it with /s first, last, with "*.*" specified, etc. and it worked every time.

The only thing I can think of is that your file in the subdirectory may not match the filespec you're entering, if any.
Rick Sprague
 
Recursion is not that difficult to implement, if you use the FileSystemObject to do the job.

You must add the FileSystemObject to your project through
the Project Meny -> References -> Scripting Runtime


Const c_strFolderName As String = "D:\MainFolder"

Public Sub Main()

Dim objFso As FileSystemObject
Dim objFolder As Folder

Set objFso = New FileSystemObject
Set objFolder = objFso.GetFolder(c_strFolderName)

ScanFolders objFolder

Set objFolder = Nothing
Set objFso = Nothing

End Sub

Private Sub ScanFolders(objFolder As Folder)

Dim objSubFolder As Folder
Dim objFile As File


'Folder may contain subfolders too

For Each objSubFolder In objFolder.SubFolders


'Scan for SubFolders in this (Sub-)Folder

ScanFolders objSubFolder

Next objSubFolder


'Scan for Files in this folder. If any of them is readonly
' turn it of to normal and keep other attributes

For Each objFile In objFolder.Files

Select Case (objFile.Attributes And ReadOnly) = ReadOnly
Case True
objFile.Attributes = objFile.Attributes And (Not ReadOnly)
Case False
End Select

Next objFile

End Sub

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top