开发者

renaming multiple file

开发者 https://www.devze.com 2023-03-13 17:33 出处:网络
int rename_file() { WIN32_FIND_DATA FindFileData; HANDLE hFind; hFind = FindFirstFile(L\"\\\\Hard Disk\\\\*.*\", &FindFileData);
int rename_file()

{   

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
 hFind = FindFirstFile(L"\\Hard Disk\\*.*", &FindFileData);
 LPTSTR oldfilename;
 LPTSTR newfilename;    
 if (hFind == INVALID_HANDLE_VALUE) 
{
  printf ("FindFirstFile failed (%d)\n", 开发者_Go百科GetLastError());
  return 0;
} 
else 
{
int i=1000;       
    while (FindNextFile(hFind, &FindFileData) != 0) 
  {
   _tprintf (TEXT("The first file found is %s\n"),FindFileData.cFileName);
     oldfilename =FindFileData.cFileName;
     StringCchPrintf(newfilename, 30, TEXT("%s\\newfile_%d.txt"),dirname, i);
     BOOL rs = MoveFile(oldfilename,newfilename);
     i++;
  }

  FindClose(hFind);
  return 1;
}

}

i am unable to rename file ,i am working on wince 6 ,while debugging at StringCchPrintf iam getting exception in coredll.dll can any one help me ....


You have not allocated any buffer for newFileName, so when you use it in the StringCchPrintf it's just an uninitialized pointer.

Try this:

TCHAR newFile[260]; // or whatever length you wish
LPTSTR newfilename = &newFile[0];

Also you should check the return code from MoveFile, and output something sensible on error. Make a habit of doing this for all your function calls that can return an error.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号