Copy all files matching pattern from dir and subdirs into a single dir - r

I have multiple folders into the path: "/home/user/IAT/main/assets/"
Each folder have the following structure: "~/assets /lroot/images/batch0001/samples/"
AllFolders
lroot
images
batch0001
samples
figure_12.png
figure_17.png
...
thumbnails
dataframes
all_g2
images
batch0001
samples
figure_y32.png
figure_x79.png
...
thumbnails
dataframes
before_g1
images
batch0001
samples
figure_2.jpg
figure_17.jpg
...
thumbnails
dataframes
I have a *.txt with name of some images (without extension) into this folders.
for example: figure_12, figure_17, figure_x79, figure_2 and figure_17
I would to copy this images in folder samples referenced in *.txt to another folder in Desktop.
How could I to do that in R?
Thank's all

I recently figured out how to do this. Below I use the magick package to create a couple of test images.
library(magick)
image1 = image_blank(600,600,col="darkred")
image_write(image1, path="./tst1/image1.png")
image2 = image_blank(600,600,col="darkblue")
image_write(image2, path="./tst1/image2.png")
images=c("image1.png","image2.png")
pathOld = "./tst1/"
pathNew = "./tst1/tst2/"
for (i in 1:length(images)) {
fileOld = paste0(pathOld,images[i])
fileNew = paste0(pathNew,images[i])
file.copy(fileOld,fileNew)
print(c(fileOld,fileNew,i))
}

Related

Copy/move files by folder name pattern in R

I have a folder (~/PATH/MYFOLDER) with a lot of subfolders and files.
Subfolders are named, for example, as: LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq
Inside each subfolder has a lot of image files (*.png and/or *.jpg)
In ~/PATH/ I have folders with part of the name of subfolders, as: orgx, orgy, phyw, detz, appq
I would to copy image files of subfolders: LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq, to respective folders: orgx, orgy, phyw, detz, appq
Any help would be great.
Thanks all.
You can use sub to remove "MYFOLDER/Lclass_" from the file names. Something like this:
from = list.files(
path = "~/PATH/MYFOLDER",
pattern = "(png|jpg)$",
recursive = TRUE,
full.names = TRUE
)
to = sub(x = from, pattern = "MYFOLDER/Lclass_", replacement = "", fixed = TRUE)
file.copy(from = from, to = to)
This should take input from list.files like "~/PATH/MYFOLDER/LClass_orgx/file.jpg" (from) and change it to "~/PATH/orgx/file.jpg" (to), and then copy it accordingly. You could then use file.remove to delete the old ones. (Potentially you could do this all at once with file.rename, but it seems safer to copy and take a minute to check that things look right before deleting the old ones.)
If you need to be more specific in the sources, you could modify the list.files(pattern) to specify the source directories you mention, LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq.

How to save the R output in different directory?

I have three different folders name as "5k", "10k" and "15k", I can save the R out from the following code using this for loop.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
setwd(paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i]))
save(y, file =paste0("ydat",iter_name[i],".RData"))
}
Is there any shortcut or better way to do this.
Any help is appreciated.
Try the following code. It looks like you are omitting / in the second call to paste0.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
file = paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i], '/' , "ydat",iter_name[i],".RData")
save(y, file = file)
}

How to delete all files in a directory?

