I searched online to find that how to change the directory name? But I found nothing. All I found was rename() that was changing filenames. I want to change a directory name. Linux uses mv command for renaming a directory but if the directory contains large files then moving will take time more right? I want to do this in C.
On every file system I can think of, renaming a directory doesn't actually involve moving the files to a new location but rather changing a record in the file system.
To move a directory on Windows:
if(!MoveFileW(L"C:\\Path\\OldDirName", L"C:\\Path\\NewDirName"))
{
// Operation failed
}
On *nix:
if(rename("/path/olddirname", "/path/newdirname") != 0)
{
// Operation failed
}
Wrapper macros:
#ifdef __linux__
#define MvDir(old, new) (rename((old), (new)) != 0)
#elif defined(WINVER)
#define MvDir(old, new) !(MoveFileW((L ## old), (L ## new))
#endif
This also automatically wraps the success-test of the expression into a 1/0 value for you.
...how to change the directory name? ...All I found was rename() that was changing filenames.
No, that's incorrect. rename is totally fine for renaming directories, also.
Linux uses mv command for renaming a directory but if the directory contains large files then moving will take time more right?
The mv command is typically implemented using rename, which is very fast. Renaming a directory does not necessarily require moving (or doing anything with) the files in it.
If you're doing a more complicated rename, like trying to rename a/b/c to d/e/f, and if this ends up trying to move things onto a different mounted filesystem, then a simple directory rename won't work, and yes, actually "moving" (actually copying) of files would be required. In that case, the rename() call will fail, so you'll know. (errno should contain EXDEV.)
In the cross-device case, the mv command will do the expensive work of copying files, but only if it has to.
If you use rename from a C program, you'll get a quick rename for renames on the same filesystem, and an error for renames that attempt to cross filesystems, and you'll never get any expensive copies unless you code them up yourself.
Related
i want to move a file from a directory to anther directory with C Coding.
I search and find rename(); function , but when working it doesnt work and have a error:
my code:
#include <stdio.h>
int main() {
if(rename("/root/tmpfile.php", "/home/check-tmp.php"))
perror( NULL );
}
the code well compiled but when running this code showing this error:
Invalid cross-device link
How to move a file from a directory to anther directory without using System for fopen?
Aslo , i finded many codes and ways to do it but doesnt working all codes.
Please say me a way and make sure it will work
Thanks.
Many aspects of the behavior of `rename' are inherently platform-dependent: The rename operation might not be able to move a file from one file system to another , it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
In other words, your system does not support rename files cross different partitions (your root partition and your home partition are different.)
So the solution is when it fails, copy the file to the destination and delete the original.
The rename call can only rename and move files within a single disk partition. The error "cross-device link" indicates that you attempted to move a file from one partition to another. (If you were on a Windows system, you can imagine if you tried to rename a file from C: to D:.)
When you use the Unix mv command to move files, it first tries a rename, but if it fails in this way, it falls back and makes a new copy of the file in the new location, then deletes the original. That's what you would have to have your code do in this situation, too.
(Copying a file is easy enough, but there are plenty of library functions out there that will do it for you, and also take care of things like preserving the last-modified time and other file attributes.)
I'm looking to list and store the contents of a directory in a struct using C on Windows.
I got a problem with stat(), I don't really understand this line
if (statut.st_mode & S_IFDIR)
So I want to understand how it works for checking if it's a directory or a file?
stat() retrieves a block of information describing the specified file. Directories are also files. A directory can be thought of as a file that contains other files.
So, in the file's st_mode, you can see whether the current file is actually a directory by checking for the presence of the S_IFDIR bit.
I'm trying to implement an in-memory file system using fuse. The rename function accepts 'from' and 'to' parameters.
When I tried doing this on bash,
mv file1 file2,
it internally calls rename function (I used the -d option to check it).
When i try to rename the function in the GUI, it again calls the rename function.
But if file2 is an already existing file, mv command overwrites it whereas the GUI prevents me from renaming the file. How do I enforce this constraint because internally both these actions call the rename function with no distinction.
The rename function replaces the target file atomically with removal of the old name. This is the whole point of it, and if it doesn't do that correctly, various things would break badly. For applications that want to prevent renaming over top of another file, they have to use the link function (which will fail if the target exists) first, then unlink the old name if link succeeded.
I'd like to create a unique temporary directory in Windows from some C
code (not C++ or C#). I want to do this so that I can put some temp
files in the directory, and then delete them all easily when I'm done
(by removing the directory recursively).
I'm essentially looking for an equivalent of the linux
mkdtemp
function. There is a C# answer here, and responses on this
question
suggest using Boost. But since I'm using C, those solutions don't work
for me.
The best I've been able to come up with so far is to use
GetTempFileName
followed by CreateDirectory,
but the problem there is that if I ask
GetTempFileName
to create a unique file name, it will also create the file (which I
don't want, since I want to make a directory instead).
Relatedly, there's
GetTempPath,
which returns the location of the user's temp folder from environment
variables - but since I want to create my own directory that I can
safely delete later, I still need to create a directory inside any
path it would return.
It looks like if I want a unique directory to be created, I'll have to
create a temp file, get the name, delete it, and then create a
directory with the same name - which sounds very messy.
Any other ideas?
You can use what GetTempPath returns concatenated with a Guid to ensure uniqueness of the directory. You can create a Guid using UuidCreate or CoCreateGuid Function.
To delete recursively the directory, there is an example here in pure C: How to remove directory recursively? based on FindFirstFile, FindNextFile, DeleteFile and RemoveDirectory.
There is also SHFileOperation but it's more heavyweight and is based on the Windows Shell functions, and the Shell DLLs are not always wanted, especially if you're writing server code.
Use GetTempPath then CreateDirectory with a random name under it, optionally retrying if CreateDirectory fails due to it already existing. But if your name generation is good enough, the likelihood of a collision with an existing name is much smaller than the likelihood of a blackhat guessing your password or even your private key, so you might as well ignore it.
Use _tempnam tmpnam_s to create a filename that doesn't exist yet, and then use CreateDirectory to create the directory. There's technically a race condition if you do this, in that another process could potentially create a file or directory with that name in the time in between when you generate the filename and when you create the directory, but the odds of that are rather unlikely. To protect against that, you can loop until you succeed.
For recursively removing a directory tree, you can use SHFileOperation. Alternatively, you can do the directory traversal yourself with FindFirstFile/FindNextFile, DeleteFile, and RemoveDirectory.
If you want to remove the directory automatically upon exiting, register a function using atexit. This will only work for normal program termination (i.e. via the exit function or via returning from main/WinMain). This will not work for abnormal program termination (e.g. via abort, an access violation, someone else calling TerminateProcess, etc.).
I want to copy an XML file to the output directory (debug/release). I don't want to do it using the resource system because this doesn't allow me to modify the file without recompiling. 5314464 shows how to open the file in a portable way, but I couldn't find a good solution for copying it (obviously I want to automate this somehow). Solutions like 1740534 suggest using the copy command of each operating system but perhaps there is a better solution.
Try using the INSTALLS keyword in your qmake file. (See QMake Reference on INSTALLS)
stuff_to_copy.path = /path/to/put/it/in
stuff_to_copy.files += file1
stuff_to_copy.files += file1
INSTALLS += stuff_to_copy
Having done this, you will need to run "make install" as part of your build process to actually cause the files to be copied.