Copying Files To Different Directory

Hi, I'm not very familiar with MFC, but I was wondering how I can copy the files from one directory to another directory like how in DOS we do copy *.* c:\temp.
Thanks
Victor
[197 byte] By [epod] at [2007-11-17 0:00:51]
# 1 Re: Copying Files To Different Directory
You can use the API function CopyFile().
Just run inside a loop and take in every run a different file name and use CopyFile.
xen0x at 2007-11-10 6:40:53 >
# 2 Re: Copying Files To Different Directory
hi,
use this code

SHFILEOPSTRUCT FileOp;
FileOp.hwnd = m_hWnd;
FileOp.wFunc = FO_COPY ;
FileOp.pFrom = "D:\\latest\\*.*";
FileOp.pTo = "D:\\Testing";
FileOp.fFlags = FOF_NOCONFIRMATION|FOF_SILENT|FOF_NOERRORUI;
FileOp.fAnyOperationsAborted = false;
FileOp.hNameMappings = NULL;
FileOp.lpszProgressTitle = NULL;
SHFileOperation(&FileOp);

Regards,
soundar
soundar at 2007-11-10 6:41:53 >
# 3 Re: Copying Files To Different Directory
I tried doing this but instead of explicitly defining fileop.pfrom I am trying to get a CString instead so that if I pass in a source and sink directory the function will still work ..

Can't get it to work though .. any ideas ..

btw, at the trace the correct directory is being shown i.e. c:\folder\*.*

CString str = DLLPARENT->GetPath();
str += _T("\\*.*");

TRACE("%s is the path\n",str);

SHFILEOPSTRUCT FileOp;
FileOp.hwnd = m_hWnd;
FileOp.wFunc = FO_COPY ;
FileOp.pFrom = str;
FileOp.pTo = "W:\\";
FileOp.fFlags = NULL
FileOp.fAnyOperationsAborted = false;
FileOp.hNameMappings = NULL;
FileOp.lpszProgressTitle = NULL;
SHFileOperation(&FileOp);
Tommy_D at 2007-11-10 6:43:02 >
# 4 Re: Copying Files To Different Directory
Worked like a charm .. I never consider the obvious :-)

Many Thanks ..
Tommy_D at 2007-11-10 6:43:57 >
# 5 Re: Copying Files To Different Directory
Try this I don't claim that its wonderful but it works:

BOOL CMyObject::MoveFileFromSourceToDestination(CString strFileToMove, CString strSourceDir,CString strDestinationDir)
{
BOOL bResult = FALSE;
CFileFind filefindTemp;

CString strFile = strSourceDir;
strFile +="\\";
strFile += strFileToMove;

CString strTo = strDestinationDir;
strTo +="\\";
strTo += strFilename;
_mkdir(m_strLocalStorageDirPath);//ensure destination dir exists

if (filefindTemp.FindFile(strFile))
{
filefindTemp.FindNextFile();
if ((!filefindTemp.IsDirectory())&&(!filefindTemp.IsDots()))//ensure file found isn't directory
{

bResult = MoveFileEx((LPCTSTR)strFile,
(LPCTSTR)strTo,
MOVEFILE_COPY_ALLOWED|MOVEFILE_WRITE_THROUGH|MOVEFILE_REPLACE_EXISTING);
}
}
filefindTemp.Close();
return bResult;
}

In this instance it's moving a file, but you sould easily replace the MoveFileEx call with CopyFileEx. Note that I build up the full path name of the file based on the input directory paths. I also check that the file exists and isn't a directory before moving it.
Hope this helps.
Chris BD at 2007-11-10 6:45:05 >