2014-10-31 84 views

回答

2

寫文件下的test.bat:

set delFileList=%* 
FOR %%A IN (%delFileList%) DO (
    DEL %%A 
    echo file %%A deleted 
) 
+0

這是一個很好的第一次嘗試,但如果它試圖刪除一個只讀文件,它不會表現良好。另外,如果它支持許多標準的DEL選項,它將會很好。 – dbenham 2014-10-31 18:05:50

2

警告 -該解決方案確實確認提示,如果用戶試圖刪除整個目錄。請注意!

下面是LISTDEL.BAT,它支持大多數正常的DEL選項。

我相信LISTDEL.BAT非常強大。我能想到的唯一限制是,如果共享相同%TEMP%文件夾的兩個進程嘗試在幾乎同一時間運行實用程序,它可能會失敗。代碼被廣泛記錄在案。

::: 
:::LISTDEL.BAT [/?] [/F] [/S] [/A[[:]attributes]] Names 
::: 
::: Deletes one or more files in much the same way as DEL, except each 
::: deleted file is listed. 
::: 
::: Unlike DEL, LISTDEL never prompts for confirmation of global wildcard. 
::: Also, the /P option is not supported. 
::: 
::: names   Specifies a list of one or more files or directories. 
:::     Wildcards may be used to delete multiple files. If a 
:::     directory is specified, all files within the directory 
:::     will be deleted. 
::: 
::: /?   Display this help information 
::: 
::: /F   Force deleting of read-only files. 
::: 
::: /S   Delete specified files from all subdirectories. 
::: 
::: /A   Selects files to delete based on attributes 
::: attributes R Read-only files   S System files 
:::     H Hidden files    A Files ready for archiving 
:::     I Not content indexed Files L Reparse Points 
:::     - Prefix meaning not 
::: 
::: LISTDEL.BAT was written by Dave Benham and originally posted at: 
::: http://stackoverflow.com/a/26680846/1012053 
::: 
@echo off 
setlocal disableDelayedExpansion 

:: Print help if /? used 
echo(%*|findstr /ir "\</\?\>" >nul && (
    for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do echo(%%A 
    exit /b 0 
) 

:: Fail if /P option used 
echo(%*|findstr /ir "\</P\>" >nul && (
    echo Invalid switch - "P".>&2 
    exit /b 0 
) 

:: Determine if /F option was used and save value 
echo(%*|findstr /ir "\</F\>" >nul && set "force=/f" || set "force=" 

:: Determine length of the delete prompt that varies by language. 
:: The English prompt is ", Delete (Y/N)? ", which results in -16. 
set "tempFile=%temp%\showDel.tmp" 
for %%F in ("%tempfile%") do >"%tempFile%" echo %%~fF 
del /p "%tempFile%" <nul >"%tempFile%2" 
for %%A in ("%tempFile%") do for %%B in ("%tempFile%2") do set /a "promptLen=%%~zA-%%~zB" 
del "%tempFile%" "%tempFile%2" 

:: Iterate list of files to be deleted via DEL /P /Q plus user supplied arguments. 
:: No files are deleted because stdin is disabled - only a listing is produced. 
:: Each listed file contains an unwanted delete prompt at the end. 
for /f "delims=" %%F in ('del /p /q %* ^<nul') do (

    %= Get the full path to the file by stripping off the trailing delete prompt =% 
    %= Toggle delayed expansion on and off to protect ! that may be in the path =% 
    set "file=%%F" 
    setlocal enableDelayedExpansion 
    set "file=!file:~0,%promptLen%!" 

    %= Attempt to delete the file and pipe any error message to FINDSTR. =% 
    %= Redirect FINDSTR output (any found error message) back to stderr. =% 
    %= If error message found, then inform what file coudn't be deleted, =% 
    %= else list the deleted file.           =% 
    %= File attribute restrictions have already been applied in outer loop, =% 
    %= so DEL /A option is used to disregard file attributes.    =% 
    del %force% /a "!file!" 2>&1 >nul | findstr . >&2 && >&2 echo Unable to delete !file!||echo !file! 
    endlocal 
) 
exit /b 0 
+0

編輯 - 添加幫助設施,並解除/ P選項。 – dbenham 2014-10-31 19:11:16