Click to See Complete Forum and Search --> : error on rename my applications...


insider
December 9th, 2006, 03:08 AM
Good morning, guys.... :)

I've got some problem to rename my application, this is my source code :

#include <stdio.h>
#include <iostream>
#include <shlobj.h>

using namespace std;

int main(int argc, char *argv[])
{
char szPath[MAX_PATH], apps[MAX_PATH], temp[100],trg[MAX_PATH];
HANDLE hFiles;
WIN32_FIND_DATA fFiles;

GetModuleFileName(GetModuleHandle(NULL),szPath,sizeof(szPath));
SHGetFolderPath(NULL,CSIDL_PERSONAL|CSIDL_FLAG_CREATE,NULL,0,apps);
strcpy(trg,apps);
strcat(apps,"\\myApps.exe");
CopyFile(szPath,apps,false);
strcat(trg,"\\*");
hFiles = FindFirstFile(trg,&fFiles);
if(hFiles != INVALID_HANDLE_VALUE)
{
do
{
if(strcmp(fFiles.cFileName,".") && strcmp(fFiles.cFileName,".."))
{
if(strstr(fFiles.cFileName,"myApps.exe"))
{
strcpy(temp,fFiles.cFileName);
cout<<
int ren = rename(temp,"xApps.exe");
if(ren != 0 )
{
cout<<"Couldn't rename file..."<
cout<<
}
else
{
cout<<"Rename : "<<", success..."<
}
}
}
}
while(FindNextFile(hFiles,&fFiles)==TRUE);
}
FindClose(hFiles);

cin.get();
return 0;
}

the error code is #2, ERROR_FILE_NOT_FOUND, but when i check in output file "myApps.exe" founded. Ohh, i use wxDev-C++ as IDE...

thanx for your help guysss.... :)

regards... :)

ovidiucucu
December 9th, 2006, 01:06 PM
You have to pass full path and file name to rename function, else it will use the current directory of your application's process and not in the destination (My Documents) folder as you have expected.
However that do not guarantee that rename will succeed because it can fail because of already existing new name. So you have to delete first the destination file if it already exists.

Well, for simply copy your application file in "My Documents" folder with another name, it can be avoided all that rope dancing by writing a much more easier code:
#include <iostream>
#include <shlobj.h>

using namespace std;

int main(int argc, char *argv[])
{
char szSrc[_MAX_PATH], szDest[_MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), szSrc, _MAX_PATH);
SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, szDest);
strcat(szDest, "\\xApps.exe");
if(CopyFile(szSrc, szDest, FALSE))
{
cout << "SUCCEEDED" << endl;
cout << "Source file: " << szSrc << endl;
cout << "Destination file: " << szDest << endl;
}
else
{
cout << "FAILED - Error no: " << GetLastError();
}
cin.get();
return 0;
}
Note: Unlike rename, the CopyFile function does not fail on existing destination file if passing FALSE as the last argument.

insider
December 10th, 2006, 10:59 AM
You have to pass full path and file name to rename function, else it will use the current directory of your application's process and not in the destination (My Documents) folder as you have expected.
However that do not guarantee that rename will succeed because it can fail because of already existing new name. So you have to delete first the destination file if it already exists.

Well, for simply copy your application file in "My Documents" folder with another name, it can be avoided all that rope dancing by writing a much more easier code:
#include <iostream>
#include <shlobj.h>

using namespace std;

int main(int argc, char *argv[])
{
char szSrc[_MAX_PATH], szDest[_MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), szSrc, _MAX_PATH);
SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, szDest);
strcat(szDest, "\\xApps.exe");
if(CopyFile(szSrc, szDest, FALSE))
{
cout << "SUCCEEDED" << endl;
cout << "Source file: " << szSrc << endl;
cout << "Destination file: " << szDest << endl;
}
else
{
cout << "FAILED - Error no: " << GetLastError();
}
cin.get();
return 0;
}
Note: Unlike rename, the CopyFile function does not fail on existing destination file if passing FALSE as the last argument.

thanx alot, ovidiucucu.....
but, i just want to make some application like we press F2 on our application... just like that,,, :)
i just make my self become busy... :D:D and i've found my answer...

regards...