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. [SOLVED] How to rename a directory?

[SOLVED] How to rename a directory?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.3k 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.
  • Q Offline
    Q Offline
    Qub1
    wrote on last edited by
    #1

    Hello,

    I'm trying to change the name of a directory and I'm unsure how to do this.
    I've found the QDir::rename function, however it requires an old name? This is really odd, since when you're using the function you've already created an object with the directory in it, so why would it need the old name?
    What I'm trying to achieve is:
    @QDir currentDirectory("C:\SomeDirectory\A");
    currentDirectory.rename("B");
    // Now the directory should be renamed to C:\SomeDirectory\B@

    Could someone provide an example on how to do this?

    Thanks!

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      [quote author="Qub1" date="1411849364"]
      @QDir currentDirectory("C:\SomeDirectory\A");
      currentDirectory.rename("B");
      // Now the directory should be renamed to C:\SomeDirectory\B@
      [/quote]

      This specification for C:\SomeDirectory\A won't work. The compiler will attempt to handle \S and \A as escape sequences, and likely fail. If the compiler being used supports C++ 0x11, raw strings can be used, or \ can be escaped as \.
      @
      // C++ 0x11 raw string
      QDir currentDirectory(R"(C:\SomeDirectory\A)");
      // C string with \ escaped
      QDir currentDirectory("C:\SomeDirectory\A");
      @

      Either way, Qt uses "/" for the directory separator. Use QDir::fromNativeSeparators() and toNativeSeparators() if file paths will be coming in or going out in the native form.

      One solution to the rename problem that spawned the thread is:
      @ QDir currentDirectory(...)
      currentDirectory.rename(currentDirectory.path(), "../B")@

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qub1
        wrote on last edited by
        #3

        Ah, thanks for that! It is working now. And regarding the escape character, sorry, I was just writing an example from on the fly :) - didn't think about that

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qub1
          wrote on last edited by
          #4

          Alright, so I did some more testing and it appears that just creates a new folder with the new name, but none of the files are inside and the old folder with the old name is still there. So all it does is create a new empty folder.

          EDIT: Nevermind, I was doing it wrong :S

          1 Reply Last reply
          0

          • Login

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