Exemple #1
0
void copyFiles(const QStringList &files, const QDir &fromDir, const QDir &toDir,
			   const QString &intendent = "    ")
{
	if (intendent.size() > 40) {
		out << "Infinite loop detected\n";
		out.flush();
		abort();
	}
	for (const QString &file : files) {
		if (!silent) {
			out << intendent << "Copying " << fromDir.absoluteFilePath(file) << " to "
				<< toDir.absoluteFilePath(file) << "...\n";
		}
		if (file.contains('*')) {
			copyFiles(fromDir.entryList(QStringList() << file,
										QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs),
					  fromDir, toDir, intendent);
		} else {
			if (QFileInfo(fromDir, file).isDir()) {
				QDir dir(toDir);
				if (!dir.exists(file)) {
					if (!dir.mkpath(file)) {
						abort();
					}
				}
				dir.cd(file);
				QDir fDir(fromDir);
				fDir.cd(file);
				copyFiles(fDir.entryList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs), fDir,
						  dir, intendent + "  ");
			}
			QFile::copy(fromDir.absoluteFilePath(file), toDir.absoluteFilePath(file));
		}
	}
}
bool NativeFileSystem::CopyDir(const gd::String & source, const gd::String & destination)
{
    wxString sFrom = source.ToWxString();
    wxString sTo = destination.ToWxString();

    //As seen on https://forums.wxwidgets.org/viewtopic.php?t=2080
    if (sFrom[sFrom.Len() - 1] != '\\' && sFrom[sFrom.Len() - 1] != '/') sFrom += wxFILE_SEP_PATH;
    if (sTo[sTo.Len() - 1] != '\\' && sTo[sTo.Len() - 1] != '/') sTo += wxFILE_SEP_PATH;

    if (!::wxDirExists(sFrom)) {
        return false;
    }
    if (!wxDirExists(sTo)) {
        if (!wxFileName::Mkdir(sTo, 0777, wxPATH_MKDIR_FULL)) {
            return false;
        }
    }

    wxDir fDir(sFrom);
    wxString sNext = wxEmptyString;
    bool bIsFile = fDir.GetFirst(&sNext);
    while (bIsFile) {
        const wxString sFileFrom = sFrom + sNext;
        const wxString sFileTo = sTo + sNext;
        if (::wxDirExists(sFileFrom)) {
            CopyDir(sFileFrom, sFileTo);
        }
        else {
            if (!::wxFileExists(sFileTo)) {
                if (!::wxCopyFile(sFileFrom, sFileTo)) {
                    return false;
                }
            }
        }
        bIsFile = fDir.GetNext(&sNext);
    }
    return true;
}