My script :
#RequireAdmin
FileDelete("C:\Users\Administrator\Desktop\temp\")
I want to delete all files in that directory. I also tried :
#RequireAdmin
DirRemove("C:\Users\Administrator\Desktop\temp\")
But it's not working, any suggestion?
The syntax for FileDelete() is FileDelete("filename") ;not only directory!. You can also use wildcards for filename (* and ?).
DirRemove() works as follows: DirRemove ( "path" [, recurse = 0] ). With recurse=0 (default), deletes the folder, but only if it is empty. With recurse=1 removes files and subdirectories (like the DOS DelTree command).
Maybe you misunderstood the flag to use:
; Remove only the empty folder "Folder_path"
DirRemove("Folder_Path")
; Remove folder "Folder_Path" with all subfolder and all files within
DirRemove("Folder_Path", 1)
If this doesn't work it's a matter of system rights. If you want to delete files without deleting containing folder:
#include <Files.au3>
; Get all files in folder and delete them:
Local $aFilesInRoot = _FileListToArray("Your_Path", 1, True) ; 1=$FLTA_FILES = Return files only, True=returns full path
For $i = 1 To $aFilesInRoot[0]
FileDelete($aFilesInRoot[1])
Next
; Get all subfolders under root and delete them:
Local $aFolderInRoot = _FileListToArray("Your_Path", 2, True) ;2=$FLTA_FOLDERS = Return Folders only
For $i = 1 To $aFolderInRoot[0]
DirRemove($aFolderInRoot[1], 1)
Next
But isn't it easier to remake the deleted folder after deleting all with only one command?

Create directory structure using template in a text file

Suppose I have a text file text.txt that contains several lines of text indented using single spaces (not tabs) like:
My folder1/
My folder2/
My folder3/
file1.md
My folder4/
For example, and actual directory template might look like:
Proposal/
rules/
proposal/
document.tex
figs/
tabs/
ref/
log/
src/
master.r
crdat/
andat/
temp/
data/
raw/
clean/
Admin/
budget/
contract/
invoices/
receipts/
team/
hiring/
vitae/
gantt/
forms/
misc/
Study/
document.tex
figs/
tabs/
ref/
log/
src/
master.r
crdat/
andat/
temp/
data/
raw/
clean/
Dissemination/
presentations/
conference1/
submission.md
slides.tex
notes.md
admin/
registration/
travel/
receipts/
program/
forms/
conference2/
manuscripts/
journal1/
submission/
letter_v1.tex
manuscript_v1.pdf
replication_v1.zip
comments/
reviewer1.txt
R&R/
letter_v2.tex
manuscript_v2.tex
diff.tex
replication_v2.zip
journal2/
master_notes.md
TODOs.md
README.md
Essentially the file is a template for directory structure. This template can be set by the user. She could use different templates for different projects say, and the names are generic. The only constraints are that hierarchy is established using spaces, and folders end in forward slash.
I want to write a function that takes a any such directory template as an input and creates a directory structure in your current working directory. The pseudo code is as follows:
lines <- readLines(text.txt)
last.indent <- 0
for (i in lines) {
Create directory structure by looking at leading character and last characters (folders end in /)
using dir.create(i) or file.create(i)
}
I suspect you're actually doing something more complicated. In which case this may not work:
library(qdap); library(reports)
x <- readLines(n=13) ## you'd read this in from a file I assume
My folder1/
My folder2/
My folder3/
file1.md
My folder4/
My folder1b/
My folder2/
My folder3/
file1.zip
My folder1c/
My folder2/
My folder3/
file1.pdf
## You didn't ask for this but this keeps the direcories contained
new <- folder(new)
setwd(new)
dirs <- which(substring(x, 1, 1) != " ")
lens <- lapply(seq_along(dirs), function(i) {
dirs[i]:c(tail(dirs - 1, -1), length(x))[i]
})
paths <- split(x, rep(1:length(dirs), sapply(lens, length)))
path <- sapply(paths, function(x) paste(Trim(x), collapse=""))
is.dir <- sapply(path, function(x) substring(x, nchar(x))) == "/"
lapply(seq_along(is.dir), function(i){
out <- folder(folder.name =ifelse(is.dir[i],
path[i], dirname(path[i])))
if(!is.dir[i]) {
cat("", file = paste0(path[i], "_hold_.txt"))
}
})

Open up and load multiple files in Scilab with the extension .csv

I need a way to open all the files in a folder, regardless of the filename. All the files have a .csv extension.
You could use listfiles and csvRead like this
function doSomethingWithCSVdata(CSV_data)
disp(CSV_data);
endfunction
csv_files = listfiles('*.csv');
for i=1:size(csv_files,1)
disp('Opening file: '+csv_files(i))
csvData = csvRead(csv_files(i),ascii(9), [], 'string');
doSomethingWithCSVdata(csvData);
end

Resources