# Set the working directory to the location of the files
setwd("C:/Users/Username/Desktop/filename")
# List all md files in the directory
<- list.files(pattern = "\\.md$")
file_list
# Function to extract the date from the content and reformat it to "YYYY-MM-DD"
<- function(file_content) {
extract_date # Use regex to match the date pattern within the file content
<- regmatches(file_content, regexpr("Date: ([A-Za-z]+ [0-9]+, [0-9]{4})", file_content))
matches
if(length(matches) > 0 && nchar(matches[1]) > 0) {
<- gsub("Date: ", "", matches[1])
date_string
# Convert the string to a Date object
<- as.Date(date_string, format = "%B %d, %Y")
date_obj
# 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
<- function(file_path) {
rename_file # Read the file content
<- readLines(file_path, warn = FALSE)
content
# Extract the date from the content
<- extract_date(content)
date
# Extract the main title excluding the alphanumeric string at the end
<- gsub(" [0-9a-f]{32}\\.md$", "", basename(file_path))
main_title
# Create the new name by prepending the date and appending the extension
<- paste0(date, "_", main_title, ".md")
new_name
# 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)))
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.
Skip to code to rename markdown files.
Skip to code to rename files associated with the exported markdown files.
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:
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.
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
- Open Your Browser: Navigate to the official 7-Zip website.
- 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.
- 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.
- 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
- Locate the Notion Export: Navigate to the folder where your exported Notion file (probably a
.zip
file) is saved. - Right-click on the File: A dropdown menu will appear.
- Select 7-Zip from the Menu: Hovering over it will show you a submenu.
- 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.
- If you choose “
- 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.
- 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.
- If you want to view the content without extracting, right-click on the Notion exported
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
- Navigate to CRAN:
- Open your browser and go to the Comprehensive R Archive Network (CRAN) download page.
- Select a Mirror:
- Choose a location close to you. This is where you’ll download R from.
- 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).
- Install R:
Locate the downloaded
.exe
file.Double-click on it and follow the installation instructions.
Step 2: Installing RStudio
Navigate to RStudio’s Website:
- Go to the RStudio download page.
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.
Install RStudio:
- Once the download is complete, locate the
.exe
file. - Double-click on it and follow the installation instructions.
- Once the download is complete, locate the
Step 3: Renaming Files Extracted from Notion Using RStudio
- Open RStudio: After installation, locate the RStudio program and open it.
- Set Up the Working Directory:
On RStudio, go to the
Console
window.Type
setwd("C:/Users/ERosa/Desktop/anti-science-tracker")
and hitEnter
. This sets the working directory to where your extracted Notion files are.
- 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
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
<- list.files()
all_files
# Function to extract the date from the md content
<- function(file_content) {
extract_date <- regmatches(file_content, regexpr("Date: ([A-Za-z]+ [0-9]+, [0-9]{4})", file_content))
matches if(length(matches) > 0 && nchar(matches[1]) > 0) {
<- gsub("Date: ", "", matches[1])
date_string <- as.Date(date_string, format = "%B %d, %Y")
date_obj return(format(date_obj, "%Y-%m-%d"))
else {
} return("ND")
}
}
# Placeholder for the desired new name of the most recently encountered .md file
<- NULL
last_md_name
# Process each file in the directory
for (file in all_files) {
# If it's an md file
if (grepl("\\.md$", file)) {
<- readLines(file, warn = FALSE)
content <- extract_date(content)
date <- gsub(" [0-9a-f]{32}\\.md$", "", file)
main_title <- paste0(date, "_", main_title)
last_md_name
# 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
<- paste0(last_md_name, ".", tools::file_ext(file))
new_name file.rename(file, new_name)
}# For .csv and other file types, or before any md file has been encountered, do nothing
}
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}
}