title: R,Rename.file iteratively
body: <p>I'm trying to write an algorithm in R language which allows me to rename files. I want to rename all pdf files in a folder by numbers 1,2,3,4,5,... and keeping the pdf extension.
  The main difficulty is the following: My folders contains files and folders and these folders contain files and folders etc.... I want that all pdf files in all these subfolders are named by numbers starting to 1 and going up to the number of files in this specific folder (following for example the alphabetic order)! Another difficulty: My folders also contain non-pdf files !</p>
  
  <p>Here is an example:
  (In the following example Folder 1 contains folder2 and 3 files. folder two contains 4 files.)</p>
  
  <pre><code>Folder1   "contains"   folder2     "contains"   Bzzz.file.R
                         A.file.txt               B.file.pdf
                         Bla.file.pdf             C.file.pdf
                         C.file.pdf               Delta.file.pdf
  </code></pre>
  
  <p>... should be transform into this....</p>
  
  <pre><code>Folder1  "contains"  folder2   "contains"  Bzzz.file.R
                       A.file.txt            1.pdf
                       1.pdf                 2.pdf
                       2.pdf                 3.pdf
  </code></pre>
  
  <p>The algorithm should be working whatever is the number of folders "and how deep is the folder inception"</p>
  
  <p>If this would be working even if there are already files called 3.pdf or other number.pdf in some folders it would be a bit better but I can actually deal without this flexibility !</p>
  
  <p>Thanks a lot for your help !</p>
  
  <p>I ran this code but it is not totally working. If I just run it, I get this error message: "Error in if (is.na(pathname)) {  : the argument's length is zero" (I had to translate this error message from french so there might have some missmatch with an error message we can get from R in english). </p>
  
  <p>Then I realized that d is weird because it contains a directory called "." at the first position that I don't have in my folder (Do you have an explanation for that by the way ?!).</p>
  
  <p>so I did d&lt;-d[-1] and rerun the big lapply I get this error message: </p>
  
  <p>"Error in setwd(d[x]): impossible to change working directory"
  And indeed the files in the first folder were renamed but only in the first folder</p>
  
  <p>Here what list.dirs() gives:</p>
  
  <p>"."        "./3.Sept" "./4.Oct"  "./5.Nov"  "./6.Dec" </p>
  
  <p>But I only have 4 folders. There is no folder called "." ! And as I said if I do d&lt;-d[-1] the code is not succeding at changing directory so that only one folder is renamed</p>