How to Rename Exported Notion Files

Remove the alphanumeric scramble in exported and extraction Notion files using R studio

tools
resource
Author
Affiliations

Information Epidemiology Lab

Johns Hopkins School of Public Health (2022)

Published

September 3, 2023

If you use Notion in your research, then you’ve probably encountered the dreaded renamed files. Here’s how you can remove the alphanumeric scramble added to document titles in bulk using R.

When you export files from Notion using the “markdown and CSV” option, the file names are often written something like “YourFileName <alphanumeric scramble>.md”. The other available export options are HTML and PDF, which may be even less desirable than the first option if you have a large table with data.

Two issues you may face when attempting to export your files:

  1. The automated naming system that Notion applies to your files is long, and your computer may be unable to read it and, thus, unable to extract the files so you can use them.

  2. Your extracted files have names that trail in an alphanumeric scramble you don’t want as the title for your file.

This post addresses both of these issues.

Extracting files if your computer is unable

Here’s a step-by-step guide on downloading and using the 7z file manager to extract exported Notion files:

Downloading and Installing 7-Zip

  1. Open Your Browser: Navigate to the official 7-Zip website.
  2. Download 7-Zip:
    • Visit the 7-Zip download page.
    • Depending on your Windows version (32-bit or 64-bit), click the respective link to download the installer. Most modern computers will use the 64-bit version.
  3. Run the Installer:
    • Locate the downloaded file in your Downloads folder (or wherever you’ve saved it).
    • Double-click on the installer file to run it.
  4. Install 7-Zip:
    • Follow the on-screen instructions to install 7-Zip on your computer.
    • Once installed, you should see the 7-Zip option when you right-click on files.

Extracting Exported Notion Files using 7-Zip

  1. Locate the Notion Export: Navigate to the folder where your exported Notion file (probably a .zip file) is saved.
  2. Right-click on the File: A dropdown menu will appear.
  3. Select 7-Zip from the Menu: Hovering over it will show you a submenu.
  4. Click ‘Extract Here’ or ’Extract to “/”:
    • If you choose “Extract Here,” the contents will be extracted in the current folder.
    • If you select Extract to "<folder_name>/", 7-Zip will create a new folder named after the zip file and extract the contents there.
  5. Access the Extracted Files: Once extracted, you’ll see the files/folders that were inside the Notion export. Navigate through them as you would with any regular file or folder.
  6. Optional – Open with 7-Zip File Manager:
    • If you want to view the content without extracting, right-click on the Notion exported .zip file.
    • Choose 7-Zip > Open Archive. This will open the file within the 7-Zip file manager, allowing you to view its contents without extracting.

That’s it! You’ve successfully used 7-Zip to extract your exported Notion file. Remember, the structure and organization of the files inside depend on how they were organized within Notion, so navigate through the extracted folders to find your content.

Renaming the files

Installing R & RStudio, and Renaming Files Extracted from Notion

Step 1: Installing R

  1. Navigate to CRAN:
    • Open your browser and go to the Comprehensive R Archive Network (CRAN) download page.
  2. Select a Mirror:
    • Choose a location close to you. This is where you’ll download R from.
  3. Download R for Windows:
    • Click on the “Download R for Windows” link.

    • Click on the “base” link on the next page.

    • Click on the link “Download R x.x.x for Windows” (where x.x.x will be the latest version number).

  4. Install R:
    • Locate the downloaded .exe file.

    • Double-click on it and follow the installation instructions.

Step 2: Installing RStudio

  1. Navigate to RStudio’s Website:

  2. Download RStudio:

    • Under the “Installers for Supported Platforms” section, click on the “RStudio x.x.x - Windows 10/11” link or the appropriate program for your operating system. There are options for Mac and Linux, too.
  3. Install RStudio:

    • Once the download is complete, locate the .exe file.
    • Double-click on it and follow the installation instructions.

Step 3: Renaming Files Extracted from Notion Using RStudio

  1. Open RStudio: After installation, locate the RStudio program and open it.
  2. Set Up the Working Directory:
    • On RStudio, go to the Console window.

    • Type setwd("C:/Users/ERosa/Desktop/anti-science-tracker") and hit Enter. This sets the working directory to where your extracted Notion files are.

  3. Run the Renaming Script:
    • Copy the provided R code from above.

    • In RStudio, open a new script window by going to File > New File > R Script.

    • Paste the copied R code into this new window.

    • Click on the Source button at the top-right of this script window to run the entire script.

