例如:
system("attrib -r C://MyFolder//*.* /S /D");这样会有一个DOS窗口弹出,如果不希望有窗口弹出,
可以用:WinExec("attrib -r C://MyFolder//*.* /S /D", SW_HIDE);
但是这样只能改变一个文件的属性,如果要把文件夹下的所有文件都改变(包括文件夹),需要用递归自己写一个函数
如:void ChangeDirectoryAttribute(TCHAR* m_szFdPath)来代替上边的两个命令。
代码如下:
#include "stdafx.h"
#include "windows.h"
void ChangeDirectoryAttribute(TCHAR* m_szFdPath)
{
HANDLE hFile; // Handle to directory
WIN32_FIND_DATA FileInformation; // File information
TCHAR m_szPath[MAX_PATH];
TCHAR m_szFolderInitialPath[MAX_PATH];
TCHAR wildCard[MAX_PATH] = "//*.*";
strcpy(m_szPath,m_szFdPath);
strcpy(m_szFolderInitialPath,m_szFdPath);
strcat(m_szFolderInitialPath,wildCard);
hFile = ::FindFirstFile(m_szFolderInitialPath, &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do{
if(FileInformation.cFileName[0] != '.')
{
strcpy(m_szPath,m_szFdPath);
strcat(m_szPath,"//");
strcat(m_szPath,FileInformation.cFileName);
if(FileInformation.dwFileAttributes &
FILE_ATTRIBUTE_DIRECTORY)
{
//it is a sub directory
::SetFileAttributes(m_szPath,FILE_ATTRIBUTE_NORMAL);
ChangeDirectoryAttribute(m_szPath);
}
else
{
//it is a file
::SetFileAttributes(m_szPath,FILE_ATTRIBUTE_NORMAL);
}
}
}while(::FindNextFile(hFile, &FileInformation) == TRUE);
// Close handle
::FindClose(hFile);
DWORD dwError = ::GetLastError();
if(dwError == ERROR_NO_MORE_FILES)
{
//Attributes successfully changed
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//method 1
//char* str = "attrib -r /"C://Program Files//MyFolder//*.*/"
/D /S";
//system(str);
//WinExec(str,SW_HIDE);
//method 2
TCHAR m_szFolderPath[MAX_PATH] = "C://Program Files//MyFolder";
ChangeDirectoryAttribute(m_szFolderPath);
::SetFileAttributes(m_szFolderPath,FILE_ATTRIBUTE_NORMAL);
return 0;
}