Example #1
0
void MoveFileDelayAbPath(CSTRING& fileName)
{
	//删不掉的文件更名,延迟删除
	char szTempPath[MAX_PATH], szNum[NUM_LENGTH];
	memset(szTempPath, 0, MAX_PATH);
	memset(szNum, 0, NUM_LENGTH);
	DWORD dwResult=::GetTempPath(MAX_PATH, szTempPath);

	CSTRING TempPath = szTempPath;
	CSTRING temp = fileName.Left(fileName.Find('.'));
	CSTRING postfix = fileName.Right(fileName.GetLength() - fileName.ReverseFind('.'));
	CSTRING tempFile, sysTempFile;

	dwResult=::GetTempPath(MAX_PATH, szTempPath);
	TempPath = szTempPath;
	int n = 0;
	HANDLE hFind;
	WIN32_FIND_DATA ffd; 
	tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
	hFind = FindFirstFile(tempFile, &ffd);
	while(hFind != INVALID_HANDLE_VALUE)
	{
		n++;
		FindClose(hFind);
		tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
		hFind = FindFirstFile(tempFile, &ffd);
	}
	tempFile.Format("%s.%s.tmp", temp.GetBuffer(), itoa(n, szNum, 10));
	int ret = temp.ReverseFind('\\');
	CSTRING temp1 = temp;
	if(ret != -1)
	{
		temp1 = temp.Right(temp.GetLength() - ret -1);
	}
	sysTempFile.Format("%s\\%s.tmp", TempPath.GetBuffer(), temp1.GetBuffer());

	if(rename(temp + postfix, tempFile) == 0)
		MoveFileEx(tempFile, sysTempFile, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING); 
}