{"id":669,"date":"2014-10-14T11:34:52","date_gmt":"2014-10-14T18:34:52","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=669"},"modified":"2014-10-14T11:34:52","modified_gmt":"2014-10-14T18:34:52","slug":"programatically-rename-files-or-do-other-stuff-to-them-in-r","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/10\/14\/programatically-rename-files-or-do-other-stuff-to-them-in-r\/","title":{"rendered":"Programatically rename files (or do other stuff to them) in R"},"content":{"rendered":"<div>In order to do something to a bunch of files at once, we first need a vector which contains the file paths of just the files we are interested in.<\/p>\n<p>startingDir&lt;-&#8220;\/myDirectory&#8221;<\/p><\/div>\n<div><\/div>\n<div>filez&lt;-list.files(startingDir,pattern=&#8221;searchPattern&#8221;)<\/div>\n<p>&nbsp;<\/p>\n<div>head(filez)<br \/>\n[1] &#8220;\/myDirectory\/xxxFile1.txt&#8221;<br \/>\n[2] &#8220;\/myDirectory\/xxxFile2.txt&#8221;<br \/>\n[3] &#8220;\/myDirectory\/xxxFile3.txt&#8221;<br \/>\n[4] &#8220;\/myDirectory\/xxxFile4.txt&#8221;<br \/>\n[5] &#8220;\/myDirectory\/xxxFile5.txt&#8221;<br \/>\n[6] &#8220;\/myDirectory\/xxxFile6.txt&#8221;<\/p>\n<\/div>\n<div>Once you have a vector that consists of a bunch of file paths, you can do useful stuff with it, like renaming files in a consistent way. I can use sapply() to apply a function to every element of a vector.\u00a0 For example, I can use the file.rename() function along with the sub() function to change the filename.<\/div>\n<p>&nbsp;<\/p>\n<div>sapply(filez,FUN=function(eachPath){<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0 file.rename(from=eachPath,to=sub(pattern=&#8221;xxx&#8221;,replacement=&#8221;newTextString&#8221;,eachPath))<\/div>\n<p>})<\/p>\n<div>If you aren&#8217;t familiar with sapply(), I highly recommend you take a moment to study up on how it works.\u00a0 Using the apply() family of functions is really important to being productive in R.\u00a0 Now my filenames look like this.<\/div>\n<div><\/div>\n<p>[1] &#8220;\/myDirectory\/NewTextStringFile1.txt&#8221;<br \/>\n[2] &#8220;\/myDirectory\/NewTextStringFile2.txt&#8221;<br \/>\n[3] &#8220;\/myDirectory\/NewTextStringFile3.txt&#8221;<br \/>\n[4] &#8220;\/myDirectory\/NewTextStringFile4.txt&#8221;<br \/>\n[5] &#8220;\/myDirectory\/NewTextStringFile5.txt&#8221;<br \/>\n[6] &#8220;\/myDirectory\/NewTextStringFile6.txt&#8221;<\/p>\n<p>This is a whole lot faster and less error prone than trying to rename 500 files by hand.\u00a0 Just type ?files for a list of other file functions that might be useful, like file.move() and unlink().\u00a0 Be careful with unlink()&#8230;.it will delete files from your machine!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to do something to a bunch of files at once, we first need a vector which contains the file paths of just the&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-669","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}