How to locate oldest folder

  • Thread starter Thread starter Colon Terminus
  • Start date Start date
C

Colon Terminus

Using techniques gleaned from this newsgroup, I've written a batch file
that creates a folder named todays date (yyyy-mm-dd) and copies some
files into that folder for archival purposes. The batch file is run by
task scheduler during off hours. All these folders are on an external
Firewire drive and the contents of the drive consist of nothing but
folders named yyyy-mm-dd. Now I'm in over my head. What I need to do is
delete the oldest folder on the disk when there is less than 10GB free
space remaining.

I'd sure appreciate any help you guys can give.
 
Colon Terminus said:
Using techniques gleaned from this newsgroup, I've written a batch file
that creates a folder named todays date (yyyy-mm-dd) and copies some
files into that folder for archival purposes. The batch file is run by
task scheduler during off hours. All these folders are on an external
Firewire drive and the contents of the drive consist of nothing but
folders named yyyy-mm-dd. Now I'm in over my head. What I need to do is
delete the oldest folder on the disk when there is less than 10GB free
space remaining.

I'd sure appreciate any help you guys can give.

Change C:\ to the root of the drive to check
Change C:\Users to the directory in question

After testing, remove the @echo part of @echo rd /s /q "%%a"

@echo off & setlocal ENABLEEXTENSIONS

for /f "usebackq tokens=1-10" %%a in (`dir /-p /-s /-c c:\ ^| findstr /i
/C:" Bytes Free"`) do set FreeSpace=%%c

if %FreeSpace% LEQ 10000000000 (
for /f "tokens=*" %%a in ('dir C:\Users /ad /b /od') do (
if NOT "%%a"=="" (
@echo rd /s /q "%%a"
goto JumpOut
)
)
:JumpOut
::Dummy line - leave in to keep error from happening
)
 
Paul R. Sadowski said:
)
:JumpOut
::Dummy line - leave in to keep error from happening
)

I was interrupted and reflexively posted before cleaning up.

@echo off & setlocal ENABLEEXTENSIONS

for /f "usebackq tokens=1-10" %%a in (`dir /-p /-s /-c c:\ ^| findstr /i
/C:" Bytes Free"`) do set FreeSpace=%%c

if %FreeSpace% LEQ 10000000000 (
for /f "tokens=*" %%a in ('dir C:\Users /ad /b /od') do (
if NOT "%%a"=="" (
@echo rd /s /q "%%a"
goto JumpOut
)
)
)
:JumpOut
 
In said:
Using techniques gleaned from this newsgroup, I've written a batch
file that creates a folder named todays date (yyyy-mm-dd) and
copies some files into that folder for archival purposes. The
batch file is run by task scheduler during off hours. All these
folders are on an external Firewire drive and the contents of the
drive consist of nothing but folders named yyyy-mm-dd. Now I'm in
over my head. What I need to do is delete the oldest folder on the
disk when there is less than 10GB free space remaining.

Now that you have a *sortable* format for the directory names...

for /f %a in ('dir /o-n /b') do set lasttarget=%a
rd %lasttarget% /s /q

might get you started.
 
Paul R. Sadowski said:
I was interrupted and reflexively posted before cleaning up.

@echo off & setlocal ENABLEEXTENSIONS

for /f "usebackq tokens=1-10" %%a in (`dir /-p /-s /-c c:\ ^| findstr /i
/C:" Bytes Free"`) do set FreeSpace=%%c

if %FreeSpace% LEQ 10000000000 (
for /f "tokens=*" %%a in ('dir C:\Users /ad /b /od') do (
if NOT "%%a"=="" (
@echo rd /s /q "%%a"
goto JumpOut
)
)
)
:JumpOut

Thanks, Paul, for your most excellent solution. I guess I need to read
up on these "Extensions".
 
Mark V said:
Now that you have a *sortable* format for the directory names...

for /f %a in ('dir /o-n /b') do set lasttarget=%a
rd %lasttarget% /s /q

might get you started.

Yeah, I named the directories that way, 'cause I knew they'd need to be
sorted.
Thanks for the elegant little for statement.
 
for /f %a in ('dir /o-n /b') do set lasttarget=%a
rd %lasttarget% /s /q

That could be a problem. If there are any regular files in the directory
then lasttarget could be set to a file name. You should specify dirs only
(/ad). Also, you should either explicitly tell dir not to do subdirs or at
least reset DIRCMD with a setlocal in case it includes the /s switch.

You also need to double quote the second "%a" and use a tokens=* qualifier
in the for to handle names with spaces.
 
Colon Terminus said:
Thanks, Paul, for your most excellent solution. I guess I need to read
up on these "Extensions".

I added the set DIRCMD line here just in case. I failed to add the /-s to
the second dir command.

@echo off & setlocal ENABLEEXTENSIONS
set DIRCMD=
for /f "usebackq tokens=1-10" %%a in (`dir /-p /-s /-c c:\ ^| findstr /i
/C:" Bytes Free"`) do set FreeSpace=%%c

if %FreeSpace% LEQ 10000000000 (
for /f "tokens=*" %%a in ('dir C:\Users /ad /b /od') do (
if NOT "%%a"=="" (
@echo rd /s /q "%%a"
goto JumpOut
)
)
)
:JumpOut
 
In microsoft.public.win2000.cmdprompt.admin Paul R. Sadowski [MVP]
wrote:
That could be a problem. If there are any regular files in the
directory then lasttarget could be set to a file name. You should
specify dirs only (/ad). Also, you should either explicitly tell
dir not to do subdirs or at least reset DIRCMD with a setlocal in
case it includes the /s switch.

You also need to double quote the second "%a" and use a tokens=*
qualifier in the for to handle names with spaces.

All true. I plead *assumptions* :) Thanks Paul
 
Paul R. Sadowski said:
I added the set DIRCMD line here just in case. I failed to add the /-s to
the second dir command.

@echo off & setlocal ENABLEEXTENSIONS
set DIRCMD=
for /f "usebackq tokens=1-10" %%a in (`dir /-p /-s /-c c:\ ^| findstr /i
/C:" Bytes Free"`) do set FreeSpace=%%c

if %FreeSpace% LEQ 10000000000 (
for /f "tokens=*" %%a in ('dir C:\Users /ad /b /od') do (
if NOT "%%a"=="" (
@echo rd /s /q "%%a"
goto JumpOut
)
)
)
:JumpOut

Thanks once again, Paul
 
Back
Top