Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with copying a folder in a specific path
QtWS25 Last Chance

Problem with copying a folder in a specific path

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 294 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1
    QDirIterator it(fromDir, QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs ,QDirIterator::Subdirectories);
    
    	QDir dir(fromDir);
    	dir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
    	dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Name);
    
    	qDebug() << "00000000: " << dir.absoluteFilePath(fromDir);
    	qDebug() << "44444444: " << dir.dirName();
    
    	const int absSourcePathLength = dir.absoluteFilePath(fromDir).length();
    
    	while (it.hasNext())
    	{
    		it.next();
    
    		const auto fileInfo = it.fileInfo();
    		if (!fileInfo.isHidden()) 
    		{
    
    			qDebug() << "111111111: " << fileInfo.fileName();
    			qDebug() << "222222222: " << fileInfo.filePath();
    			qDebug() << "333333333: " << fileInfo.absoluteFilePath();
    			
    			const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength);
    			const QString constructedAbsolutePath = toDir + "/" + dir.dirName() + subPathStructure;
    
    			if (fileInfo.isDir())
    			{
    				//Create directory in target folder
    				dir.mkpath(constructedAbsolutePath);
    			}
    			else if (fileInfo.isFile())
    			{
    				//Copy File to target directory
    				//Remove file at target location, if it exists, or QFile::copy will fail
    				QFile::remove(constructedAbsolutePath);
    				QFile::copy(fileInfo.absoluteFilePath(), constructedAbsolutePath);
    			}
    		}
    	}
    
    	if (copyAndRemove)
    		dir.removeRecursively();
    
    	m_lstCopyPath.clear();
    

    Copy paste source code.
    If you designate a specific folder as the copy destination, only the contents within the specific folder are copied. What I wanted was to copy the folder.

    folder
    A
    -> A_1
    -> A_2
    -> a_1.txt
    -> a_2.txt
    B

    For example
    There are two folders, A and B. Folder B is empty and you want to copy folder A into folder B.
    If I run it with the source I made
    A
    -> A_1
    -> A_2
    -> a_1.txt
    -> a_2.txt
    B
    -> A_1
    -> A_2
    -> a_1.txt
    -> a_2.txt

    It shows results like this.
    The result I want is
    A
    -> A_1
    -> A_2
    -> a_1.txt
    -> a_2.txt
    B
    A
    -> A_1
    -> A_2
    -> a_1.txt
    -> a_2.txt

    I want results like this. How can I modify the code to get the result I want?

    jsulmJ 1 Reply Last reply
    0
    • I IknowQT

      @jsulm

      My code is based on the code you linked. That doesn't show the result I want

      It just copies the contents of the folder

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @IknowQT Is really not that difficult to do (I assume that my assumption of what you want to achieve is correct, as you still did not clarify that).
      First: create A inside B and then apply the code from the link using B/A as toDir.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • I IknowQT
        QDirIterator it(fromDir, QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs ,QDirIterator::Subdirectories);
        
        	QDir dir(fromDir);
        	dir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
        	dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Name);
        
        	qDebug() << "00000000: " << dir.absoluteFilePath(fromDir);
        	qDebug() << "44444444: " << dir.dirName();
        
        	const int absSourcePathLength = dir.absoluteFilePath(fromDir).length();
        
        	while (it.hasNext())
        	{
        		it.next();
        
        		const auto fileInfo = it.fileInfo();
        		if (!fileInfo.isHidden()) 
        		{
        
        			qDebug() << "111111111: " << fileInfo.fileName();
        			qDebug() << "222222222: " << fileInfo.filePath();
        			qDebug() << "333333333: " << fileInfo.absoluteFilePath();
        			
        			const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength);
        			const QString constructedAbsolutePath = toDir + "/" + dir.dirName() + subPathStructure;
        
        			if (fileInfo.isDir())
        			{
        				//Create directory in target folder
        				dir.mkpath(constructedAbsolutePath);
        			}
        			else if (fileInfo.isFile())
        			{
        				//Copy File to target directory
        				//Remove file at target location, if it exists, or QFile::copy will fail
        				QFile::remove(constructedAbsolutePath);
        				QFile::copy(fileInfo.absoluteFilePath(), constructedAbsolutePath);
        			}
        		}
        	}
        
        	if (copyAndRemove)
        		dir.removeRecursively();
        
        	m_lstCopyPath.clear();
        

        Copy paste source code.
        If you designate a specific folder as the copy destination, only the contents within the specific folder are copied. What I wanted was to copy the folder.

        folder
        A
        -> A_1
        -> A_2
        -> a_1.txt
        -> a_2.txt
        B

        For example
        There are two folders, A and B. Folder B is empty and you want to copy folder A into folder B.
        If I run it with the source I made
        A
        -> A_1
        -> A_2
        -> a_1.txt
        -> a_2.txt
        B
        -> A_1
        -> A_2
        -> a_1.txt
        -> a_2.txt

        It shows results like this.
        The result I want is
        A
        -> A_1
        -> A_2
        -> a_1.txt
        -> a_2.txt
        B
        A
        -> A_1
        -> A_2
        -> a_1.txt
        -> a_2.txt

        I want results like this. How can I modify the code to get the result I want?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @IknowQT said in Problem with copying a folder in a specific path:

        I want results like this

        Like what?
        Do you want to have A as subfolder in B?
        Maybe https://forum.qt.io/topic/105993/copy-folder-qt-c/5 helps.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply
        2
        • jsulmJ jsulm

          @IknowQT said in Problem with copying a folder in a specific path:

          I want results like this

          Like what?
          Do you want to have A as subfolder in B?
          Maybe https://forum.qt.io/topic/105993/copy-folder-qt-c/5 helps.

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @jsulm

          My code is based on the code you linked. That doesn't show the result I want

          It just copies the contents of the folder

          jsulmJ 1 Reply Last reply
          0
          • I IknowQT

            @jsulm

            My code is based on the code you linked. That doesn't show the result I want

            It just copies the contents of the folder

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @IknowQT Is really not that difficult to do (I assume that my assumption of what you want to achieve is correct, as you still did not clarify that).
            First: create A inside B and then apply the code from the link using B/A as toDir.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved