Created
February 15, 2021 19:04
-
-
Save stefanzweifel/f24505bacc6ebf9f59a478ab992e8a5a to your computer and use it in GitHub Desktop.
A PHP script to rename a collection of Zettelkasten notes into a different format. I've used this script to rename my notes, so that the timestamp/ID is always at the front.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/collections": "^8.26", | |
"illuminate/support": "^8.26", | |
"symfony/var-dumper": "^5.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Str; | |
require_once 'vendor/autoload.php'; | |
$pattern = "/([A-z]*.*)(\ )(\d{1,14})(\.md)$/"; | |
# Absolute path to where your notes are stored | |
$pathToNotes = '/Users/username/Library/Mobile Documents/iCloud~co~fluder~fsnotes/Documents/'; | |
$directoryIterator = collect(new FilesystemIterator($pathToNotes)) | |
->reject(function (SplFileInfo $fileInfo) { | |
return $fileInfo->isDir(); | |
}) | |
->filter(function (SplFileInfo $fileInfo) { | |
return $fileInfo->getExtension() == 'md'; | |
}) | |
->filter(function (SplFileInfo $fileInfo) use ($pattern) { | |
preg_match($pattern, $fileInfo->getFilename(), $matches); | |
if (count($matches) == 0) { | |
return false; | |
} | |
[, , , $timestamp, ] = $matches; | |
return $timestamp !== null; | |
}) | |
->each(function (SplFileInfo $fileInfo) use ($pathToNotes) { | |
preg_match("/([A-z]*.*)(\ )(\d{1,14})(\.md)$/", $fileInfo->getFilename(), $matches); | |
[, $title, , $timestamp, $extension] = $matches; | |
# Update the order of the variables to what you want. | |
$newFileName = "{$timestamp} {$title}{$extension}"; | |
var_dump( | |
$fileInfo->getFilename(), | |
$newFileName, | |
'-------' | |
); | |
rename("{$pathToNotes}{$fileInfo->getFilename()}", "{$pathToNotes}{$newFileName}"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment