使用了DOS command forfiles方式批次查找檔案的修改日期
如此一來當目錄下有幾千個檔案都不用手動排序找了
一鍵幫我複製到新目錄,省時省力😀
另一個常使用的情境是
系統常常產生大量的Log記錄檔
也可以用forfiles方式刪除超過30天的檔案將空間釋放
### 範例如下 ###
將30天前的檔案刪除
forfiles /p D:\test\ /s /m *.csv /d -30 /c "cmd /c del @file /q"
將30天前修改過的csv檔案寫入文字檔outfile.txt
forfiles /p D:\test\ /s /m *.csv /d -30 /c "cmd /c dir @path /b/s" > "D:\test_cp\outfile.txt"
/p <pathname> : 搜尋的目錄位置
/s : 以遞迴方式搜尋子目錄
/m <filename> : 搜尋內容
/d : 在指定時間範圍內最後修改日期的檔案
/c <command> : 在每個檔案上執行指定的命令
複製csv檔案到新目錄,並排除outfile.txt內的清單
xcopy "D:\test\*.csv" "D:\test_cp\file\" /d /y /EXCLUDE:D:\test_cp\outfile.txt
xcopy <Source> <Destination>
/d : 如果沒有指定日期,只複製來源檔案時間比目的地時間新的檔案
/y : 隱藏提示
/EXCLUDE:<file> : 排除的檔案 (本例改用文字檔表列清單)
### 最後batch檔案內容如下 ###
@echo off rem 將指定目錄的30天內的檔案及資料夾copy到指定目錄下
set "src=d:\test\" rem 源目錄路徑 del /q d:\test_cp\outfile.txt rem 刪除舊清單 del /q d:\test_cp\file\*.csv rem 刪除舊檔
forfiles /p %src% /m *.csv /d -30 /c "cmd /c dir @path /b/s" > "d:\test_cp\outfile.txt" xcopy "%src%*.csv" "d:\test_cp\file\" /d /y /EXCLUDE:d:\test_cp\outfile.txt |