if (!CreateDirectory(szDirPath, NULL)) { printf("Could not create new directory.\n"); return; }
// Start searching for text files in the current directory.
hSearch = FindFirstFile(TEXT("*.txt"), &FileData); if (hSearch == INVALID_HANDLE_VALUE) { printf("No text files found.\n"); return; }
// Copy each .TXT file to the new directory // and change it to read only, if not already.
while (!fFinished) { StringCchCopy(szNewPath, MAX_PATH, szDirPath); StringCchCat(szNewPath, MAX_PATH, FileData.cFileName); if (CopyFile(FileData.cFileName, szNewPath, FALSE)) { dwAttrs = GetFileAttributes(FileData.cFileName); if (dwAttrs==INVALID_FILE_ATTRIBUTES) return; if (!(dwAttrs & FILE_ATTRIBUTE_READONLY)) { SetFileAttributes(szNewPath, dwAttrs | FILE_ATTRIBUTE_READONLY); } } else { printf("Could not copy file.\n"); return; }
if (!FindNextFile(hSearch, &FileData)) { if (GetLastError() == ERROR_NO_MORE_FILES) { printf("Copied all text files.\n"); fFinished = TRUE; } else { printf("Could not find next file.\n"); return; } } }
#include <stdio.h>
#include <strsafe.h>
void _tmain(int argc, _TCHAR* argv[])
{
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
TCHAR szDirPath[] = TEXT("c:\\TextRO\\");
TCHAR szNewPath[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
printf("Could not create new directory.\n");
return;
}
// Start searching for text files in the current directory.
hSearch = FindFirstFile(TEXT("*.txt"), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No text files found.\n");
return;
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
StringCchCopy(szNewPath, MAX_PATH, szDirPath);
StringCchCat(szNewPath, MAX_PATH, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (dwAttrs==INVALID_FILE_ATTRIBUTES) return; if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Could not copy file.\n");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
printf("Copied all text files.\n");
fFinished = TRUE;
}
else
{
printf("Could not find next file.\n");
return;
}
}
}
// Close the search handle.
FindClose(hSearch);
}
重命名的权限??//够呛,估计要用IFS驱动,或用FindFirstChangeNotification得到通知后,再改回原名
#include <aclapi.h>
#include <lmerr.h>#include <stdio.h>#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13void
DisplayLastError(
LPSTR szAPI
);int
__cdecl
main(
int argc,
char *argv[]
)
{
LPTSTR FileName;
LPTSTR TrusteeName; DWORD AccessMask = GENERIC_ALL;
DWORD InheritFlag = NO_INHERITANCE;
ACCESS_MODE option;
EXPLICIT_ACCESS explicitaccess; PACL ExistingDacl;
PACL NewAcl = NULL;
PSECURITY_DESCRIPTOR psd = NULL; DWORD dwError;
BOOL bSuccess = FALSE; // assume failure if(argc < 4) {
printf("Usage: %s <filename> {/Deny | /Grant | /Revoke | /Set} [<trustee>] [<permissions>] [<InheritFlag>]\n", argv[0]);
return RTN_USAGE;
} FileName = argv[1];
TrusteeName = argv[3]; if ( (0 == stricmp(argv[2], "/Deny") ) ||
(0 == stricmp(argv[2], "/D") ) )
{
option = DENY_ACCESS;
} else if ( ( (0 == stricmp(argv[2], "/Revoke") ) ||
(0 == stricmp(argv[2], "/R") ) ) )
{
option = REVOKE_ACCESS;
} else if ( (0 == stricmp(argv[2], "/Set") ) ||
(0 == stricmp(argv[2], "/S") ) )
{
option = SET_ACCESS;
} else if ( (0 == stricmp(argv[2], "/Grant") ) ||
(0 == stricmp(argv[2], "/G") ) )
{
option = GRANT_ACCESS;
} else {
printf("Invalid action specified\n");
return RTN_ERROR;
} if (argc > 4)
{
AccessMask = atol( argv[4] );
} if (argc > 5)
{
InheritFlag = atol( argv[5] );
} //
// get current Dacl on specified file
// dwError = GetNamedSecurityInfo(
FileName,
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
NULL,
NULL,
&ExistingDacl,
NULL,
&psd
); if(dwError != ERROR_SUCCESS) {
DisplayLastError("GetNamedSecurityInfo");
return RTN_ERROR;
} BuildExplicitAccessWithName(
&explicitaccess,
TrusteeName,
AccessMask,
option,
InheritFlag
); //
// add specified access to the object
// dwError = SetEntriesInAcl(
1,
&explicitaccess,
ExistingDacl,
&NewAcl
); if(dwError != ERROR_SUCCESS) {
DisplayLastError("SetEntriesInAcl");
goto cleanup;
} //
// apply new security to file
// dwError = SetNamedSecurityInfo(
FileName,
SE_FILE_OBJECT, // object type
DACL_SECURITY_INFORMATION,
NULL,
NULL,
NewAcl,
NULL
); if(dwError != ERROR_SUCCESS) {
DisplayLastError("SetNamedSecurityInfo");
goto cleanup;
} bSuccess = TRUE; // indicate successcleanup: if( NewAcl != NULL ) AccFree( NewAcl );
if( psd != NULL) AccFree( psd );
if(!bSuccess)
return RTN_ERROR; return RTN_OK;
}void
DisplayLastError(
LPSTR szAPI
)
{
HMODULE hModule = NULL; // default to system source
DWORD dwLastError = GetLastError();
LPSTR MessageBuffer;
DWORD dwBufferLength; DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM ; //
// if dwLastError is in the network range, load the message source
// if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
hModule = LoadLibraryEx(
TEXT("netmsg.dll"),
NULL,
LOAD_LIBRARY_AS_DATAFILE
); if(hModule != NULL)
dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
} printf("%s error! (rc=%lu)\n", szAPI, dwLastError); //
// call FormatMessage() to allow for message text to be acquired
// from the system or the supplied module handle
// if(dwBufferLength = FormatMessageA(
dwFormatFlags,
hModule, // module to get message from (NULL == system)
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPSTR) &MessageBuffer,
0,
NULL
))
{
DWORD dwBytesWritten; //
// Output message string on stderr
//
WriteFile(
GetStdHandle(STD_ERROR_HANDLE),
MessageBuffer,
dwBufferLength,
&dwBytesWritten,
NULL
); //
// free the buffer allocated by the system
//
LocalFree(MessageBuffer);
} //
// if we loaded a message source, unload it
//
if(hModule != NULL)
FreeLibrary(hModule);
}