user778199
user778199

Reputation:

renaming multiple file

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", 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 ....

Upvotes: 0

Views: 259

Answers (1)

Steve Townsend
Steve Townsend

Reputation: 54148

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.

Upvotes: 4

Related Questions