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.
Related
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
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
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?
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"))
}
})
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