0

I have read the answer to "Batch File to Delete Files in a Folder" proposed by @Magoo and having tried the code but could not comment or ask additional questions, so I do apologize if I'm breaking some rules here.

Let me say up front that I may misunderstood how this is supposed to work.

First I made sure all files in the directory I wanted to clean up had the same date . I then replaced the c:\sourcedir with the name of said directory and ran the cmd file.

I expected it to produce an empty string, but got the full filelist.

Regardless of the fact that I may have missed something, this could turn out to be quite unfortunate.

I'm not very good at DOS/CMD stuff, so I was wondering if @Magoo might clarify this issue.

Community
  • 1
  • 1
webhiker
  • 49
  • 6
  • Try pasting the above into a comment in the correct thread now. Delete this question if it works. – foxidrive Oct 01 '14 at 15:26
  • Can't do it - It seems silly but one has to have at least 50 reputation to comment, although for some reason I can do in my own thread. – webhiker Oct 01 '14 at 17:32

1 Answers1

1

Not sure this is what you need, but can be used as a skeleton to start

@echo off
    setlocal enableextensions disabledelayedexpansion

    pushd "c:\where\the\filesAre" && (
        for /f "skip=1 delims=" %%a in ('dir /b /a-d /tw /o-d *') do echo del "%%a"
        popd
    )

First, the active directory is changed to the adecuated folder (change to your needs). Then a for command is used to execute a dir command and process its output. For each line in the dir output, the code after the do clause is executed, with the content of the line stored in the for replaceable parameter %%a

The dir command will list the files in bare format (/b only file names), excluding directories (/a-d), in descending last write date order (/tw /o-d). So, the newest file is the first in the list (the output of the dir command) that the for command will process.

As we are asking for to skip one line (skip=1), this first file will be ignored. For the rest of the lines/files a del command is executed.

del operations are only echoed to console. If the output is correct, remove the echo command to delete the files.

edited to adapt to comments

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Configure extensions to process
    set "_ext.00=al*.txt"
    set "_ext.01=cm*.txt"

    rem Change to the desired folder
    pushd "c:\where\the\filesAre" || goto :eof

    rem For each pattern in the list
    rem     For each file matching the pattern (except the newest)
    rem         Delete the file
    for /f "tokens=1,* delims==" %%x in ('
        set _ext.
    ') do for /f "skip=1 delims=" %%a in ('
        dir /b /a-d /tw /o-d "%%~y" 2^>nul
    ') do echo del "%%a"

    popd

Define an "array" with the list of file patterns to process. For each of the patterns (retrieved with a set command inside the outter for /f) iterate over the files matching that pattern (the inner for), skipping the first/newest and removing the rest.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thank you for your effort. I'm not sure why, but this code does nothing on my machine except list all files bar one - that is the cmd file being executed. What I need is to keep the latest file for a given pattern as in AL*.TXT, CM*.TXT and so on, and delete the rest. – webhiker Oct 02 '14 at 11:06
  • @webhiker, change the `*` into `al*.txt` and test – MC ND Oct 02 '14 at 11:17
  • Now I'm getting only txt files, and it sort of works - but only if there is more than one file of it's kind. If there is only one, it will get deleted as well - and that just won't do. Also, it would be nice if one could pass the filemask as parameter. – webhiker Oct 02 '14 at 13:00
  • Works like a charm - you are a prince! Is there a limit to the number of elements one can enter into the list? I'm up to ten by now and it works just fine - although I have not quite gotten around to removing the echo just yet :-) – webhiker Oct 03 '14 at 09:01
  • @webhiker, the limit is the memory available in the environment block. From [Microsoft](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx): `*Windows Server 2003 and Windows XP*: The maximum size of the environment block for the process is 32,767 characters. Starting with Windows Vista and Windows Server 2008, there is no technical limitation on the size of the environment block.` – MC ND Oct 03 '14 at 09:05