IVO, Judago - Clean-up Script

Dell / Precision workstation 390
January 28, 2009 at 18:24:40
Specs: Microsoft Windows XP Professional, 1.861 GHz / 2045 MB
hey guys, long time! can you help me out again here?

i need a clean-up script.. i have a bunch of files in different sub-folders that i want to be compared against to, move the older files to a different folder.. illustration below..

file structure:


ok, so what i need is a script to search through d:\marie all common files, move the older files to a separate folder d:\marie_backup and just leave the new ones on the current folder..

thanks a bunch!
Marie :D

hopeless but trying

See More: IVO, Judago - Clean-up Script

Report •

January 28, 2009 at 21:44:24

It would help if you post the dates of the
existing files and the desired output
structure, before anyone spends time figuring
this one out.


Report •

January 29, 2009 at 08:28:45
Hello Marie,

glad you are back and heralding the incoming spring.

The problem you propose looks more like a brain trainer puzzle than an actual job, please remember me the layout of files' timestamp in Canada as displayed by e.g.

dir D:\marie\01\fluffy1.a

and confirm any subfolder contains the same files thought with different timestamps.

Anyway if I fail I'm sure Judago and other guys will go on to support you.


Report •

January 29, 2009 at 08:43:25
hello IVO hello Holla

IVO its same system as before.. i have cut and pasted from my old post:


from a DIR line:

10/17/2008 03:09 PM 0 ivo.txt

Holla, output structure should be:
1. latest files to be left in the folder
2. outdated files to be MOVED to d:\marie_backup

IVO its not a brain puzzle hahaha.. its a working directory with lots of duplicate files in different sub-folders!

hopeless but trying

Report •

Related Solutions

January 29, 2009 at 22:48:54
Ok here you go.

Use for /r command and use %~t to get the time.
Parse it using variable substitution techniques and print the time in yyyy-mm-dd format.
Now redirect the output, yyyy-mm-dd and full filename to a single file.
Once the file is created, sort it and create a sorted file.
Now parse the sorted file using for /f command and when the file name is same as previous, compare dates and output the older file name to a files-2b-deleted file.
finally, delete all files listed in in files-2b-deleted file.


Report •

January 30, 2009 at 07:09:34
Hello Marie,

work in progress in a way much like what Holla outlined. I hope I can give you the tested and working script in the weekend.

See you as soon and Ciao


Report •

January 30, 2009 at 14:50:47
Hi Marie,

Close to arrival... more accurate test planned, but the script is working fine.

You'll find it in my next post tomorrow as now time is late night in Milan.

Report •

January 30, 2009 at 16:32:00
Hi marie & Ivo!

I too have a script that seems to be working, I am going to post it with the same double check as last time, prefixing the move command with echo so you can see what it would do.

It creates a directory in d:\marie_backup that it is the current date with [n] where n is the number of times it has been run in the day.

@echo off
setlocal enabledelayedexpansion
set fcnt=1
set dt=%date:/=-%
if exist "d:\marie_backup\%dt% [%fcnt%]\*" set /a fcnt+=1&&goto nfolder
xcopy "d:\marie" "d:\marie_backup\%dt% [%fcnt%]" /s /t /i
for %%z in (fluffy1.a fluffy1.b mayo1.a mayo1.b) do (
type nul>"%temp%\$$$$temporaryfile.temp"
for /r d:\marie %%a in (%%z) do (
if exist "%%~fa" (
for /f "tokens=1-5 delims=/ " %%b in ("%%~ta") do (
set tme=%%e
if !tme:~0^,2!==12 set tme=00!tme:~2!
>>"%temp%\$$$$temporaryfile.temp" echo %%d %%b %%c %%f !tme! %%~fa
for /f "tokens=5,* delims= " %%g in ('sort "%temp%\$$$$temporaryfile.temp"') do set newest=%%h
if defined newest for /f "delims=" %%i in ('2^>nul dir /s /b /a-d d:\marie\%%z^|find /v /i "!newest!"') do (
set bkfol=%%~pi
set bkfol=\!dt! [!fcnt!]!bkfol:~6!
move "%%i" "d:\marie_backup!bkfol!%%~nxi"

It will probably leave some empty directories in the marie folder after moving files out and could also leave some in the <date> [n] directory. If this is a problem I can append something to clean them up.

Report •

February 1, 2009 at 09:33:04
Hi Marie,

Judago landed first on the Moon, but mission n°2 may be interesting too.

Please until you are not abosolutely sure the batch works as you wish type the /T or /t switch after the script name, e.g. myclean /T, so the script does nothing but generates the log report you can examine.

The batch takes into account the date only not the time of the files. That can be done but requires more work.

Here my script, have a nice week start.

:: MYCLEAN.BAT  Usage: myclean [/T]
:: /T is for testing purpose and suppresses the MOVE statement
@echo off & setlocal EnableDelayedExpansion

pushd "\marie"
if not exist "\marie_backup" MD "\marie_backup"

echo. > %temp%.\%~n0.log
if /I "%1"=="/T" (
  set T=echo ..TEST
  echo.  TEST MODE - NO ACTION PERFORMED >> %temp%.\%~n0.log
echo.  Backup and Cleanup performed on %date% at %time% >> %temp%.\%~n0.log

type nul > %temp%.\%~n0.tmp
for /F "delims=" %%i in ('dir /S /A-D /B *.*') do (
  set stamp=%%~ti
  set stamp=!stamp:~6,4!-!stamp:~0,2!-!stamp:~3,2!
  set line=%%~nxi~!stamp!~%%~dpi
  echo.!line!>> %temp%.\%~n0.tmp

set file=
set counter=0
for /F "tokens=1-3 delims=~" %%i in ('sort /R ^< %temp%.\%~n0.tmp') do (
  if /I not "%%i"=="!file!" (
    if !counter! gtr 1 (
      set stamp=!oldest!
      set stamp=!stamp:~5,2!/!stamp:~8,2!/!stamp:~0,4!
      echo.--!stamp! moved from !folder! >> %temp%.\%~n0.log
      %T% MOVE "!folder!!file!" "\marie_backup"
    echo. >> %temp%.\%~n0.log
    echo.%%i >> %temp%.\%~n0.log
    set stamp=%%j
    set stamp=!stamp:~5,2!/!stamp:~8,2!/!stamp:~0,4!
    echo.--!stamp! latest  in %%k >> %temp%.\%~n0.log
    set counter=0
  set file=%%i
  set oldest=%%j
  set folder=%%k
  set /A counter+=1
if !counter! gtr 1 (
  set stamp=!oldest!
  set stamp=!stamp:~5,2!/!stamp:~8,2!/!stamp:~0,4!
  echo.--!stamp! moved from !folder! >> %temp%.\%~n0.log
  %T% MOVE "!folder!!file!" "\marie_backup"

echo. >> %temp%.\%~n0.log
echo.  Processing ended on %date% at %time% >> %temp%.\%~n0.log
start "" %temp%.\%~n0.log
del %temp%.\%~n0.tmp
:: [End_Of_Batch]

Report •

February 3, 2009 at 08:22:38
sweeeettt!! ill test it out guys asap and let you know.. - Marie

hopeless but trying

Report •

February 4, 2009 at 06:19:02
Holla, thanks for the roadmap..
IVO and Judago, you guys rock.. tried out both of your scripts and worked like a charm the first time! i just have to sort a few kinks here and there but really, the scripts did what i expect them to do..

really appreciate the help guys!

Marie <--- ecstatic

hopeless but trying

Report •

Ask Question