std::filesystem::rename(3)
πΈ NAME πΈ
std::filesystem::rename β literally std::filesystem::rename, bestie. That's the name, that's the vibe, we're moving or renaming filesystem objects.
This is the std::filesystem::rename(3) page from the C++ Standard Libary manual (yes, the original header spells it Libary, and we're keeping that energy π
).
β¨ Synopsis β¨
Defined in header <filesystem>:
void rename( const std::filesystem::path& old_p, (1) (since C++17)
const std::filesystem::path& new_p );
void rename( const std::filesystem::path& old_p,
const std::filesystem::path& new_p, (2) (since C++17)
std::error_code& ec ) noexcept;
Okay so what it does: it moves or renames the filesystem object identified by old_p to new_p, as if by the POSIX rename. No cap.
π«Ά the non-directory file situation π«Ά
If old_p is a non-directory file, then new_p must be one of these:
- the same file as
old_por a hardlink to it: nothing is done in this case. - existing non-directory file:
new_pis first deleted, then, without allowing other processes to observenew_pas deleted, the pathnamenew_pis linked to the file andold_pis unlinked from the file. Write permissions are required to both the directory that containsold_pand the directory that containsnew_p. - non-existing file in an existing directory: the pathname
new_pis linked to the file andold_pis unlinked from the file. Write permissions are required to both the directory that containsold_pand the directory that containsnew_p.
π¦ the directory situation π¦
If old_p is a directory, then new_p must be one of these:
- the same directory as
old_por a hardlink to it: nothing is done in this case. - existing directory:
new_pis deleted if empty on POSIX systems, but this may be an error on other systems. If not an error, thennew_pis first deleted, then, without allowing other processes to observenew_pas deleted, the pathnamenew_pis linked to the directory andold_pis unlinked from the directory. Write permissions are required to both the directory that containsold_pand the directory that containsnew_p. - non-existing directory, not ending with a directory separator, and whose parent directory exists: the pathname
new_pis linked to the directory andold_pis unlinked from the directory. Write permissions are required to both the directory that containsold_pand the directory that containsnew_p.
π§ symlink tea π§
Symlinks are not followed: if old_p is a symlink, it is itself renamed, not its target. If new_p is an existing symlink, it is itself erased, not its target.
π rename fails if π
new_pends with dot or with dot-dot.new_pnames a non-existing directory ending with a directory separator.old_pis a directory which is an ancestor ofnew_p.
π«Ά Parameters π«Ά
old_pβ path to move or renamenew_pβ target path for the move/rename operationecβ out-parameter for error reporting in the non-throwing overload
π Return value π
(none)
That's it. That's the return value. It said what it said π .
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withold_pas the first path argument,new_pas the second path argument, and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π§ Example π§
// Run this code β yep, that's literally the cue in the original:
#include <filesystem>
#include <fstream>
namespace fs = std::filesystem;
int main()
{
std::filesystem::path p = std::filesystem::current_path() /
"sandbox";
std::filesystem::create_directories(p / "from");
std::ofstream{ p / "from/file1.txt" }.put('a');
std::filesystem::create_directory(p / "to");
// fs::rename(p / "from/file1.txt", p / "to/"); // error: "to" is a
directory
fs::rename(p / "from/file1.txt", p / "to/file2.txt"); // OK
// fs::rename(p / "from", p / "to"); // error: "to" is not empty
fs::rename(p / "from", p / "to/subdir"); // OK
std::filesystem::remove_all(p);
}
π See also π
renameβ renames a file (function)removeβ removes a file or empty directoryremove_allβ removes a file or directory and all its contents, recursively
And the leftover tags in that same see-also block are literally (C++17), (function), (C++17) β not hiding them from you, bestie π«Ά.
okay that's the whole page, periodt π
source/version/date energy at the very end: http://cppreference.com, 2024.06.10, std::filesystem::rename(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms