Skip to content

Instantly share code, notes, and snippets.

@appliedintelligencelab
Created September 26, 2023 12:14
Show Gist options
  • Save appliedintelligencelab/c02cd9d831fcb7144b645bcd59add6ab to your computer and use it in GitHub Desktop.
Save appliedintelligencelab/c02cd9d831fcb7144b645bcd59add6ab to your computer and use it in GitHub Desktop.
Batch file to delete every nth file matching the spec
@echo off
::: ndel - Deletes every nth file matching the spec
::: syntax: ndel.bat FILESPEC [skipcount] [sortorder]
::: FILESPEC - Files to be searched through
::: skipcount - number of files to skip (optional - default 3)
::: sortorder - File order (see: DIR for options - default N (name))
:: With no arguments show the above usage text
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
set find=%1
set evry=4
set ord=n
if "%~2" neq "" set /a evry=(%2+1)
if "%~3" neq "" set ord=%3
set count=0
for /f %%f IN ('dir %find% /b /o%%ord%%') do (
call :test_file "%%f"
)
GOTO:eof
:test_file
set /a _r="%count% %% %evry%"
if not %_r%==0 del %1
set /a count+=1
GOTO:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment