0

I understand that there are tons of questions on this site regarding the creation of a batch file that goes through the file in a specified folder and deletes them if it satisfies the condition stated.

However, I would like to tweak that a little bit. In my batch file, I would like to look at a folder, say C:\Dev and get all the files that are within the same month. After getting all those files, I want to sort through all the dates and delete everything except for the latest one. So if I have 5 files for January on that folder with dates January 1, 12, 20, 27, and 30, I would only keep the file dated January 30th and delete all the others.

Is this possible?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Smiley
  • 3,207
  • 13
  • 49
  • 66
  • possible duplicate of [Batch file that keeps the 7 latest files in a folder](http://stackoverflow.com/questions/13367746/batch-file-that-keeps-the-7-latest-files-in-a-folder) – Ken White Jan 17 '14 at 22:39
  • Then look at one of the other answers, like the one I've linked, and make an effort to "tweak it" yourself. You can't learn if you depend on everyone else to give you the code. :-) (The one I linked deletes all but the "latest 7", and it should be pretty easy to figure out how to modify it to "latest 1".) There are dozens of other similar questions here; I found that one by simply looking through the list of **Related** posts to the right of your question. =====>>>>> – Ken White Jan 17 '14 at 22:40
  • ohhhh. i'm sorry! i didn't see this one! but thanks. yeah, looks pretty easy to tweak it. just have to figure out how to sort the files by month and do the deletion from there! thanks a lot! appreciate it! – Smiley Jan 17 '14 at 23:04

1 Answers1

2

< lang-dos -->

@ECHO OFF
SETLOCAL
SET "targetdir=c:\sourcedir"
SET "pfname="
PUSHD "%targetdir%"
FOR /f "delims=" %%a IN ('dir /b /a-d /o:d "*" ') DO (
  SET "fname=%%a"
  SET "fdate=%%~ta"
  CALL :process
)
POPD

GOTO :EOF
:process
:: reformat date - this depends on yout local date-format.
:: YY(YY)MM required - my format is dd/mm/yyyy
SET fdate=%fdate:~6,4%%fdate:~3,2%
IF NOT DEFINED pfname GOTO nodel
IF %fdate%==%pfdate% ECHO DEL "%targetdir%\%pfname%"
:nodel
SET pfdate=%fdate%
SET "pfname=%fname%"
GOTO :eof

This should work for you. The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.

First, the target directory is set up and pfname is cleared.

The PUSHD changes the current directory until the POPD is executed

the dir command outputs filenames only (/b), no directory names (/a-d) in date-order (/o:d). Each line sets fname to the filename and fdate to the filedate.

within :process, the date-string is manipulated. I don't know which format you use, but the basic formula is %variable:~startposition,length% where startposition starts at 0=first character. The idea is to have fdate in the format yyyymm

if pfname (previos filename) is not set, this is the first file found, so we don't delete that.

For every other file, if the filedate is the same as the previous filedate, then delete the previous filename.

The current filename/date is then recorded as the previous version.

Done!

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Wow! This works like a charm!!! Thanks so much! Also big thanks for taking time to explain it! I use a different date format and your explanation saves me a lot of time and headache!!! Thanks so much!!!!! – Smiley Jan 17 '14 at 23:51