如何删除带有尾随空格(或句点)字符的Windows文件?
如何删除意外创建的名称为“Call”的零字节文件?
我正面临这个问题,直到现在我才弄明白?
我尝试使用命令Del但没有机会。
@echo off
set "targetdir=C:FolderDir"
for /f "delims=" %%a in ('dir/s/b/a-d "%targetdir%*.*"') do ( if %%~Za equ 0 echo %%~na>>results.txt && del "%%~a" )
我还尝试删除包含此文件“Call”的目录
RD /S /Q [ParentFolder with file Call Inside]
我也尝试过使用 Powershell:
Get-ChildItem -Path "C:MyFolderDir" -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
编辑 :
我在他的评论中执行@Compo 在下面提供的这个命令:
%SystemRoot%System32where.exe /T /F "C:FolderDir":"*call*"
我得到了这样的结果:
? ? ? "C:UsersHackooDesktopScriptingCall "
回答
The Windows underlying file system does support file or directory names which end with a space or a period, so it is possible to create them. However the Windows Win32 file namespace API functions as used with shell applications do not. In your case, based upon your provided information, I determined that your actual filename was not Call but Call .
To clarify that there was a trailing whitespace character, I asked you to check your filename using the where.exe utility, which using its /F option, would display your filepath double-quoted. I additionally included the /T option which would confirm the files' size and modified date details.
%SystemRoot%System32where.exe /T /F "C:UsersHackooDesktopScripting":"*call*"
根据您提供的说明,我的假设是正确的,您只需要一种删除文件的方法,绕过 API 的字符串解析。
您可以在此处阅读有关文件、路径和命名空间的 Windows 规则的所有信息。
正如在上述链接的信息中所指出的,解决您的问题的方法是使用?前缀来绕过该强制执行。
Del /A /F "?C:UsersHackooDesktopScriptingCall "
在您的情况下,如您的文件属性所述,-a----如另一条评论所示,您实际上并不需要/Aor/F选项,因此以下内容就足够了:
Del "?C:UsersHackooDesktopScriptingCall "
为了完整起见,您可以在powershell 中做同样的事情:
Remove-Item -LiteralPath "?C:UsersHackooDesktopScriptingCall "