[英]Batch Script To Delete Files Within a Directory After A Certain Count Value
I am currently looking to implement a small script to browse through a directory and delete all files/folders after a certain count value in this case ill use 3 我目前正在寻求实现一个小的脚本,以浏览目录并在达到一定计数值后删除所有文件/文件夹,在这种情况下,请使用3
This is an example of what i'm trying to achieve 这是我正在努力实现的一个例子
c:/test Contains c:/ test包含
I am not very familiar with batch scripting but believe the process will require a for iteration that adds 1 to a count variable for every file (of any type) in the directory and when the count reaches 3 it will begin to delete files after that point. 我对批处理脚本不是很熟悉,但是相信该过程将需要for迭代,该迭代将目录中每个文件(任何类型)的计数变量加1,并且当计数达到3时,它将在该点之后开始删除文件。 Could someone please assist I can write the base process in Java but am slightly unfamiliar with batch scripting.
有人可以帮忙吗,我可以用Java编写基本流程,但对批处理脚本有点不熟悉。 Could someone please assist?
有人可以帮忙吗?
Thanks 谢谢
There is no need for a counter 无需柜台
@echo off
setlocal enableextensions disabledelayedexpansion
rem How many elements to keep
set "keep=3"
rem Retrieve folder from batch arguments
set "folder=%~1"
rem If no folder indicated, use current active directory
if not defined folder for %%a in (.) do set "folder=%%~fa"
rem Ensure we are working in the correct folder
pushd "%folder%" && (
rem For each element in the folder, skipping the first n
for /f "skip=%keep% delims=" %%a in (' dir /b /on ') do (
rem If it is a folder, use rmdir, else use del
if exist "%%a\" ( echo rmdir /s /q "%%a" ) else ( echo del "%%a" )
)
rem Work done. Return to previous active directory
popd
) || (
rem Folder change failed
echo Target folder does no exist
)
File/folder removal operations are only echoed to console. 文件/文件夹删除操作仅回显到控制台。 If the output is correct, remove the
echo
commands that precede rmdir
and del
如果输出正确,请除去
rmdir
和del
之前的echo
命令
Not tested.Change the directory , number and etc variables at the beginning of the file.And its ok delete the echo
at the last line 不tested.Change目录,数量和等变量在file.And之初其确定删除
echo
最后一行
@echo off
set "start_deleting_from=4"
set "directory=c:\bak_files"
set "extension=.bak"
set counter=0
pushd "%directory%"
setlocal enableDelayedExpansion
for %%# in (%extension%) do (
set /a counter=counter+1
if !counter! GEQ !start_deleting_from! (
rem delete "echo" if to activate deletion
echo del /q /f file!counter!!extension!
)
)
So if the requirement is "when alphabetically sorted, keep the top three of the *.bak
files in the folder and delete the rest" , the batch file would be: 因此,如果要求是“按字母顺序排序,请将
*.bak
文件的前三个保留在该文件夹中,然后删除其余三个” ,则批处理文件将为:
@echo off
setlocal EnableDelayedExpansion
pushd C:\test
set top=3
set filespec=*.bak
set counter=0
for /f "delims=" %%f in ('dir /b %filespec%') do (
set /a counter+=1
if !counter! GTR %top% del /q "%%f"
)
You can use the various options of the dir
command to achieve different orders or select different files. 您可以使用
dir
命令的各种选项来实现不同的顺序或选择不同的文件。
You could use also use command line arguments to configure the script. 您还可以使用命令行参数来配置脚本。 Assuming you call the script like this:
假设您这样调用脚本:
delete_after.bat 3 *.bak
then 3
and *.bak
are available inside the batch file as %1
and %2
, respectively. 那么批处理文件中的
3
和*.bak
分别作为%1
和%2
可用。 You can use that to get rid of the hard-coded values. 您可以使用它来摆脱硬编码的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.