批处理文件,用于删除超过210天的C:\ users中的用户文件夹

时间:2015-07-28 13:32:50

标签: batch-file

我正在尝试编写一个批处理文件,用于删除/删除超过210天的C:\ users中的不同用户文件夹。

我试过这个 forfiles / p C:\ users / d -210 / c“cmd / c del @file”

但是,此命令会删除当前用户的快捷方式以及这些快捷方式超过210天。

如果内容的上次​​修改日期超过210天,我是否可以将批处理文件仅删除整个文件夹

我也可以指定要删除的文件夹的起始名称(用户以PDC开头),因为我担心它可能会删除计算机中一些重要的隐藏文档,因为这个批处理文件将在管理员上运行帐户。

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

First run robocopy in list-only mode and if there were no files newer than the specified age proceed with the actual delete operation:

@echo off
set AGE=2
set EXCLUDE="Public" "admin1" "admin2"
for /d %%D in (C:\users\*) do (
    echo %EXCLUDE% | find /i """%%~nD""" >nul
    if errorlevel 1 (
        set too_new=0
        robocopy "%%D" "%%D" /L /v /s /xjd /minage:%AGE% | findstr /r /c:"^. *too new" >nul
        if errorlevel 1 (
            echo Deleting %AGE% days old %%D
            rd /s /q "%%D"
        )
    )
)
pause

Don't forget to do a test run first by commenting out the rd line: put rem and a space before it.

Notes:

  • /L = list-only mode, no copying performed
  • /V = verbose mode, report files skipped for various reasons, e.g. the "too new" we look for
  • /S = look in subdirectories
  • /xjd = don't follow junction points for directories, useful if those point to an unavailable device
  • ^. *too new regular expression in findstr lists lines beginning with a tab character followed by lots of spaces and then too new.
相关问题