I have a question how to create a new folder based on part of the filename and also move directly the corresponding files to the new folder. Below you will find the structure of my directory with some examples. In both D0 and D1 folders you will find Weather and Day folder that contains .TIF files. What I would like is that inside the brightfield and FITC folders folders are created that are based partially on the filename. For example if the file contains s2, then a folder S2 is created with the file that has S2 in his name.
Currently the situation
main Directory
|
|___ Experiment
├── D0
├── Weather
|__ D0_PM_flow__Weather 100 - CAM_s3_t5.TIF
|__ D0_PM_flow__Weather 100 - CAM_s3_t5.TIF
│ └── Temperature
└── D1
├── Weather
└── Temperature
What I would like:
main Directory
|
|___ Experiment
├── D0
├── Weather
|__ S1
| D0_PM_flow__Weather 100.txt
|__ S2
|D0_PM_flow__Weather 100.txt
│ └── Temperature
└── D1
├── Weather
└── Temperature
I can do this manually with the code below, but that takes a lot of time. Is there a more convenient way to do this more automatically? For convenience I only showed 3 files in the D0/Weather folder, but there are also files in the D0/Temperature folder, D1/Weather and D1/Temperature where I also want to create folders such as S1,S2 etc.. Furthermore, some folders have only three groups, so S1- S3, while other groups have S1- S6.
Recycling some code:
library(stringr)
#Get all files
path <- 'C:/temp/test'
files <- list.files(path= path, recursive = TRUE)
move.file <- function(filename) {
(fromdir <- dirname(filename))
(filebase <- basename(filename))
dir.match <- toupper(str_extract(filename,'_[Ss][0-9]+_'))
if (isTRUE(nchar(dir.match)>0)) {
dir.match <- substr(dir.match,2,nchar(dir.match)-1)
# File not in right directory
if (basename(fromdir)!=dir.match) {
dir.create(file.path(path,fromdir,dir.match),showWarnings = F)
file.rename(from = file.path(path,filename),
to = file.path(path,fromdir,dir.match,filebase))
}
} else {F}
}
lapply(files, move.file)
Related
I need to iterate over a subfolder for SCSS files in a variety of locations to compile in CSS (obviously). However, I have no idea how to do this, not being skilled in that area. I am also hampered by the fact I have to try to do this using NPM, and SASS (not node-sass).
Could someone give me a whistle-stop tour on how to achieve what I want?
It might also be worth pointing out that the SCSS files may reside in the SRC folder on the odd occasion, and I need to create the CSS files in the folder the SCSS was found in :(
As an example my folder structure looks a little like this;
Root
|
|- Components
| |- src
| |- styles
| |- buttons.scss
|
|- package.json
The end result would look like this
Root
|
|- Components
| |- src
| |- styles
| |- buttons.scss
| |- buttons.css
|
|- package.json
Any help would be greatly appreciated as I would like to learn how to do this once and for all ;)
I have folder containing files a1_1, a1_2, a2_1, a2_2, a3_1, a3_2 and folder in another directory containing b1_1, b1_2, b2_1, b2_2, b3_1, b3_2. Want to combine them into one new folder like this:
1_1 (Folder)
a1_1
b1_1
1_2 (Folder)
a1_2
b1_2
2_1 (Folder)
a2_1
b2_1
2_2 (Folder)
a2_2
b2_2
I am using UNIX, doing it one by one will be troublesome since I have 300 pairs of file that should be combined. Please help
You can easily do it using basic bash scripting, here is an example on how to do it with comments explaining the steps.
#!/bin/bash
source_dir_a=$1 // Directory containing a files given as first argument
source_dir_b=$2 // Directory containing b files given as second argument
a_files=$(ls $source_dir_a) // Save list of files in provided a dir
for a_file in $a_files; do // Loop over the files in the a dir
echo "a_file=$a_file"
folder=$(echo $a_file | sed -e 's/^a//g') // Remove the leading "a" of a files using a regex for the storing folder name
echo "folder=$folder"
b_file=$(echo b$folder) // Adding leading "b" to folder name to have b_file name
echo "b_file=$b_file"
mkdir $folder // Create output folder
mv $source_dir_a/$a_file $source_dir_b/$b_file $folder // Move the a and b files in output folder
done
What's missing is some error checking, especially if some expected files are not present.
I have a working directory with the following content
readdir()
6-element Vector{String}:
"Manifest.toml"
"Project.toml"
"Report"
"main.jl"
"src"
"test.jl"
I can identify folders here:
filter(isdir, readdir())
2-element Vector{String}:
"Report"
"src"
And I can show the content of the sub-folder Report:
readdir("Report/")
3-element Vector{String}:
"jl_ACKKIu"
"jl_zfa8Ys"
"test.pdf"
So, why can I not identify folders in the sub-folder?
filter(isdir, readdir("Report/"))
String[]
readdir contains paths relative to the input directory and isdir then interprets these paths as relative to the current working directory (see pwd).
Since Julia 1.4 you can pass join=true as a keyword argument to readdir, such that the returned paths are joined with the input path:
shell> tree .
.
├── DirectoryB
│ ├── DirectoryC
│ └── FileB
└── FileA
2 directories, 2 files
julia> readdir("DirectoryB")
2-element Vector{String}:
"DirectoryC"
"FileB"
julia> readdir("DirectoryB"; join=true)
2-element Vector{String}:
"DirectoryB/DirectoryC"
"DirectoryB/FileB"
julia> filter(isdir, ans)
1-element Vector{String}:
"DirectoryB/DirectoryC"
There is my project.gpr file :
project Test is
Project_Source_Dirs := ("Dir1")
& ("src")
& ("../../lib_all");
for Object_Dir is "lib";
for Main use ("Main_file.adb");
for Languages use ("Ada");
for Source_Dirs use Project_Source_Dirs & project'Object_Dir;
-- [...]
end Test;
There is my "../../lib_all" folder :
lib_all
|- file1.adb
|- file1.ads
|- file2.adb
|- file2.ads
|- file3.adb
|- file3.ads
|- file4.adb
|- file4.ads
I only want to use file3, how can I modify my .gpr file to not compile all other files each time ?
You can use Source_Files or Excluded_Source_Files settings, as described here: https://docs.adacore.com/gprbuild-docs/html/gprbuild_ug/gnat_project_manager.html#source-files-and-directories
But I think, the easier way could be simply moving unneeded files to separated directory and adding that directory with its content on demand (based on variables, etc.).
Is there a way to get the directory path and the paths of all the subdirectories recursively using GNU make without explicitly writing
dirpaths :=
dirpaths +=$(wlidcard L0dir/*)
dirpaths +=$(wlidcard L0dir/*/*)
and so on.
L0dir
├── L1subdir1
│ ├── L2subdir1
│ ├── L2subdir2
│ └── L2subdir3
├── L1subdir2
│ └── L2subdir1
├── L1subdir3
└── L1subdir4
The content of the variable $(dirpaths) should be :
L0dir L0dir/L1subdir1 L0dir/L1subdir1/L2subdir1 L0dir/L1subdir1/L2subdir2 L0dir/L1subdir1/L2subdir3 L0dir/L1subdir2 L0dir/L1subdir2/L2subdir1 L0dir/L1subdir3 L0dir/L1subdir4
You can use something like this:
recurse = $(foreach D,$1,$(wildcard $D/*) $(call recurse,$(patsubst %/.,%,$(wildcard $D/*/.))))
dirpaths := $(call recurse,$(DIR))