Append to file names in folder - r

How to append filenames in a folder
Filenames:
abc.wav
wjejrt.wav
13567tin.wav
Desired Output
abc_ENG.wav
wjejrt_ENG.wav
13567tin_ENG.wav
Tried this line code below but getting an error, maybe because I don't know the right use of file.rename function. Please help...
file.rename(list.files(pattern="*.wav"), paste0("_ENG"))

With base Ryou can do:
Filenames <- c("abc.wav", "wjejrt.wav", "13567tin.wav")
Fnames_new <- sub(".wav", "_ENG.wav", Filenames, fixed = TRUE)
file.rename(Filenames, Fnames_new)

Since you tagged Python, you could use os.rename() to rename your files:
from os import rename
from os import listdir
from os.path import splitext
# Current directory script is being run in
# You can change this to any path you want
path_to_folder = "."
for f in listdir(path_to_folder):
if f.endswith(".wav"):
name, ext = splitext(f)
rename(f, name + "_ENG" + ext)

You can try this one
^.*(?=\\.wav)
Explanation
^ - Anchor to start of string.
.* - Match anything except new line.
(?=\\.wav) - Positive look ahead matches .wav.
Change your code to this
file.rename(list.files(pattern=".*(?=\\.wav)"), paste0("_ENG"))
Demo

Related

How to Change Part of URL With a Function Input in R?

Let's say we have a url in R like:
url <- 'http://google.com/maps'
And the objective is to change the 'maps' part of it. I'd like to write a function where basically I can just input something (e.g. 'maps', 'images'), etc., and the relevant part of the url will automatically change to reflect what I'm typing in.
Is there a way to do this in R, where part of the url can be changed by typing something into a function?
Thanks!
You have to store the part you type into a variable and paste this to the base URL:
base_url <- "http://google.com/"
your_extension <- "maps"
paste0(base_url, your_extension)
[1] "http://google.com/maps"
If you have to start with a fixed URL, use sub to replace the last part:
sub("\\w+$", 'foo', url)
# "http://google.com/foo"
You can use dirname to remove the last part of the URL and paste it with additional custom string.
change_url_part <- function(base_url, string) {
paste(dirname(base_url), string, sep = '/')
}
change_url_part('http://google.com/maps', 'images')
#[1] "http://google.com/images"

make file.exists() case insensitive

I have a line of code in my script that checks if a file exists (actually, many files, this one line gets looped for a bunch of different files):
file.exists(Sys.glob(file.path(getwd(), "files", "*name*")))
This looks for any file in the directory /files/ that has "name" in it, e.g. "filename.csv". However, some of my files are named "fileName.csv" or "thisfileNAME.csv". They do not get recognized. How can i make file.exists treat this check in a case insensitive way?
In my other code i usually make any imported names or lists immediately lowercase with the tolower function. But I don't see any option to include that in the file.exists function.
Suggested solution using list.files:
If we have many files we might want to do this only once, otherwise we can put in in the function (and pass path_to_root_directory instead of found_files to the function)
found_files <- list.files(path_to_root_directory, recursive=FALSE)
Behaviour as file.exists (return value is boolean):
fileExIsTs <- function(file_path, found_files) {
return(tolower(file_path) %in% tolower(found_files))
}
Return value is file with spelling as found in directory or character(0) if no match:
fileExIsTs <- function(file_path, found_files) {
return(found_files[tolower(found_files) %in% tolower(file_path)])
}
Edit:
New solution to fit new requirements:
keywordExists <- function(keyword, found_files) {
return(any(grepl(keyword, found_files, ignore.case=TRUE)))
}
keywordExists("NaMe", found_files=c("filename.csv", "morefilenames.csv"))
Returns:
[1] TRUE
Or
Return value are files with spelling as found in directory or character(0) if no match:
keywordExists2 <- function(file_path, found_files) {
return(found_files[grepl(keyword, found_files, ignore.case=TRUE)])
}
keywordExists2("NaMe", found_files=c("filename.csv", "morefilenames.csv"))
Returns:
[1] "filename.csv" "morefilenames.csv"
The following should return a 1 if the filename matches in any case and a 0 if it does not.
max(grepl("*name*",list.files()),ignore.case=T)

Preventing duplicate slashes in file paths

I would like to build a path to a file, given a filename and a folder where that file exists. The folder may include a trailing slash or it may not. In python, os.path.join solves this problem for you. Is there a base R solution to this problem? If not, what is the recommended way in R to build file paths that do not have duplicate slashes?
This works fine:
> file.path("/path/to/folder", "file.txt")
[1] "/path/to/folder/file.txt"
But if the user provides a folder with a trailing slash, file.path does the still-functional-but-annoying double-slash:
> file.path("/path/to/folder/", "file.txt")
[1] "/path/to/folder//file.txt"
I'm looking for a built-in, 1 function answer to this common issue.
might be os independent, instead of explicitly coding /
joinpath = function(...) {
sep = .Platform$file.sep
result = gsub(paste0(sep,"{2,}"), sep, file.path(...), fixed=FALSE, perl=TRUE)
result = gsub(paste0(sep,"$"), '', result, fixed=FALSE, perl=TRUE)
return(result)
}
You could replace the // with / using gsub if it is too annoying. You could put it in a custom function for ease
file.path2 = function(..., fsep = .Platform$file.sep){
gsub("//", "/", file.path(..., fsep = fsep))
}
file.path2("/path/to/folder", "file.txt")
#[1] "/path/to/folder/file.txt"
file.path2("/path/to/folder/", "file.txt")
#[1] "/path/to/folder/file.txt"

Stuck with string.translate function in python 3

import os
def rename_files():
#(1) get file names from a folder
file_list = os.listdir("my_directory")
#print(file_list)
os.chdir("my_directory")
saved_path = os.getcwd()
print("Current work directory is " + saved_path)
os.getcwd()
#(2) for each file, rename filename
for file_name in file_list:
os.rename(file_name, file_name.translate(None, "0123456789"))
os.chdir("my_directory")
rename_files()
And after this I've got an error:
TypeError: translate() takes exactly one argument (2 given)
str.translate in python 3.x accepts just one argument i.e. translation table.
From docs:
str.translate(table)
Return a copy of the string in which each character has been mapped
through the given translation table
You can create required translation table using str.maketrans
table = str.maketrans(dict.fromkeys('0123456789'))
file_name.translate(table)

Concatenate variables in R

I want to create an object in R, which will contain one string, with a few variables (in my case it is file path). When I try to use paste to concatenate the file paths I can see only one last variable instead of all variables in one string. I use next code:
for(i in seq_len(nrow(samples))) {
lib = samples$conditions[i]
txtFile = file.path(lib, "hits.txt")
testfiles = paste(txtFile, sep = ',')
}
print(testfiles)
and get something like
cond/hits.txt,
instead of
cond/hits.txt,cond1/hits.txt,cond2/hits.txt and so on
Thank you very much for help

Resources