简体   繁体   中英

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

This is an example of what i'm trying to achieve

c:/test Contains

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

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. Could someone please assist I can write the base process in Java but am slightly unfamiliar with batch scripting. 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

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

@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:

@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.


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. You can use that to get rid of the hard-coded values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM