删除 Windows 保留名称文件指南
问题描述
在 Windows 系统中,以下名称是系统保留的设备名称,无法通过常规方式创建或删除:
nul- 空设备con- 控制台aux- 辅助设备prn- 打印机com1~com9- 串行端口lpt1~lpt9- 并行端口
当项目中意外出现这些文件时,使用普通的删除命令会失败。
解决方案
方法一:使用 UNC 路径前缀(推荐)
使用 \\?\ 前缀可以绕过 Windows 的保留名称检查:
PowerShell:
powershell
Remove-Item -LiteralPath "\\?\D:\项目路径\nul" -Force
CMD:
cmd
del "\\?\D:\项目路径\nul"
方法二:使用 Git 命令
如果文件在 Git 仓库中:
bash
git rm --cached nul
git commit -m "删除保留名称文件"
方法三:重命名后删除
先重命名再删除:
powershell
# 重命名
Rename-Item -LiteralPath "\\?\D:\项目路径\nul" -NewName "nul_temp"
# 删除
Remove-Item "D:\项目路径\nul_temp"
预防措施
在 .gitignore 中添加这些保留名称,防止意外提交:
gitignore
# Windows 保留名称
nul
con
aux
prn
com[1-9]
lpt[1-9]