Hmm. strange - works as-is for me...
Good to see you're trying out the debugging facilities.
Here's the same source with [line numbers] for easier reference:
----- batch begins -------
[01]@echo off
[02]:: delete oldest from subtree until freespace > specified size
[03]setlocal enabledelayedexpansion
[04]set "tempfile=%temp%\temp##.###"
[05]del "%tempfile%" >nul 2>nul
[06]set "target=c:\destdir"
[07]for /f "tokens=*" %%i in (
[08]'dir /s /b /a-d "%target%" 2^>nul'
[09]) do (
[10]call :procdate %%~ti
[11]>>"%tempfile%" echo !formdate! %%i
[12])
[13]for /f "tokens=1*" %%i in (
[14]'sort ^<"%tempfile%" ' ) do (
[15]for /f "tokens=3" %%s in ( ' dir /-c "%%j" 2^>nul ' ) do set
size=000000000000000%%s
[16]set size=!size:~-15!
[17]if "!size!" gtr "001681000000000" echo Done!&del "%tempfile%" >nul&goto
:eof
[18]ECHO del "%%j"
[19]ping -n 15 127.0.0.1 >nul
[20])
[21]goto :eof
[22]
[23]::
[24]:: Process the date and time supplied as parameters
[25]:: destination envvar=formdate
[26]:: destination format is
[27]:: CCYYMMDDHH(:)MM or
[28]:: CCYYMMDD[a/p](.)(m(.))HH(:)MM
[29]:: if 12-hr clock used, pay attention to leading zeroes
[30]:: and convert HH=12 to 00. That way, the sequence
[31]:: 12:59am 01:00am 11:59am 12:00pm 12:59pm 01:00pm becomes
[32]:: a0059 a0100 a1159 p0000 p0059 p0100
[33]:: which sorts correctly.
[34]:: Critical issue is consistency of length of subfields.
[35]:: Colons, etc are superfluous.
[36]::
[37]:: date part - keep or generate consistent-length subfields
[38]:: and observe order
[39]::
[40]:: Fortunately for me, I use 24-hr time. Date format is dd/mm/yy
[41]::
[42]:: Not interested in dates pre-2000. If you are, make adjustments.
[43]::
[44]:procdate
[45]set formdate=%1
[46]set formdate=%formdate:~6,2%%formdate:~3,2%%formdate:~0,2%%2
[47]goto :eof
------ batch ends --------
So - to clarify (some of this will be obvious but might help others
interested)
[03] Allows use of delayed-expansion mode so !var! is evaluated as the
current value of VAR within FOR loops (%var% would be the value at parse
time.) As a bonus, ensures initial environment values are restored at
batch-end.
[04,05] Establishes a temporary filename and deletes the file if it exists,
suppressing messages
[07..12] Performs a dir /s /b /a-d on the target directory, producing a list
of the full names of each file (/b) except directorynames (/a-d) including
all subdirectories (/s)
For each line in this list, assign the FULL text (ie full filename) to %%i
and
1) call the PROCDATE subroutine passing the file's datestamp (%%~ti) as
parameters
2) append a line containing the current value of FORMDATE (which will be set
up by PROCDATE) and a space and the full filename to the temporary file
In consequence the tempfile should contain a lines of the form
YYMMDDHH:MM full-filename
[13..20] The tempfile is used as input to a SORT, and each line then parsed
according to the tokens. The first token on the line is assigned to %%i and
the remainder of the line to %%j.
With each line (=filename in %%j) :
1) perform a DIR on the filename found, using /-c to remove the
thousands-separators from the sizes reported. Assign the third token from
the line to %%s, prefix this with a goodly number of 0s and assign that to
SIZE.
The result should be that SIZE after executing [15] contains the remaining
space on the target drive, prefixed by zeroes, since the last line of the
DIR command reports the remaining-space as the third token.
2) [16] sets SIZE to its own last 15 characters, using the current value set
in the FOR..%%s just executed
3) compare that result to the desired minimum freespace, forcing CHARACTER
comparison which proceeds left-to-right. The values will therefore be
compared correctly if the two strings are of equal length. The comparison
can't be made in NUMERIC mode because the values involved may be larger than
the 31-bit computation capacity allowed.
If we have enough space, display done, delete the tempfile and exit the
batch
If we don't have enough space, delete the target file named in %%j. Note
that the SORT in [14] ensures that the lines are presented to the FOR...%%i
in order of timestamp since the first column is YYMMDDHH:SS, therefore the
deletion proceeds oldest-first until the freespace-required is reached.
the PING and ECHO are used for testing, as noted.
The PROCDATE procedure should present no problem.
PROCDATE receives two parameters - the date and time. It assigns the date
part to FORMDATE then re-assigns the value using substringing (which can
only be done on a conventional environment variable, not on a metavariable)
and tags the time on to the end of the rearranged date-string as it's in a
convenient form.
So - hope the explanation is clear and helps to clear up any problem you're
having.