![](/img/trans.png)
[英]Batch file to delete subfolders in directory if all files is older than 3 days
[英]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.