Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

List Files with Metadata: Size, Created and Modified Time


Lesson Description
-
  • Sometimes, we want to work with the concept of "List Files with Metadata: Size, Created and Modified Time" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.
filename mydir "C:\Users\curio\Desktop\Rough";

data file_list;
  length full_filename filename folder basename extension $256;
  length file_size file_created file_modified $64;

  folder = pathname("mydir");
  did = dopen("mydir");

  if did > 0 then do;
    nfiles = dnum(did);
    do i = 1 to nfiles;
      filename = dread(did, i);
      full_filename = cats(folder, "\", filename);

      rc1 = filename("fref", full_filename);
      if rc1 = 0 then do;
        fid = fopen("fref");
        if fid > 0 then do;
          file_size = strip(put(fsize(fid), best.));
          file_modified = finfo(fid, "Last Modified");
          file_created  = finfo(fid, "Create Time");
          rc2 = fclose(fid);
        end;
      end;

      if index(filename, ".") then do;
        extension = scan(filename, -1, ".");
        basename = substr(filename, 1, length(filename) - length(extension) - 1);
      end;
      else do;
        extension = "";
        basename = filename;
      end;

      output;
    end;
    rc = dclose(did);
  end;

  drop did nfiles i rc rc1 rc2 fid;
run;
  • We use `dopen()` and `dread()` to list all files in the folder.
  • The `filename()` and `fopen()` functions allow us to inspect each file.
  • `fsize()` retrieves file size, while `finfo()` gets creation and modification timestamps.
  • These details are stored in `file_size`, `file_created`, and `file_modified`.
  • Filename and extension are extracted using `scan()` and `substr()`.
library(tidyverse)
library(fs)

folder_path <- "C:/Users/curio/Desktop/Rough"

file_paths <- dir_ls(path = folder_path, type = "file")

file_list <- tibble(
  full_filename = file_paths,
  filename = path_file(file_paths),
  folder = folder_path,
  basename = tools::file_path_sans_ext(filename),
  extension = tools::file_ext(filename),
  file_size = file_info(file_paths)$size,
  modified_time = file_info(file_paths)$modification_time,
  created_time = file_info(file_paths)$change_time
)

print(file_list)
  • We are using `fs::dir_ls()` to list all file paths from the folder.
  • `path_file()` extracts just the filename from the full path.
  • `tools::file_path_sans_ext()` and `file_ext()` extract the base name and extension.
  • `file_info()` provides file size, modification time, and creation time for each file.
  • These details are added to the tibble to create a complete file listing with metadata.
folder_path <- "C:/Users/curio/Desktop/Rough"

file_paths <- list.files(path = folder_path, full.names = TRUE)

file_info <- file.info(file_paths)

file_list <- data.frame(
  full_filename = file_paths,
  filename = basename(file_paths),
  folder = folder_path,
  basename = tools::file_path_sans_ext(basename(file_paths)),
  extension = tools::file_ext(basename(file_paths)),
  file_size = file_info$size,
  modified_time = file_info$mtime,
  created_time = file_info$ctime,
  stringsAsFactors = FALSE
)

print(file_list)
  • file.info() returns size and timestamps for each file.
  • Build a data frame with name parts and metadata.