簡體   English   中英

如何創建一個批處理腳本來刪除早於 X 的文件而不是在根文件夾中刪除並排除某些子文件夾?

[英]How to create a batch script that deletes files older than X and not delete at the root folder and exclude certain subfolders?

更新:我想通了,答案如下:-)

如何創建一個批處理腳本來刪除早於 X 的文件,而不是在根文件夾中刪除並排除某些子文件夾?

到目前為止,我有這個,但它刪除了C:\Temp\TestFolder\根目錄中的文件,我只希望它刪除子目錄中的文件。 我還想排除某些我知道可以放入exclude.txt文件的子目錄,但我不確定執行此操作的代碼是什么。

當前批處理腳本:

echo deleting files...
forfiles /p C:\Temp\TestFolder\ /s /m *.* /D -30 /c "cmd /c echo /f /q @path"
echo finished deleting files!
pause

我剛剛完成了代碼(也許它仍然會幫助你。)

powershell 代碼為:

# pre-sets
$place = "C:\test"
$exclude_name = "C:\test\exclude.txt"
$wipe_list = "C:\test\wipe_list.txt"
$time = 5

#go to the root
cd $place

#get a list of alll folders in the root
$folders = Get-ChildItem -Directory

#get the excluded files
$exclude = get-content $exclude_name

#take the first line of the list of folders
ForEach ($line in $($folders -split "`r`n")){
    #go threw each line in the list of excluded items
    ForEach ($linex in $($exclude -split "`r`n")) {
    #if it finds a file in the directory that isnt equal to a exclusion file it will write it to $wipe_list
    if ("$line" -ne "$linex") {
        $line | Out-File -filepath $wipe_list -append
    }
}}
#clearing some variables
$exclude_nama = ""
$folders = ""
$exclude = ""
$line = ""
$linex = ""
#setting folders equal to all the folders to check and wipe
$folders = get-content $wipe_list
#read the list of folders 1 by 1
ForEach ($line in $($folders -split "`r`n")){
#go to the root
cd $place
#go to the sub folder
cd $line
#make a list of all files
$folder_list = dir
#split the list into single files
ForEach ($linex in $($folder_list -split "`r`n")){
#check how old the file is
$lastWrite = (get-item "$linex").LastWriteTime
$timespan = new-timespan -days $time 
if (((get-date) - $lastWrite) -gt $timespan) {
#delete the file if its older than $time
Remove-Item –path "$linex" –recurse -force
}
}
}
#clearing more variables
$time = ""
$timespan = ""
$linex = ""
$place = ""
$line = ""
$folders = ""
$folder_list = ""
#deleting the temp file
Remove-Item –path "$wipe_list"
#clearing the last variable
$wipe_list = ""
#closing the window
exit

批處理文件代碼為:

@echo off
echo Starting ...
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\test\check_and_wipe.ps1'"
echo Done...
ping localhost >nul
exit

查看您的代碼很可能是更好的選擇,但如果您想使用它,請替換批處理腳本中的預設和文件路徑。

這是一個未經測試的示例,供您運行、玩耍和嘗試理解。 只需根據需要替換您的源目錄和排除目錄的名稱(請注意包含My Files的雙引號,因為它使用空格/毒字符)。

@Echo Off
SetLocal EnableExtensions
Set "source=C:\Temp\TestFolder"
Set "xclude="My Files" Backups"
For /F Tokens^=* %%G In ('%__AppDir__%Robocopy.exe "%source%" "%source%"^
 /IS /L /MinAge:30 /NC /NDL /NJH /NJS /NS /S /XD %xclude% ^| ^
 %__AppDir__%\findstr.exe /VRI "%source:\=[\\]%[\\][^\\]*$"'
) Do Del /A /F "%%G"

由於您沒有對您在問題中提出的任務做出合理的嘗試,因此我不會提供解釋或發布支持。

更新(2021 年 1 月 8 日):這在 2 個子目錄深處不起作用。 :(

耶! 我自己想通了!

感謝@smvd 抽出寶貴時間。 順便說一句 @Compo 我不能使用 robocopy - 我的客戶空間不足,現在會導致問題。 不過謝謝!

for /F "delims=" %%D in ('dir /B /S /A:D "C:\Temp\TestFolder\" ^| findstr /I /V /L /G:"C:\Temp\TestFolder\exclude.txt"') DO (
    forfiles /p %%D /D -30 /c "cmd /c del @file" >nul 2>nul
    IF ERRORLEVEL 1 (
        ECHO No %%D files 30 days old or older were found.
    ) ELSE (
        ECHO %%D files as old as 30 days or older have been deleted.
    )
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM