本站正式启用一月有余,随着文章越写越多,vs code 编辑器左侧的工作区文件浏览令我十分困惑,文章的顺序好像不是按日期排序的,好像是按字母排序。一眼望去不知道先后顺序和写作日期。如图:
这个问题我咨询了deepseek,deepseek给出了一个有用的办法,在文件名前加上日期,比如这样:
content/posts/
├── 2023-10-01-post-title-a.md
├── 2023-10-05-post-title-b.md
└── 2023-10-10-post-title-c.md
我感觉十分有用,用脚本把posts目录下的文件名全部加了日期,以下内容使用保存为文本文件,扩展名为.ps1。
# 进入 content/posts 目录
cd content/posts
# 遍历所有 .md 文件,读取 Front Matter 中的日期,重命名文件
Get-ChildItem *.md | ForEach-Object {
$content = Get-Content $_.FullName
$dateLine = $content | Where-Object { $_ -match '^date:\s*(.*)' }
if ($dateLine) {
$dateStr = [datetime]::ParseExact($matches[1].Trim(), "yyyy-MM-ddTHH:mm:sszzz", $null)
$newName = "{0:yyyy-MM-dd}-{1}" -f $dateStr, $_.Name
Rename-Item $_ $newName
}
}
但是powershell不让运行脚本怎么办?那就复制以下代码粘贴到powershell中吧。
Get-ChildItem *.md | ForEach-Object {
$content = Get-Content $_.FullName;
$dateLine = $content | Where-Object { $_ -match '^date:\s*(.*)' };
if ($dateLine) {
$dateStr = [datetime]::ParseExact($matches[1].Trim(), "yyyy-MM-ddTHH:mm:sszzz", $null);
$newName = "{0:yyyy-MM-dd}-{1}" -f $dateStr, $_.Name;
Rename-Item $_.FullName -NewName $newName
}
}