分享一个强制删除文件脚本

最近公司的同事遇到好几起邮箱下载的文件打不开也删除不了,只能在PE模式下用工具删除,鼓捣了一会儿写了一个强制删除的脚本,把文件拖到脚本上就可以强制删除了。创建bat文件,并粘贴进去,需要的可以试试。

@echo off
set "target=%~1"

:: Check if user dragged a file onto the script
if "%target%"=="" (
    echo.
    echo ========================================================
    echo  ERROR: Please drag and drop a file or folder here.
    echo ========================================================
    echo.
    pause
    exit /b
)

echo.
echo --------------------------------------------------------
echo  Attempting to Force Delete: "%target%"
echo --------------------------------------------------------

:: 1. Try to delete as a FILE (Using \\?\ to bypass path limits)
:: /F: Force /A: Select All Attributes /Q: Quiet
DEL /F /A /Q "\\?\%target%" >nul 2>&1

:: 2. Try to delete as a FOLDER (Using \\?\ to bypass path limits)
:: /S: Subdirectories /Q: Quiet
RD /S /Q "\\?\%target%" >nul 2>&1

:: 3. Check Result
echo.
IF EXIST "%target%" (
    echo  [FAILED] Could not delete the item.
    echo  Reason: The file might be in use or requires Admin rights.
) ELSE (
    echo  [SUCCESS] Item deleted successfully.
)

echo.
pause