2013-03-19 85 views
2

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

这里的命令,我发现:

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 

对于文件不能使用,因为它删除长的路径和文件名,它被记录到长文件名和非全长文件名的TXT文件中,而非8dot3:

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 

这4个命令是w如果我只是复制并粘贴到dos命令提示符,请点击OK。

但是当我把它们放入批处理文件例如“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. 

任何原因?

我创建这个目标的目的是在30天内删除* .bak文件,而在长路径和文件名中没有任何问题。任何人都有这个简化的解决方案

回答

0

你会打你一巴掌。问题是在批处理文件中,for循环变量需要双重百分比。 %% A和%%〜sA。