After executing the script, the Notion files in your “anti-science-tracker” directory should be renamed according to the rules you’ve specified.

And that’s it! You have now successfully installed R and RStudio and renamed your files from the Notion export using R.

Code to Rename Markdown file

# Set the working directory to the location of the files
setwd("C:/Users/Username/Desktop/filename")

# List all md files in the directory
file_list <- list.files(pattern = "\\.md$")

# Function to extract the date from the content and reformat it to "YYYY-MM-DD"
extract_date <- function(file_content) {
  # Use regex to match the date pattern within the file content
  matches <- regmatches(file_content, regexpr("Date: ([A-Za-z]+ [0-9]+, [0-9]{4})", file_content))
  
  if(length(matches) > 0 && nchar(matches[1]) > 0) {
    date_string <- gsub("Date: ", "", matches[1])
    
    # Convert the string to a Date object
    date_obj <- as.Date(date_string, format = "%B %d, %Y")
    
    # Reformat the Date object to "YYYY-MM-DD"
    return(format(date_obj, "%Y-%m-%d"))
  } else {
    # Return "ND" if no date is found
    return("ND")
  }
}

# Function to rename files based on your requirements
rename_file <- function(file_path) {
  # Read the file content
  content <- readLines(file_path, warn = FALSE)
  
  # Extract the date from the content
  date <- extract_date(content)
  
  # Extract the main title excluding the alphanumeric string at the end
  main_title <- gsub(" [0-9a-f]{32}\\.md$", "", basename(file_path))
  
  # Create the new name by prepending the date and appending the extension
  new_name <- paste0(date, "_", main_title, ".md")
  
  # Rename the file
  file.rename(file_path, file.path(dirname(file_path), new_name))
}

# Apply the rename function to all files in the directory
sapply(file_list, function(file) rename_file(file.path(getwd(), file)))

Code to Rename Markdown Files and Associated Files

This renames the associated files for the .md file directly before them. This assumes the folder contents are in the order they were during extraction. If they have been reordered, this will almost certainly create a mess.

# Set the working directory
setwd("C:/Users/Username/Desktop/folder")

# List all files in the directory
all_files <- list.files()

# Function to extract the date from the md content
extract_date <- function(file_content) {
  matches <- regmatches(file_content, regexpr("Date: ([A-Za-z]+ [0-9]+, [0-9]{4})", file_content))
  if(length(matches) > 0 && nchar(matches[1]) > 0) {
    date_string <- gsub("Date: ", "", matches[1])
    date_obj <- as.Date(date_string, format = "%B %d, %Y")
    return(format(date_obj, "%Y-%m-%d"))
  } else {
    return("ND")
  }
}

# Placeholder for the desired new name of the most recently encountered .md file
last_md_name <- NULL

# Process each file in the directory
for (file in all_files) {
  # If it's an md file
  if (grepl("\\.md$", file)) {
    content <- readLines(file, warn = FALSE)
    date <- extract_date(content)
    main_title <- gsub(" [0-9a-f]{32}\\.md$", "", file)
    last_md_name <- paste0(date, "_", main_title)
    
    # Rename the .md file
    file.rename(file, paste0(last_md_name, ".md"))
  } else if (!is.null(last_md_name) && grepl("\\.(jpeg|jpg|mp4|png)$", file)) {
    # If it's a jpeg or mp4 file and we've encountered an md file earlier
    new_name <- paste0(last_md_name, ".", tools::file_ext(file))
    file.rename(file, new_name)
  }
  # For .csv and other file types, or before any md file has been encountered, do nothing
}

Citation

BibTeX citation:
@article{li2023,
  author = {Li, E. Rosalie},
  publisher = {Information Epidemiology Lab},
  title = {How to {Rename} {Exported} {Notion} {Files}},
  journal = {InfoEpi Lab},
  date = {2023-09-03},
  url = {https://infoepi.org/posts/2023/09/03-rename-notion-files.html},
  langid = {en}
}
For attribution, please cite this work as:
Li, E. Rosalie. 2023. “How to Rename Exported Notion Files.” InfoEpi Lab, September. https://infoepi.org/posts/2023/09/03-rename-notion-files.html.