43636

Batch File to Delete Files in a Folder

Question:

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?

Answer1:

< 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!

Recommend

  • Problems saving data in added fields to a javascript form
  • Needed Batch command for moving file other than *.bat all others to the different folder [closed]
  • Renaming folders with different names or removing them?
  • How to use bash to test directory limits of filesystem
  • Looping files in bash
  • Add resource files in wix installer
  • Wix Custom Action Implementation for Writing Installfolder in text
  • auto renaming batch file
  • Parse file name using batch automation
  • Wix Installer - Create Folder hierarchy based on Property
  • Deleting/replacing characters delimited by commas
  • Creating a batch file to delete multiple, separated subdirectories
  • how to make folder name with yesterday's date?
  • error LGHT0204 : ICE57: Component 'XXX' has both per-user data and a keypath that can be e
  • How to create a specific URL for a user during registration?
  • oracle duplicate rows based on a single column
  • Email Intent Email Chooser
  • Multithreaded Server receives data from just one client JAVA
  • Getting Powershell variable value in batch script
  • Dump all Raima db_VISTA Version 3.20 data to text
  • ANTLR4: Unrecognized constant value in a lexer command
  • Batch: Set one directory up path in variable
  • How do I chomp a string if I have Perl 4?
  • K-means in OpenCV's Python interface
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • List images(01.png) and descriptions(01.txt) from directory
  • Copy to all folders batch file?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How to test if a URL from an Eclipse bundle is a directory?
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?