【问题标题】:Batch Script doesn't delete the contents of the Temp folder with user input批处理脚本不会使用用户输入删除 Temp 文件夹的内容
【发布时间】:2020-12-06 08:30:14
【问题描述】:
@echo off

SET dir=%temp%
SET /p choice= Are you sure you want to delete your Temp Folder's contents? [y/n] 
IF /I "%choice%"=="y" (
    rmdir /s /Q %dir%
    mkdir %dir%
    Title Deleted Temp's contents
    echo Deleted Temp's contents
    timeout 2 > nul
    exit
)
IF /I "%choice%"=="n" (
    exit
) ELSE (exit)

我想自动删除 Temp 文件夹。 我在 Windows 10 版本 2004 上运行此批处理脚本

【问题讨论】:

  • 如果不是拼写错误,您需要在 if 语句中的 ==“y” 和右括号之间留一个空格。 ==“y” (。如果你根本不需要第二个。
  • 有些文件夹永远不要删除:%TEMP%引用的文件夹就是这样一个文件夹。永远不要完全删除它,只删除此文件夹中的文件和所有子文件夹。如果任何应用程序想要在文件夹中创建一个临时文件,该文件夹的路径被分配给环境变量PATH,而它当前被删除,则应用程序可能会因未定义的行为而失败。见How to delete files/subfolders in a specific directory at the command prompt in Windows?由于NTFS权限,也不建议删除%TEMP%
  • 不应删除和重新创建应始终存在的文件夹,因为这也会导致删除重要文件夹的所有者和权限集。重要文件夹的重新创建接下来可能会导致为重要文件夹设置不同的所有者或不同的权限,并且在删除和重新创建一个或多个帐户后对该文件夹的访问可能会有所不同。因此,对于临时文件,最好只删除重要文件夹中的文件和文件夹,而不是文件夹本身。
  • 莫菲是正确的。不要删除文件夹并重新创建它。您应该清空该文件夹。 (为什么?正如他所说,如果您删除并重新创建,所有者和权限都会丢失。)
  • 会发生什么?您尝试过什么调试问题?

标签: windows batch-file automation windows-10


【解决方案1】:

请改用choice,默认选项是Y/N。我也不会删除整个临时文件夹并重新创建,只需将其清除:

@echo off
choice /m "Are you sure you want to delete your Temp Folder's contents?"
if errorlevel 2 goto :eof
for /D %%i in ("%temp%\*") do rd "%%i" /S /Q
del "%temp%\*" /S /Q
Title Deleted Temp's contents
echo Deleted Temp's contents
timeout 2 > nul

【讨论】:

    【解决方案2】:

    Windows 无法删除当前工作目录,因此清空目录的最佳方法是在尝试删除之前将其设为当前目录。这将实质上删除所有允许的内容,而不会触及目录本身。 (注意:可能不会删除所有内容,只删除未锁定/未使用且您有足够权限删除的内容)

    @Echo Off
    SetLocal EnableExtensions
    
    Rem If the directory to empty, has no file content, end the script.
    "%__APPDIR__%where.exe" /Q /R "%TEMP%" * || (
        Echo %TEMP% has no file content.
        Echo Exiting . . .
        "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
        GoTo :EOF
    )
    
    Rem If the running script is in within, the directory to empty, end it.
    SetLocal EnableDelayedExpansion
    If /I Not "!__CD__:%TEMP%=!" == "%__CD__%" (
        Echo Cannot empty %TEMP% because this script is located within it.
        Echo Please move %~f0 to another location and try again.
        Echo Press any key to exit . . .
        "%__APPDIR__%timeout.exe" /T -1 1> NUL
        GoTo :EOF
    )
    EndLocal
    
    Rem Make the directory to empty, the current working directory.
    Rem If the directory to empty, cannot be found, end the script.
    PushD "%TEMP%" 2> NUL || (
        Echo The directory assigned to %%TEMP%% cannot be found.
        Echo Exiting . . .
        "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
        GoTo :EOF
    )
    
    Rem If the end user answers N, or n, end the script.
    "%__APPDIR__%choice.exe" /M "Are you sure you want to empty your Temp directory"
    If ErrorLevel 2 (
        Echo Exiting . . .
        "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
        GoTo :EOF
    )
    
    Rem Empty permissible content from the current directory.
    RD /S /Q . 2> NUL
    Title Cleaned %CD%.
    Echo Emptied permissible content from %CD%
    
    Rem Make the previously current directory, current again.
    PopD
    
    Echo Press any key to exit . . .
    "%__APPDIR__%timeout.exe" /T -1 1> NUL
    GoTo :EOF
    

    上面的代码可能比其他答案有更多的内容,但它显示了您在创建脚本时应该尝试遵循的逻辑进程,尤其是那些正在删除文件和目录内容的脚本。

    如果您不希望代码结构经过深思熟虑,那么您可以这样做:

    @CD /D "%TEMP%" 2>NUL||Exit /B
    @"%__APPDIR__%choice.exe" /M "Are you sure you want to empty your Temp directory"
    @If ErrorLevel 2 Exit /B
    @RD /S /Q . 2>NUL
    @Title Cleaned %CD%.
    @Echo Emptied permissible content from %CD%
    @"%__APPDIR__%timeout.exe" /T 2 1>NUL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多