Deleting all files/folders within a directory C++11

May 5, 2018 at 7:50pm
Hello,

First post so I apologize if things aren't formatted properly...

Here is the error I am receiving:

Exception thrown at 0x73C0E476 (KernelBase.dll) in updateSchool.exe: 0xC0000005: Access violation writing location

Here is my code:

#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include <windows.h>
#include <iostream>

int main(void)
{

bool sentinel;
LPCWSTR E = L"E:\\Documents\\1 - School";
//LPCWSTR C = L"C:\\Users\notza\\Documents\\1 - School";

LPWIN32_FIND_DATAW w32fd = 0;
HANDLE hFind;

hFind = FindFirstFile(E, w32fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{

DeleteFile(w32fd->cFileName);

} while (FindNextFile(hFind, w32fd));
FindClose(hFind);
}

std::cout << GetLastError() << std::endl;
system("pause");
return 0;
}

As mentioned in the title I would simply like to delete all files within the specified directory.

My ultimate goal is to copy a folder from my C:// drive to an external storage device (E://).

I am using compiler Visual Studio 2017

Any help will be greatly appreciated!
Last edited on May 5, 2018 at 7:51pm
May 5, 2018 at 8:27pm
#include <filesystem> to get the function remove_all; https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-functions#remove_all

1
2
path pathToDelete(L"E:\\Documents\\1 - School");  
remove_all(pathToDelete);



Last edited on May 5, 2018 at 8:27pm
May 6, 2018 at 4:11pm
Here is the error I am receiving:
Exception thrown at 0x73C0E476 (KernelBase.dll) in updateSchool.exe: 0xC0000005: Access violation writing location


LPWIN32_FIND_DATAW w32fd = 0;

You can't pass a null pointer to the FindFirstFile function
May 8, 2018 at 11:23am
You need append "*.*" to the search passed to FindFirstFile.

You need to pass the address of a WIN32_FIND_DATA instance. You can't pass NULL. How is the function going to return its traversal state if you pass NULL?
May 9, 2018 at 8:31am
Was there some problem with simply using the <filesystem> functions? They exist for a reason.
Topic archived. No new replies allowed.