Wednesday, March 4, 2009

Directory Copy code

This is a directory copy code . It copies all the files or subdirectories under the destination directory to the target directory:

Signature : void copyDir(QString, QString)
Argument : 1. Path of the source directory
2. Path of the destination directory (if the destination does not exist, it is created automatically)

Return Value: void

void copyDir(QString src, QString dest)
{
//Check whether the dir directory exists
if(QDir(src).exists()){
if(!QDir(dest).exists()){
QDir().mkpath(dest);
}

//Construct an iterator to get the entries in the directory
QDirIterator dirIterator(src);

while (dirIterator.hasNext()) {
QString item= dirIterator.next(), fileName= dirIterator.fileName();
QFileInfo fileInfo= dirIterator.fileInfo();

if(fileName!="." && fileName!=".."){

//If entry is a file copy it
if(fileInfo.isFile()){
QFile::copy(item, dest+"/"+ fileName);
}
//If entry is a directory, call the deltree function over it again to traverse it
else
copy(item, dest+ "/"+ fileName);
}
}
}else return;

}

No comments:

Post a Comment