Jump to content
  • Advertisement
Sign in to follow this  
Kuppikeitto

Deleting the files

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi , This is regarding the deleting the files under specific directory. Following is the code My task is to delete the files under c:\\MyDirectlry_name. ======================================================================== HANDLE hFind = NULL; // file handle WIN32_FIND_DATA FindFileData; int iDeleted; char dirpath[MAX_PATH]; SHFILEOPSTRUCT shFileOp; ZeroMemory(&shFileOp, sizeof(SHFILEOPSTRUCT)); shFileOp.hwnd =0; shFileOp.wFunc = FO_DELETE; shFileOp.fFlags = FOF_SILENT|FOF_NOERRORUI|FOF_NOCONFIRMATION; shFileOp.fAnyOperationsAborted = FALSE; strcpy(dirpath,"c:\\"); strcat(dirpath,"MyDirectory_name"); strcat(dirpath,"\\*"); shFileOp.pFrom = dirpath; // shFileOp.pFrom = "c:\\MyDirectory_name\\*"; iDeleted = SHFileOperation(&shFileOp); if (iDeleted != 0) { printf("Unable to delete the files %d \n",GetLastError()); } FindClose(hFind); // closing file handle ================================================================== When i hard code the path (c:\\MyDirectory_name) to shFileOp.pFrom, its working .Whereas,when i assign the 'dirpath' to shFileOp.pFrom is not working (See Above code). Error being , GetLastError() is '6' . INVALID_HANDLE. Any help is greatly appreciated.As i cannot Hard Code the path.Need to get folder's name dynamically and delete the files under that.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
wsprintf(dirpath, "C:\\%s\\*", "My_Directory");
DeleteFile(dirpath);

Share this post


Link to post
Share on other sites
Hi Thanks for your reply.

But i still get the same error...
I have done the follwoing ..

.............................................
...............................................
..............................................
wsprintf(dirpath, "C:\\%s\\*", "My_Directory");
shFileOp.pFrom = dirpath;

.............................................
...............................................
..............................................

I just need to delete the files under that folder , not to delete that folder .

Thanks in advance.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
pFrom is defined as LPCTSTR, you should cast it as a constant. That's why it's not working.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi i am not sure whether i am correct or not, but it is working in Window NT Environment(build with VC6.0 )the same program.But its not working in Microsoft Windows 2000.

Below is the code.

------------------------------------------
------------------------------------------
------------------------------------------

strcpy(dirpath,"c:\\");
strcat(dirpath,"MyDirectory_name");
strcat(dirpath,"\\*");

shFileOp.pFrom = dirpath;

------------------------------------------
------------------------------------------
------------------------------------------

Anyways thanks for the help.



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,
i am not sure whether i am correct or not, but it is working in Window NT Environment(build with VC6.0 )the same program.But its not working in Microsoft Windows 2000.

Below is the code.

------------------------------------------
------------------------------------------
------------------------------------------

strcpy(dirpath,"c:\\");
strcat(dirpath,"MyDirectory_name");
strcat(dirpath,"\\*");

shFileOp.pFrom = dirpath;

------------------------------------------
------------------------------------------
------------------------------------------

The same executable is working on windows NT but not on windows 2000.Is the OS does matter ??

Anyways thanks for the help.

Share this post


Link to post
Share on other sites
[ source ] [ / source ] (without spaces within the brakets)

PRODUCE THIS!


because
--------
-------
------
is ugly
-------
-----
-----

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi ,
Below is the working code for WinNT not on Win2003.

========================================================================

HANDLE hFind = NULL; // file handle
WIN32_FIND_DATA FindFileData;
int iDeleted;
char dirpath[MAX_PATH];

SHFILEOPSTRUCT shFileOp;
ZeroMemory(&shFileOp, sizeof(SHFILEOPSTRUCT));
shFileOp.hwnd =0;
shFileOp.wFunc = FO_DELETE;
shFileOp.fFlags = FOF_SILENT|FOF_NOERRORUI|FOF_NOCONFIRMATION;

shFileOp.fAnyOperationsAborted = FALSE;

strcpy(dirpath,"c:\\");
strcat(dirpath,"MyDirectory_name");
strcat(dirpath,"\\*");

shFileOp.pFrom = dirpath;

// shFileOp.pFrom = "c:\\MyDirectory_name\\*";

iDeleted = SHFileOperation(&shFileOp);

if (iDeleted != 0)
{
printf("Unable to delete the files %d \n",GetLastError());

}
FindClose(hFind); // closing file handle

==================================================================

Best Regards,
Kuppikeitto.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!