PDA

View Full Version : copying a file from one place to another by reading a path from .txt file



anh5kor
18th January 2016, 14:51
I have a requirement,where I have a text file which contain the path of all the .c file which are used in a project.I have to create a GUI which on button click read the file or the .c path and open the location,copy the respective .c file in another folder. Just like a script which copy the .c file from source to destination by reading the path from .txt file.

creating a button and reading a file I don't have any problem.But how can I achieve the opening of source location and copying of file from source to destination.
Please let me know some example or hint.

anda_skoa
18th January 2016, 17:45
QFile::copy().

Cheers,
_

anh5kor
19th January 2016, 08:30
I have file which contain the relative path of all the c file like below


RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_CanSM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_ComM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_DCM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_EthSM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_FrSM.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_Generic.c
RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM_LinSM.c

I am able to open and read the file path but not able to copy


QDir directory("Documents");
QString path = directory.filePath(" ");
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),path,tr("(*.lst)"));
ui->le_filename->setText(fileName);
QFile file(fileName);

if(!file.open(QIODevice::ReadOnly))
{
return;
}
QString line = file.readLine();
QFile::Copy(line ,"D:\Polyspace\Source");//Nothing is getting copy where line = RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM.c

So my code should create a folder and copy all the c file from the relative path of text file to the folder.
I am trying to use QFile::copy("path1/file", "path2/file");but I am not able to create a proper path.
So my problem are like below
1.how can I convert the relative path to Absolute path
2.another problem is that text file is having the "\" slash where as Qt open file with "/"slash.
3.How to create folder using qt code.
Please let me know I achieve above problem

yeye_olive
19th January 2016, 10:57
In your call to QFileDialog::getOpenFileNames() ,the value of the 'dir' argument should be a path to the folder to open/file to select by default. Your code currently passes "Documents/ ", which is unlikely to produce any interesting result. If you want to pass the path to the user's "document" folder, have a look at QStandardPaths.

Then, your code reads the QFile directly. Calling file.readLine() returns a byte array that is silently interpreted into a QString using a default character encoding. An additional problem is that, as implied by the documentation for QIODevice::readLine(), the newline character ('\n') is included in the buffer. This means that you will have to remove any such character afterwards. A much more elegant solution would be to set up a QTextStream on top of the QFile, and specify with which encoding it shall interpret the file. Then, use QTextStream::readLine(), which returns a QString without the trailing newline character.

Before you start copying the files, you should construct two QDir instances, one for the source folder and one for the destination folder. For the source folder, you will need to build a QFileInfo from the path to the text file (your 'fileName' variable), then extract its containing directory as a QDir by calling QFileInfo::dir().

For each QString read in the text file, that represents a path relative to the location of the text file:

Build the path to the source file of the copy, by calling QDir::filePath() for the source QDir.
Build a QFileInfo for the destination file of the copy, using QFileInfo::QFileInfo(const QDir &, const QString &).
Make sure that the destination directory exists: get the destination directory with QFileInfo::dir(), then use QDir::mkpath().
Build a path to the destination file of the copy from the QFileInfo: QFileInfo::filePath().
Copy the file with QFile::copy().