Link to home
Start Free TrialLog in
Avatar of Blue Street Tech
Blue Street TechFlag for United States of America

asked on

How to write a batch file to move files by 10 most recently created files

Background: We have an app that writes a backup zip file each day. It has no grooming capabilities and they are pretty large (~600MB). So I need to delete the old files and keep only the most recent ones. The script I have below does this perfectly but now since recent compliance regulations we must destroy these files to a specific data destruction spec.

Therefore, I'd like to "move" them into the Recycle Bin instead of deleting them. Once they are in the Recycle Bin I have setup a schedule to destroy the data in there everyday at a set time using a data destruction application called Eraser. I'd also like to write some command to allow the dialogue box to stay posted for a specific amount of time (say 15 minutes) rather than having the command "pause" thereby demanding user interaction in order to proceed.

Here is the script thus far:
@echo off
cd /d C:\Users\<user>\Documents\Operations\Backup Configs\
for /f "tokens=* skip=10" %%a in ('dir /b /a-d /o-d') do ECHO del "%%a"

Open in new window


Thanks in advance!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

There is no direct way that I know from batch.  There are some third party tools that do it and we coudl call a VBScript with the filenames (e.g. see the script in the link below).

Would it be another option perhaps to just monitor another directory too for erasing, e.g. "C:\Users\<user>\Documents\Operations\Backup Configs\Deleted" or something

http://stackoverflow.com/questions/1062808/how-can-i-move-files-to-the-recycle-bin-in-a-windows-batch-script-or-perl
Avatar of Blue Street Tech

ASKER

Hi dragon-it,

You are actually the one who originally wrote the script above!

Yes, I can create another dir within the backup like C:\Users\<user>\Documents\Operations\Backup Configs\Destroy\ if that will make it simpler.

I was going to use this...http://mixeduperic.com/windows/how-to-move-files-with-a-batch-file.html, but it sounds like it will not work with the additional parameters (most recent files, # of most recent files) we have setup? Is that correct?
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems to be taking the newest files and moving them rather than the oldest but it could be because of the method in which I created my test data...simply copying files in order to create multiples of them...the stamps may be off using that method.

Thoughts?
If you copy/paste the dates will probably show a new created date but the old modified time.  It is the same dir as before so it should be the same logic.  If you do the dir command manually it will show you:

dir /a-d /o-d

That should show in reverse date order, i.e. newest at the top, then it skips the top 10 and moves / deletes the rest.

You can also add a "/t" command to the dir to specifically use created / modified time if wanted:


 /T          Controls which time field displayed or used for sorting
 timefield   C  Creation
             A  Last Access
             W  Last Written

Open in new window


You could create some test files by doing something like this from commandline to make file1.txt to file30.txt.  File 30 will be the"newest" so it should keep 20-30 and move the others.

for /l %a in (1,1,30) do echo File %a>%a.txt


Steve
Also you can soon show, or log the file details for moving too:


@echo off
Set baseDir=C:\Users\<user>\Documents\Operations\Backup Configs\
cd /d "%BaseDir%
md "%BaseDir%\Destroy" 2>NUL
for /f "tokens=* skip=10" %%a in ('dir /b /a-d /o-d') do (
  MOVE "%%~a" "%BaseDir%\Destroy"
  echo %date% %time% - Moved file %%~nxa dated %%~ta for destruction
)

Open in new window



@echo off
Set baseDir=C:\Users\<user>\Documents\Operations\Backup Configs\
Set log="%BaseDir%\Log\Log.txt"
cd /d "%BaseDir%
md "%BaseDir%\Destroy" 2>NUL
md "%BaseDir%\Log" 2>NUL
for /f "tokens=* skip=10" %%a in ('dir /b /a-d /o-d') do (
  MOVE "%%~a" "%BaseDir%\Destroy"
  echo %date% %time% - Moved file %%~nxa dated %%~ta for destruction
  echo %date%,time%,%%~nxa,%%~ta,%%~za >>"%log%"
)

Open in new window

Wow...thanks for all the added bonus stuff! Everything is work perfectly. I can get all versions of the script (the script, the script w/showing log) running except for the last one, which creates a log (very cool BTW!).

Thoughts? I copied the script to avoid syntax errors.

Thanks!

Here is my script just in case I transcribed something in error:
@echo off
Set baseDir=C:\Users\WK-09\Documents\Operations\Backup Configs\
Set log="%BaseDir%\Log\Log.txt"
cd /d "%BaseDir%
md "%BaseDir%\_DESTROY" 2>NUL
md "%BaseDir%\Log" 2>NUL
for /f "tokens=* skip=10" %%a in ('dir /b /a-d /o-d') do (
MOVE "%%a" "%BaseDir%\_DESTROY" 
echo %date% %time% - Moved file %%~nxa dated %%~ta for destruction
echo %date%,time%,%%~nxa,%%~ta,%%~za >>"%log%"
)

Open in new window

Apologies I left quotes in the "set log=" line and the last line so it would have two sets.  Remove both " " from either line 10 or line 3 and should be good to go.

Steve
The log should show date, time, filename, datetime of file, and size in text format which if you call it log.csv instead can load straight into Excel.

Steve
Still no go... I tried removing double quotes from line 3, then test and then put them back and removed from line 10 & tested, and finally removed them from both lines & tested...all resulting in nothing happening except for creating an empty "Log" directory.

@echo off
Set baseDir=C:\Users\WK-09\Documents\Operations\Backup Configs\
Set log=%BaseDir%\Log\Log.csv
cd /d "%BaseDir%
md "%BaseDir%\_DESTROY" 2>NUL
md "%BaseDir%\Log" 2>NUL
for /f "tokens=* skip=10" %%a in ('dir /b /a-d /o-d') do (
MOVE "%%a" "%BaseDir%\_DESTROY" 
echo %date% %time% - Moved file %%~nxa dated %%~ta for destruction
echo %date%,time%,%%~nxa,%%~ta,%%~za >>"%log%"
)

Open in new window

for some reason broadband connection gets connection reset from EE website at the moment but phone ok.  will have proper look when working properly...

script looks ok
And then as soon as you send comment on phone the page pops up on PC....

I see the issue, % missing from %time% on line 10, helps seeing on 19" screen than 3" !

echo %date%,%time%,%username%,%%~nxa,%%~ta,%%~za >>"%log%"

You can use whatever you want there, just seemed like a good idea to record

Steve
I know, EE has been acting weird but it settled down for me now.

Hey, thank you so much...the script is rocking now! Everything is working perfectly.

P.S. Thanks for the extras - I love them!
There are some bonus feature-added build-outs toward the bottom comments but this is the core of the solution. Thanks again!
No problem, glad it helped

Steve