The file names that you gave last are different than the one you gave first, that is why the code was failing, now should work, at least for the examples you are giving.
library(stringr)
file_name <- c("../Tests/101/Testler Export/801-Yurume Ileri//Test_1/340535.txt",
"../Tests//101/Testler Export/801-Yurume Ileri/Test_1/340535.txt",
"../Tests//102/Testler Export/811-Sandalye/Test_3/340535.txt")
str_extract(file_name, "(?<=Tests//?)[0-9]+(?=/)")
#> [1] "101" "101" "102"
str_extract(file_name, "(?<=Export/)[^/]+(?=/+T)")
#> [1] "801-Yurume Ileri" "801-Yurume Ileri" "811-Sandalye"
str_extract(file_name, "(?<=/)[^/]+(?=/[:digit:]+.txt)")
#> [1] "Test_1" "Test_1" "Test_3"
str_extract(file_name, "(?<=/)[:digit:]+(?=.txt)")
#> [1] "340535" "340535" "340535"
Created on 2019-02-16 by the reprex package (v0.2.1)
So, this code should work with your data
library(tidyverse)
library(stringr)
list_of_files <- list.files(path = "../Tests/",
recursive = TRUE,
pattern = "340506\\.txt$|340527\\.txt$",
full.names = TRUE)
df <- list_of_files %>%
setNames(nm = .) %>%
map_df(read.table, header = T, fill = T, skip = 4, .id = "file_name") %>%
mutate(subject = str_extract(file_name, "(?<=Tests//?)[0-9]+(?=/)"),
act = str_extract(file_name, "(?<=Export/)[^/]+(?=/+T)"),
test = str_extract(file_name, "(?<=/)[^/]+(?=/[:digit:]+.txt)"),
sensor = str_extract(file_name, "(?<=/)[:digit:]+(?=.txt)")
)
What I'm using is called "Regular Expressions", if you're unfamiliar with them, I’d recommend starting at