Here are some useful things which filesstrings
makes easier than base
or fs
.
First let’s load the library:
#> Loading required package: stringr
“A space in your file name is a hole in your soul.” - Jenny Bryan
remove_filename_spaces(replacement = "_")
replaces them all with underscores for all files in a directory. By default, they are replaced with nothing.
#> [1] TRUE TRUE
#> 2 files required renaming and this was done successfully.
#> [1] "file_1.txt" "file_2.txt"
#> [1] TRUE TRUE
The microscope I use numbers files with 3 numbers by default, i.e. file001.tif
, file002.tif
and so on. This is a problem when the automatic numbering passes 1000, whereby we have file999.tif
, file1000.tif
. What’s the problem with this? Well, sometimes you need alphabetical order to reflect the true order of your files. These file numbers don’t satisfy this requirement:
#> [1] "file1000.tif" "file999.tif"
so file1000.tif
comes before file999.tif
in alphabetical order. The function nice_nums()
returns the names that we’d like them to have:
#> [1] "file0999.tif" "file1000.tif"
The function nice_file_nums
applies such renaming to all the files in an entire directory. It wraps nice_nums
.
#> [1] "spreadsheet_92"
Add a file extension if needed:
#> [1] "xyz.csv"
If the file name has the correct extension already, it’s left alone:
#> [1] "xyz.csv"
Change a file extension:
#> [1] "abc.csv.txt"
#> [1] "abc.txt"