Files

Rory Nolan

2017-02-23

Please Read the Manual

This package is one for which the functions are largely simple enough such that the function names well describe their purpose, so the manual is an excellent way to acquaint yourself with the package. But I made a couple of vignettes anyway.

First let’s load the library:

library(filesstrings)
#> Loading required package: stringr

Here are some file operations that I wished were easier in R.

Move files around

I find it bizarre that base R has no file.move. To move a file, you have to cleverly rename it. Well, no more.

setwd(tempdir())
dir.create("tmp_00")
file.create("tmp000.txt")
#> [1] TRUE
list.files()
#> [1] "tmp000.txt" "tmp_00"
PutFilesInDir("tmp000.txt", "tmp_00")
#> tmp000.txt 
#>       TRUE
list.files()
#> [1] "tmp_00"
list.files("tmp_00")
#> [1] "tmp000.txt"
unlink("tmp_00", recursive = TRUE)

Delete Directories

That unlink above with recursive = TRUE was a cryptic way to delete a directory right? I give you RemoveDirs().

setwd(tempdir())
dir.create("tmp_00")
list.files()
#> [1] "tmp_00"
RemoveDirs("tmp_00")
#> tmp_00 
#>   TRUE
list.files()
#> character(0)

Remove spaces from file names

Surely I don’t have to convince anyone that spaces in file names are a bad idea? Let’s get rid of some!

setwd(tempdir())
file.create(c("file 1.txt", "file 2.txt"))
#> [1] TRUE TRUE
list.files()
#> [1] "file 1.txt" "file 2.txt"
RemoveFileNameSpaces(replace.with = "_")
#> [1] TRUE TRUE
list.files()
#> [1] "file_1.txt" "file_2.txt"
file.remove(list.files())
#> [1] TRUE TRUE