2013-03-19 92 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。