Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.55/5 (4 votes)
12 Oct 2011CPOL 45.6K   1   6
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:
C++
// 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:


C++
// 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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Unneeded utility Pin
Leonid Shikhmatov21-Nov-11 10:00
Leonid Shikhmatov21-Nov-11 10:00 
GeneralRe: Reason for my vote of 1Unneeded utility Pin
firehawk26-Jun-12 8:18
firehawk26-Jun-12 8:18 
Generalwhy not use explorer? Pin
Reto Ravasio13-Oct-11 2:57
Reto Ravasio13-Oct-11 2:57 
why not use explorer?
QuestionAlternative Solutions Pin
Roger6521-Oct-11 5:02
Roger6521-Oct-11 5:02 

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

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