I have number of empty folders inside a working directory. Want to remove those empty folders in R. Can somebody help me out on this.
Assuming the current directory is the one in which you want to delete the empty folders, you can do:
folders <- list.dirs(recursive = FALSE)
for(folder in folders){
if(length(dir(folder)) == 0){
unlink(folder, recursive = TRUE)
}
}
We can delete folders using:
unlink("path_to_non_empty_dir", recursive=TRUE)
Edit: removed part about non-empty folders
From ?unlink:
If recursive = FALSE directories are not deleted, not even empty ones.
Related
I have over 7,000 .wav files in one folder which need to be split up into groups of 12 and placed into separate smaller folders.
The files correspond to 1-minute recordings taken every 5 minutes, so every 12 files corresponds to 1 hour.
The files are stored on my PC in the working directory: "E:/Audiomoth Files/Winter/Rural/Emma/"
Examples of the file names are as follows:
20210111_000000.wav
20210111_000500.wav
20210111_001000.wav
20210111_001500.wav
20210111_002000.wav
20210111_002500.wav
20210111_003000.wav
20210111_003500.wav
20210111_004000.wav
20210111_004500.wav
20210111_005000.wav
20210111_005500.wav
which would be one hour, then
20210111_010000.wav
20210111_010500.wav
20210111_011000.wav
and so on.
I need the files split into groups of 12 and then I need a new folder to be created in: "E:/Audiomoth Files/Winter/Rural/Emma/Organised Files"
With the new folders named 'Hour 1', 'Hour 2' and so on.
What is the exact code I need to do this?
As is probably very obvious I'm a complete beginner with R so if the answer could be spelt out in layman's terms that would be brilliant.
Thank you in advance
Something like this?
I intentionally used copy instead of cut in order to prevent data from being lost. I edited the answer so the files will keep their old names. I order to give them new names, replace name in the last line by "Part_", i, ".wav", for example.
# get a list of the paths to all the files
old_files <- list.files("E:/Audiomoth Files/Winter/Rural/Emma/", pattern = "\\.wav$", full.names = TRUE)
# create new directory
dir.create("E:/Audiomoth Files/Winter/Rural/Emma/Organised Files")
# start a loop, repeat as often as there are groups of 12 within the list of files
for(i in 1:(round(length(old_files)/12)+1)){
# create a directory for the hour
directory <- paste("E:/Audiomoth Files/Winter/Rural/Emma/Organised Files", "/Hour_", i, sep = "")
dir.create(directory)
# select the files that are to copy (I guess it will start with 1*12-11 = 1st file
# and end with i*12 = 12th file)
filesToCopy <- old_files[(i*12-11):(i*12)]
# for those files run another loop:
for(file in 1:12){
# get the name of the file
name <- basename(filesToCopy[file])
# copy the file to the current directory
file.copy(filesToCopy[file], paste(directory, "/", name, sep = ""))
}
}
When you're not entirely sure, I'd recommend to copy the files instead of moving them directly (which is what I hope this script here does). You can delete them manually, later on. After you checked that everything worked well and all data is where it should be. Otherwise data can be lost due to even small errors, which we do not want to happen.
$hash = '123456789';
$fileName = "test.{$hash}.html";
the final directory structure will be like following.
te/st/.1/23/45/67/test.123456789.html
it simply breaks down the file name in to directories of two characters leaving the last 2 chars from the path test.123456789
To prevent too many files in one folder error.
See https://docs.phalcon.io/4.0/en/cache#stream as well.
I need to create multiple temp directories during a single R session but every time I call tempdir() I get the same directory.
Is there an easy way to ensure that every call will give me a new temp directory?
Use dir.create(tempfile()) to create a uniquely named directory inside the R temporary directory. Repeat as necessary.
You can only have one tempdir. But you could create subdirectories in it and use those instead.
If you want to automate the creation of those subdirectories (instead of having to name them manually), you could use:
if(dir.exists(paste0(tempdir(), "/1"))) {
dir.create(paste0(
tempdir(), paste0(
"/", as.character(as.numeric(sub(paste0(
tempdir(), "/"
),
"", tail(list.dirs(tempdir()), 1))) + 1))))
} else {
dir.create(paste0(tempdir(), "/1"))
}
This expression will name the first subdirectory 1 and any subsequent one with increment of 1 (so 2, 3, etc.).
This way you do not have to keep track of how many subdirectories you have already created and you can use this expression in a function, etc.
I am trying to create a code which copies all folders/files from an existing folder to another (the origin folder has files and more folders that contains files/folders...)
My idea was to do something like this:
files <- list.files (Dir.origen)
for (i in files)
{
if (!file.info (paste(Dir.origen, i, sep = "/"))$isdir)
file.copy (paste(Dir.origen, i, sep = "/"), Dir.dest)
else dir.create (paste(Dir.dest,i,sep = "/"))
}
and insert the same for loop in else statement, and more loops inside.
My question is if there is a way to copy an entire directory.
I am also interested in source this code every time I create a new project in RStudio.
As RStudio creates a new directory for an empty project my objective is to fill this directory with all content I need.
I found out an answer, it is easier then it seems:
Dir.origen2 <- gsub("/","\\\\", Dir.origen) # Directiories must use backslashes
Dir.dest2 <- gsub("/","\\\\", Dir.dest)
comando <- paste0 ("xcopy ", Dir.origen2, " ", Dir.dest2, " /e /i /y")
system(comando)
where /e is for copy all the directories (including empties), /y for don't ask overwriting of documents and /i to create a new directory if Dir.dest do not exists (I guess).
What is the most robust method to move an entire directory from say /tmp/RtmpK4k1Ju/oldname to /home/jeroen/newname? The easiest way is file.rename however this doesn't always work, for example when from and to are on different disks. In that case the entire directory needs to be recursively copied.
Here is something I came up with, however it's a bit involved, and I'm not sure it will work cross-platform. Is there a better way?
dir.move <- function(from, to){
stopifnot(!file.exists(to));
if(file.rename(from, to)){
return(TRUE)
}
stopifnot(dir.create(to, recursive=TRUE));
setwd(from)
if(all(file.copy(list.files(all.files=TRUE, include.dirs=TRUE), to, recursive=TRUE))){
#success!
unlink(from, recursive=TRUE);
return(TRUE)
}
#fail!
unlink(to, recursive=TRUE);
stop("Failed to move ", from, " to ", to);
}
I think file.copy shall be sufficient.
file.copy(from, to, overwrite = recursive, recursive = FALSE,
copy.mode = TRUE)
From ?file.copy:
from, to: character vectors, containing file names or paths. For
‘file.copy’ and ‘file.symlink’ ‘to’ can alternatively
be the path to a single existing directory.
and:
recursive: logical. If ‘to’ is a directory, should directories in
‘from’ be copied (and their contents)? (Like ‘cp -R’ on
POSIX OSes.)
From the description about recursive we know from can have directories. Therefore in your above code listing all files before copy is unnecessary. And just remember the to directory would be the parent of the copied from. For example, after file.copy("dir_a/", "new_dir/", recursive = T), there'd be a dir_a under new_dir.
Your code have done the deletion part pretty well. unlink has a nice recursive option, which file.remove doesn't.
unlink(x, recursive = FALSE, force = FALSE)
Why not just invoke the system directly:
> system('mv /tmp/RtmpK4k1Ju/oldname /home/jeroen/newname')