79473

rename() only works for directory that program is running in?

Question:

I am trying to rename a bunch of files in a user specified directory, but it only seems to be working when the user specifies the directory that the program is running from. For example, when running from the command line:

./a.out . "NewName.txt" will work, while

./a.out .. "NewName.txt" will not work. Is there a reason for this? It's on Linux, by the way.

int main(int argc, char** argv){ char* dirpath = argv[1]; char* newName = argv[2]; DIR *d; struct dirent *dir; d = opendir(dirpath); if (d){ while ((dir = readdir(d)) != NULL){ char* filename = dir->d_name; if (rename(filename,newName) == 0){ printf("Renaming %s -> %s\n",filename,newName); } else { printf("Could not rename %s\n",filename); } } } closedir(d); }

I have also tried (while running the program from outside of Desktop):

if (rename("~/Desktop/test.txt","~/Desktop/test2.txt") == 0){ printf("Renaming %s -> %s\n",filename,newName); } else { printf("Could not rename %s\n",filename); }

and it still fails.

Answer1:

While readdir() is reading file names from the other directory, your program's current directory is still in a different location. Unless you prefix the source file name with the path to the directory (and the destination file name too) you're trying to rename non-existent files in the current directory, in general.

In pseudo-code:

dir = opendir(remote_directory) foreach name from dir rename "remote_directory/name" to "remote_directory/othername" end for

Note that the pseudo-code works if 'remote_directory' happens to be ., the current directory; you don't need to special-case that code.

Answer2:

I believe that your main problem is that the result from readdir is just the filename. It doesn't include the directory. You need to paste the directory name and the filename from dir->d_name together in your program.

Answer3:

From the documentation:

<blockquote>

The old argument points to the pathname of the file to be renamed. The new argument points to the new pathname of the file. <strong>If the new argument does not resolve to an existing directory entry for a file of type directory and the new argument contains at least one non-<slash> character and ends with one or more trailing <slash> characters after all symbolic links have been processed, rename() shall fail</strong>

</blockquote>

Looks like you're not referring to an existing element when you use any path other than '.', which is likely why it's failing.

Check the specific errno value to see why.

Recommend

  • Copy/clone an Excel shape with EPPlus?
  • Google Cloud Storage folders list with PHP on App Engine
  • C stat() ignoring files
  • Recursively find subdirectories and files
  • how to write to new files sequentially
  • Laravel 5.3 store and read file directories
  • Finding name of executable sending packet in a netfilter hook
  • how to convert filenames with special characters to valid filenames?
  • PHP Recursively File Folder Scan Sorted by Modification Date
  • Linux “ls -al” like program in C
  • Renaming duplicates in Javascript Array
  • jQuery global regex for replace with variable
  • Directory listing with wildcards in C
  • SessionHandler::gc(): ps_files_cleanup_dir | Permission denied (13)
  • Zipping the contents of a folders in PHP
  • ant copy copying order
  • Found com.google.android.gms:play-services-places:9.2.1, but version 9.0.2 is needed for the google-
  • Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=168821248)(ERR=12505)(ERROR_STACK=(ERROR=
  • NetBeans doesn't see style.css [duplicate]
  • Complex trait requirements on struct
  • integrate POCO library in android ndk
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • Simple linked list-C
  • List images(01.png) and descriptions(01.txt) from directory
  • how to upload multiple files in c# windows application
  • Copy to all folders batch file?
  • How to test if a URL from an Eclipse bundle is a directory?
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?