Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello, everybody! For this I have a serious problem, months ago I had written a File Copy non-empty directory to another,including its all children files.but this function is Recursive.anyone has a better way to resolve this function? if only not using recursive!Thank you very much! I will be appreciated,guys!
Posted
Updated 20-Apr-19 11:40am
Comments
Patrice T 8-Sep-15 2:35am    
Since a directory tree is recursive in nature, it will be difficult to do otherwise.
quguimin 8-Sep-15 3:29am    
Thank you! I have a question,how do you think the inner implementation of function 'SHFileOperation', maybe it is using the recursive way, isn't it?
Richard MacCutchan 8-Sep-15 4:19am    
Yes it uses recursion, which you can disable with the FOF_NORECURSION flag.
Patrice T 8-Sep-15 3:35am    
I don't know this function, I am not a win32 specialist.

1 solution

Maybe this code will work for you:

bool CopyNonEmptyFolder(const std::wstring& source_folder, const std::wstring& target_folder)
{
	std::wstring new_sf = source_folder + L"\\*";
	WCHAR sf[MAX_PATH + 1];
	WCHAR tf[MAX_PATH + 1];

	wcscpy_s(sf, MAX_PATH, new_sf.c_str());
	wcscpy_s(tf, MAX_PATH, target_folder.c_str());

	sf[lstrlenW(sf) + 1] = 0;
	tf[lstrlenW(tf) + 1] = 0;

	SHFILEOPSTRUCTW s = { 0 };
	s.wFunc = FO_COPY;
	s.pTo = tf;
	s.pFrom = sf;
	s.fFlags = FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NO_UI;
	int res = SHFileOperationW(&s);

	return res == 0;
}
 
Share this answer
 
Comments
Dave Kreskowiak 20-Apr-19 18:11pm    
I doubt he's still looking four years later.
Michael Haephrati 20-Apr-19 19:27pm    
I don't work for him. I answer unanswered questions for the benefit of the entire community.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900