Click here to Skip to main content
Click here to Skip to main content

Tagged as

Reset File Attributes - Set or Reset Read Only file attribute.

, 12 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
tip

Introduction

I recently downloaded a project of 23 files which all had the readonly attribute set. Not wanting to change all 23 files manually I came up with this simple utility.

Background

CFileFind has a few members to test for IsDirectory(), IsDots() etc. I needed to Set or Reset the file attribute which can only be done using System::IO

Using the Code

The project has to be compiled with Common Language Runtime Support (/clr option). /clr can't be used with /MD or /MT, they have to be cleared from the project properties page.

The default starting directory is C:\Temp copy your files there. Or enter a new starting directory.

The following code shows the System usage:

#using &ltmscorlib.dll&gt
using namespace System;
using namespace System::IO;
using namespace System::Text;
The following code shows the routine that recursively counts the files:
// Recursively count the total number of files and those with ReadOnly Set.
void CurrentAttribute(String^ Path)
{
	CFileFind finder;
	String^ filename;
	String^ fullpath;
	CString szSearch(Path);	// convert String^ to CString
    if (szSearch.Right(1) != _T ("\\"))
        szSearch += _T ("\\");
    szSearch += _T ("*.*");
	// The search engine
	BOOL bWorking = finder.FindFile(szSearch);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		Sleep(1);	// give it some time
		if(finder.IsDirectory())
		{
			filename = gcnew String(finder.GetFilePath());	// convert CString to String^
			if(!finder.IsDots())
				CurrentAttribute(filename);
		}
		fullpath = gcnew String(finder.GetFilePath());	// convert CString to String^
		if((!finder.IsDots()) && (!finder.IsDirectory()))
		{
			// Test Read Only
			if((File::GetAttributes(fullpath) & FileAttributes::ReadOnly) == FileAttributes::ReadOnly)
			{
				num++;
			}
		total++;
		}
	}
}

The following code shows how the ReadOnly Attribute is Set or Reset:

// Recursively scans the path
void Recursive(String^ path, bool bSetAttr)
{
	CFileFind finder;
	String^ fullpath;
	String^ filename;
	CString szSearch(path);	// convert String^ to CString
    if (szSearch.Right(1) != _T ("\\"))
        szSearch += _T ("\\");
    szSearch += _T ("*.*");
	// The search engine
	BOOL bWorking = finder.FindFile(szSearch);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		Sleep(2);	// give it some time
		if(finder.IsDirectory())
		{
			filename = gcnew String(finder.GetFilePath());	// convert CString to String^
			if(!finder.IsDots())
				Recursive(filename, bSetAttr);
		}
		fullpath = gcnew String(finder.GetFilePath());	// convert CString to String^
		// Include only files
		if((!finder.IsDots()) && (!finder.IsDirectory()))
		{
			if(bSetAttr)
			{
				// Sets Read Only
				if((File::GetAttributes(fullpath) & FileAttributes::ReadOnly) != FileAttributes::ReadOnly)
				{
					File::SetAttributes(fullpath, static_cast<FileAttributes>(File::GetAttributes(fullpath) |
						FileAttributes::ReadOnly));
					Console::WriteLine("The file {0} is now Read Only", fullpath);
				}
			}
			else
			{
				// Resets Read Only
				if((File::GetAttributes(fullpath) & FileAttributes::ReadOnly) == FileAttributes::ReadOnly)
				{
					File::SetAttributes(fullpath, static_cast<FileAttributes>(File::GetAttributes(fullpath) ^
						FileAttributes::ReadOnly));
					Console::WriteLine("The file {0} is no longer Read Only", fullpath);
				}
			}
		}
	}
}

Points of Interest

The code shows how to convert from String^ to CString and how to convert from CString to String^

History

Version 1.0, October 11, 2011.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Roger65

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 1 Unneeded utility PinmemberLeonid Shikhmatov21-Nov-11 11:00 
GeneralRe: Reason for my vote of 1Unneeded utility Pinmemberfirehawk26-Jun-12 9:18 
You do realize this is listed under "Tips/Tricks" and not "Utilities", right? Obviously a little more reading would of helped in your case. Laugh | :laugh:
Generalwhy not use explorer? PinmemberReto Ravasio13-Oct-11 3:57 
QuestionAlternative Solutions PinmemberRoger6521-Oct-11 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 12 Oct 2011
Article Copyright 2011 by Roger65
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid