Recursively search subdirectories to find the first instance of file - r

I have a complex dataset that is spread over 80 directories for each city (C). Each of these cities have multiple and unidentical subdirectories of varying depth. To clarify this means that for an example: city 1 can have 5 subdirectories a-e, where each subdirectory again can have multiple subdirectories. Now I need to find the first instance of a .txt file in each terminal subdirectory and apply a function to the txt file (logical function that is already written). There are no .txt files in the pre-terminal subdirectories.
lapply(list.dirs,function(x) {
if length(list.files(path=x,pattern=".txt"))==0 {
**apply function to .txt file}**
else {**lapply list.dirs etc---**}
However, I´m left with a neverending loop this way. How can this be done efficiently?

you may need something like this :
Treat_txt<-function(direct){
if(length(list.files(direct,pattern=".txt"))){
do what you need to do with the text file
} else {
dirs<-list.dirs(direct,full.names=T,recursive=F)
sapply(dirs,Treat_txt)
}
}
And then you can just call the function with the path of the "top" directory

Related

how to write a function dir_info(directory) which returns 3 values

(1) Number of files inside the directory (recursively)
(2) Number of directories inside the directory (recursively)
def dir_info(directory):
nfiles = how many files inside directory?
ndirs = how many sub directories inside directory?
return nfiles, ndirs

Phalcon 4.x creates multiple folders to store cached data. What is the benefit of having recursive and multiple folders?

$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.

How to create multiple tempdirs in a single R session?

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.

find command: conditionally prune subdirectories of any directory containing a matched file

I'd like to search a directory tree for regular files matching particular patterns, e.g.
find -regex '.*\.\(expect\|missing\|ok\)'
This is fine, but in my application it is very inefficient -- since if a directory contains at least one matching file, then none of its (many, deep, file-laden) subdirectories need to be searched, since they definitely won't contain any matches.
So how do I -prune subdirectories, only if a matched file is found in their parent directory?
E.g. if I have the following tree:
./a/file.ok
./a/a1/
./a/a2/
./b/file.missing
./b/file.expect
./b/b1/
./b/b2/
./c/c1/
./c/c1/c11/
./c/c1/c11/foo.expect
./c/c1/c11/foo.txt
Then I would like to print only:
./a/file.ok
./b/file.missing
./b/file.expect
./c/c1/c11/foo.expect
without actually searching/traversing all the subdirectory trees of a/ and b/
(This earlier question approaches what I want, but hasn't yet really been answered.)

how to select multiple folders in scilab

I am looking for a function to select multiple directories in Scilab. I have found a similar function in Matlab Central uipickfiles.m. But is there a simpler and similar function in Scilab.
If it is not already available I am trying to write a function for the same.
Any suggestions/guidance is very appreciated.
regards
Devaraj
Since the built-in GUI does not allow you to select more directories, here is a workaround in 3 steps:
first select a "parent" directory, in which you want to select
multiple subdirectories
list the subdirectories
choose multiple subdirectories with an x_choices dialog
E.g.:
directory=uigetdir(); //select the parent directory, in which you want to choose multiple subdirectories!
allfiles=dir(directory); //all files in the directory
onlydirectories=allfiles.name(find(allfiles.isdir)); //select only the directories
if size(onlydirectories,"*")>1 then //there are 2 or more directory
L=list(list(onlydirectories(1),1,["-","+"])); //build the lists for x_choices:
for i=2:size(onlydirectories,"*")
L(i)=list(onlydirectories(i),1,["-","+"]);
end
rep=x_choices("Select directories with +",L); //multiple choices with toggle buttons
selecteddirectories=onlydirectories(find(rep==2));
disp(selecteddirectories,"selecteddirectories:");
selectedfullpath=directory+selecteddirectories+"\";
disp(selectedfullpath,"selectedfullpath:");
end
Not too pretty solution but it kind of works...

Resources