Results 1 to 4 of 4

Thread: copying a file from one place to another by reading a path from .txt file

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question copying a file from one place to another by reading a path from .txt file

    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.
    Last edited by anh5kor; 18th January 2016 at 15:01. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: copying a file from one place to another by reading a path from .txt file

    QFile::copy().

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: copying a file from one place to another by reading a path from .txt file

    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
    Qt Code:
    1. QDir directory("Documents");
    2. QString path = directory.filePath(" ");
    3. QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),path,tr("(*.lst)"));
    4. ui->le_filename->setText(fileName);
    5. QFile file(fileName);
    6.  
    7. if(!file.open(QIODevice::ReadOnly))
    8. {
    9. return;
    10. }
    11. QString line = file.readLine();
    12. QFile::Copy(line ,"D:\Polyspace\Source");//Nothing is getting copy where line = RBA\AUTOSAR\SW\BSW\PF\BSMM\BswM\src\BswM.c
    To copy to clipboard, switch view to plain text mode 
    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

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: copying a file from one place to another by reading a path from .txt file

    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:
    1. Build the path to the source file of the copy, by calling QDir::filePath() for the source QDir.
    2. Build a QFileInfo for the destination file of the copy, using QFileInfo::QFileInfo(const QDir &, const QString &).
    3. Make sure that the destination directory exists: get the destination directory with QFileInfo::dir(), then use QDir::mkpath().
    4. Build a path to the destination file of the copy from the QFileInfo: QFileInfo::filePath().
    5. Copy the file with QFile::copy().

Similar Threads

  1. Replies: 4
    Last Post: 31st July 2013, 18:34
  2. Displaying file name (not the entire file name path)
    By Ferric in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2011, 05:23
  3. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 19:55
  4. Copying a file
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 27th October 2009, 15:15
  5. Replies: 2
    Last Post: 5th September 2007, 23:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.