[R] Move assets to dated subfolders

As the assets folder can quickly become crowded, one can use this simple R script to move all assets to subfolders based on creation date and update all the links in LogSeq:

old_paths <- dir("assets/", full.names=TRUE)
old_paths <- old_paths[!file.info(old_paths)$isdir]
new_paths <- paste0("assets/", as.Date(file.info(old_paths)$ctime), "/", gsub("assets/", "", old_paths, fixed = TRUE))

my.file.rename <- function(from, to) {
  todir <- dirname(to)
  if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
  file.rename(from = from,  to = to)

for( i in seq_along(old_paths) ) {
  my.file.rename(from = old_paths[i], to = new_paths[i])

#' Replace multiple strings across multiple files with original values and replacement values.
#' Source: https://gist.github.com/mattjbayly/9c56ec80ae291ff00589ffa3440806a1
#' files - A character string of file names to work through.
#' f - A character string of original values that you want to replace.
#' r - A character string of replacement values (f->r).
multi_replace <- function(files="", f="", r=""){
  file_line = data.frame() # (optional) tracking table
  #loop through each file separately
  for(j in 1:length(files)){
    nl <- suppressWarnings(readLines(files[j])) # read file line by line
    # loop through each of the find and replace values within each file
    for(i in 1:length(f)){
      cnt_replaced <- data.frame(filename = files[j], find = f[i], replace = r[i], times = length(grep(f[i], nl))) # fill tracking table with values
      file_line <- rbind(file_line, cnt_replaced) # populate tracking table count of find & replace within each file
      nl <- gsub(f[i], r[i], nl) # find and replace value line by line
    write(nl, file = files[j]) # save files with same name & overwrite old
    rm(nl) # don't overwrite with previous file if error.
  return(file_line) # print the tracking table

filer = list.files(path = "journals", pattern=".md", full.names=TRUE)
tracking_sheet_journal <- multi_replace(filer, old_paths, new_paths)

filer = list.files(path = "pages", pattern=".md", full.names=TRUE)
tracking_sheet_pages <- multi_replace(filer, old_paths, new_paths)

save(tracking_sheet_journal, tracking_sheet_pages, 
     file = paste0("moved_assets_", as.Date(Sys.time()),".Rdata"))

Hi Anjar, I’m a newbie here but this looks interesting, might use it at some point in the future.

I don’t understand what an R script means, could you give a brief instruction on how I use this script? Assuming I copy it onto a notepad text file, where do I store it etc?

Many thanks, Smithy

1 Like

Hi Smithy - Thanks for showing interest! R is a programming language, so to use it, one needs to have R installed. Fortunately, it is free and open-source. Wen you have installed R and Rstudio, you can navigate to you logseq folder within Rstudio, make it the working directory (see “More” right above the Files panel), and paste the srcipt into the console

However, I bet someone on this forum is able to write a better script in bash or something - or perhaps as a plugin - that doesn’t require installation of R or other extra software

Hi Anjar,

I noticed that in another post you talked about a plugin you are working on, aiming at creating a sort of organization implying the use of folders.
May I ask you if this project is still under development or if you decided to give up it?

Many thanks