简体   繁体   English

批处理脚本以删除特定计数值后目录中的文件

[英]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包含

  1. file1.bak file1.bak
  2. file2.bak file2.bak
  3. file3.bak file3.bak
  4. file4.bak (delete) file4.bak(删除)
  5. file5.bak (delete) file5.bak(删除)
  6. file6.bak (delete) file6.bak(删除)

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 如果输出正确,请除去rmdirdel之前的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.

相关问题 在特定计数值后自动删除子文件夹中的文件 - Auto delete files in subfolders after a certain count value 批处理文件在文件夹中经过一定时间后删除文件? - Batch file to delete files after a certain amount of time in folder? 我需要一个批处理脚本,该脚本可以删除目录中的所有文件夹,但不包括某些文件夹 - I need a batch script that can delete all folders inside a directory, EXCLUDING certain folders VB脚本删除某些文件,如果找到文件,则将其他文件复制到目录 - VB Script to delete certain files and if files are found copy other files to directory 包含某些字符的批处理脚本删除文件 - batch script delete file containining certain chars 如何创建一个批处理脚本来删除早于 X 的文件而不是在根文件夹中删除并排除某些子文件夹? - How to create a batch script that deletes files older than X and not delete at the root folder and exclude certain subfolders? 使用批处理脚本的当前目录中文件,文件夹和子文件夹的总数 - Total count of files,folders and sub folders in the current directory using batch script 如何使用批处理文件从同一目录中删除目录? - How to delete a directory from within that same directory with a batch file? Windows批处理脚本以搜索要删除的特定文件 - Windows batch script to search for specific files to delete 批处理脚本,用于删除基于findstr正则表达式的文件 - Batch Script to delete files based on findstr regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM