如何为Del命令制作一个批处理文件,但要详细说明



有人能帮我制作一个批处理文件,说明它在命令行中删除的内容吗?

我是一个批处理初学者,以前从未这样做过。

我知道del没有/V命令,所以我认为批次可能是我的答案。

有人能帮忙吗?

编写一个文件test.bat:

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

警告- 如果您试图删除整个目录,此解决方案不会提示确认。请小心!

下面是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

相关内容

最新更新