複製檔案時,當目的(destination)磁碟中不存在來源檔時,
複製的速度就會比較慢,但當目的(destination)磁碟中存在來源檔時,
速度上就會快很多,但是偵測到有相同名稱的檔案時,
不論是直接覆寫,或是殺掉重建,其結果都一樣,
明明就是一樣的檔案,速度上怎會有如此差異呢?
下面是部份的程式碼,請各位大大幫我解惑,感謝^^
struct Status{
String SourcePath;
String DestPath;
String RateUnit;
bool isSecond;
int DulTime;
long CopyRate;
};
int __fastcall TCopyFiles::Copy(Status *stCopy)
{
int iSourceFileHandle;
int iDestFileHandle;
long iFileLength;
long lFileSize = 0;
int iBytesRead;
int iFileCount;
int iProgressPos;
double dbStart;
double dbStop ;
String strStuMsg;
char *pszBuffer = new char[MAX_FILE_DATA];
try
{
//開啟來源檔
dbStart = Now();
iSourceFileHandle = FileOpen(stCopy->SourcePath, fmOpenRead);
if(iSourceFileHandle == -1)
{ //開檔失敗
strStuMsg = "Sourcefile open faild!!\n";
Memo->Lines->Add(strStuMsg);
FileClose(iSourceFileHandle);
delete [] pszBuffer;
return openSourceErr;
}
iFileLength = FileSeek(iSourceFileHandle,0,2);
FileSeek(iSourceFileHandle,0,0);
//計算檔案存取次數
iFileCount = iFileLength / MAX_FILE_DATA;
//判斷目的檔的檔案是否存在
if(FileExists(stCopy->DestPath))
iDestFileHandle = FileOpen(stCopy->DestPath, fmOpenWrite);
else
iDestFileHandle = FileCreate(stCopy->DestPath);
if(iDestFileHandle == -1)
{ //開檔或建檔失敗
strStuMsg = "Create Destination file faild!!";
Memo->Lines->Add(strStuMsg);
FileClose(iSourceFileHandle);
FileClose(iDestFileHandle);
delete [] pszBuffer;
return openDestErr;
}
//複製
for(int i = 0; i < iFileCount ; i )
{
iBytesRead = FileRead(iSourceFileHandle, pszBuffer, MAX_FILE_DATA);
FileWrite(iDestFileHandle, pszBuffer, MAX_FILE_DATA);
Application->ProcessMessages();
lFileSize = iBytesRead;
//計算進度條百分比
if(stCopy->isSecond)
iProgressPos = ( (lFileSize / (float)lSize ) 1) * 100;
else
iProgressPos = ( lFileSize / (float)lSize ) * 100;
Progress->Position = iProgressPos ;
}
delete [] pszBuffer;
//判斷最未筆資料是否存在
iFileLength = iFileLength % MAX_FILE_DATA;
if(iFileLength > 0)
{
pszBuffer = new char[iFileLength 1];
iBytesRead = FileRead(iSourceFileHandle, pszBuffer, iFileLength);
FileWrite(iDestFileHandle, pszBuffer, iFileLength);
lFileSize = iBytesRead;
if(stCopy->isSecond)
iProgressPos = 200;
else
iProgressPos = 100;
Progress->Position = iProgressPos ;
delete [] pszBuffer;
}
FileClose(iSourceFileHandle);
FileClose(iDestFileHandle);
dbStop = Now();
//計算時間差
stCopy->DulTime = (dbStop - dbStart) * 10000000;
if(stCopy->DulTime == 0)
stCopy->DulTime ;
stCopy->CopyRate = (lFileSize / stCopy->DulTime ) * 100;
stCopy->DulTime /= 100;
stCopy->RateUnit = SizeUnit(&stCopy->CopyRate);
stCopy->RateUnit = "/sec";
}
catch(...)
{
return copyErr;
}
return noErr;
}