【问题标题】:Delete file longer then 30 days old for long path and long file name对于长路径和长文件名,删除超过 30 天的文件
【发布时间】:2013-03-19 08:08:22
【问题描述】:

我尝试使用每月一次的任务计划在 Windows 中制作批处理文件以查找 *.bak。条件超过 30 岁。我创建了两个条件完整路径名和非 8dot3 路径名。对于那些无法删除的人,将其记录到 TXT 文件中。

这里是我找到的命令:

forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C del @path"

这是我使用非 8dot3 文件名删除的命令:

forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C for %A in (@path) do @echo del %~sA

对于文件因路径长、文件名长而无法删除的文件,记录为全路径文件名长且非8dot3的TXT文件:

forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c echo @path >> list.txt"
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c for %A in (@path) do @echo %~sA" >> list.txt

如果我只是复制并粘贴到 dos 命令提示符中,这 4 个命令运行良好。

但是当我将它们放入批处理文件时,例如“del30days.bat”,它不适用于非 8dot3 文件名:

@echo off
:: Set host computer name
set host=%COMPUTERNAME%

:: Set save list path
set list_path=F:\LogFiles

:: Set min age of files and folders to delete
set file_list=ListCantDeleted-%host%-%date:~10,4%-%date:~7,2%-%date:~4,2%.txt

:: Set target folder path
set target_path=E:\WP

:: Set min age of files and folders to delete
set max_days=30

:: Set what kind files or extension
set file_ext=*.bak

:: Delete files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C del @path"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (@path) do @echo del %~sA"


:: Record files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /c echo @path >> %list_path%\%file_list%"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (@path) do @echo %~sA >> %list_path%\%file_list%"

我在非 8dot3 文件名上收到此错误,但在 TXT 文件中记录的长文件名上却没有,这意味着它也无法删除使用非 8dot3 文件名的文件。

~sA" was unexpected at this time.

有什么原因吗?

我创建此文件的目标是删除 *.bak 文件超过 30 天,而不会出现长路径和文件名问题。有人对此有简化的解决方案吗?

【问题讨论】:

    标签: windows command-line batch-file scheduled-tasks


    【解决方案1】:

    你会打自己的耳光。问题是在批处理文件中,for 循环变量需要双倍百分比。 %%A 和 %%~sA。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2019-09-19
      • 2018-02-22
      • 2018-12-16
      相关资源
      最近更新 更多