Search This Blog

Wednesday 13 February 2013

How to - Automatically Delete Old Backup Files from a USB Disk

This guide explains how to use the forfiles and at commands to delete any file type from a USB disk that is over a certain age in days.

1. USB Disk

media_1360753309781.png

Here we have a USB disk that has the common problem, there is not enough disk space left on the disk for the backup to be stored.

2. Age of Files

media_1360753341070.png

Here we can see that some of the files are of different ages and we would like this disk to be clear to allow room for tonights backup.

3. Command Prompt

media_1360753421394.png

Run the command cmd and then at the command prompt we will use the command called forfiles.

4. Forfiles Command

media_1360753471047.png

The forfiles command has multiple options but the main use we have is to delete all files with a certain extension from a certain location that are over a certain number of days old. So the syntax is explained below

forfiles /P G:\ - This is the path location and in our example the G: disk, but this could be any folder path.

/M *.TIB - This describes the file extension we want to find, so by using *.TIB we will find all Acronis Backup Ffiles, however this could be used for any file extension or *.* for any file.

/D -7 - This is the number of days that the file must be older than for the command to return an answer so we are only going to find files older than seven days.

/C "cmd /c del /q @path" - This is the more complex part of the command, this is the command that will be executed against the result we generated in the first part. In this example we will run cmd with /c to terminate the command once it is complete, next we use del to delete files with the /q for quiet mode so we do not have to confirm the delete and finally we add @path to take the folder path from the first part of the command!

So what we end up with is forfiles /P G:\ /M *.TIB /D -7 /C "cmd /c del /q @path"

5. Run Forfiles Command

media_1360753482138.png

If we now run this command we see that the one TIB file has been deleted as this was over 7 days old. NB The command has no confirmation so be 100% sure you know what you are going to delete and where before you execute.

6. Schedule this Everyday

media_1360753507407.png

OK so now we have the command to clean up our disks we want to run this everyday automatically and we can do this with the at command. The at command allow you to run a command at a specific time at reoccuring intervals.

at 18:00 - The first part of the command says run at 18:00 or anytime in the 24 hour clock you choose.
/every:m,t,w,th,f - The second part of the command schedules what days the command will run, in this case it is Mon - Fri. Saturday is S and Sunday is SU

You then follow the command with the forfiles command to create a scheduled task.

7. Confirm Scheduled Task

media_1360753520256.png

To confirm that the task is scheduled you can run the at command with no switches and you will see the output describing the task number, days to run, time to run and the command to run.

NB If you make an error entering the command you can delete the task with the command

at /1 delete - Where 1 is the number of the task ID.

8. Command Switch Options

Below are the full switch outputs for the forfiles and at commands.

9. ForFiles Command

FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {dd/MM/yyyy | dd}]

Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.

Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).

/M searchmask Searches files according to a searchmask.
The default searchmask is '*' .

/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".

/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.

The default command is "cmd /c echo @file".

The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.

To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".

/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"dd/MM/yyyy" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.

/? Displays this help message.

Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C "cmd /c type @file | more"
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C "cmd /c echo @path 0x09 was changed 30 days ago"
FORFILES /D 01/01/2001
/C "cmd /c echo @fname is new since Jan 1st 2001"
FORFILES /D +13/2/2013 /C "cmd /c echo @fname is new today"
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

10. At Command

The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
[ /EVERY:date[,...] | /NEXT:date[,...]] "command"

\\computername Specifies a remote computer. Commands are scheduled on the
local computer if this parameter is omitted.
id Is an identification number assigned to a scheduled
command.
/delete Cancels a scheduled command. If id is omitted, all the
scheduled commands on the computer are canceled.
/yes Used with cancel all jobs command when no further
confirmation is desired.
time Specifies the time when command is to run.
/interactive Allows the job to interact with the desktop of the user
who is logged on at the time the job runs.
/every:date[,...] Runs the command on each specified day(s) of the week or
month. If date is omitted, the current day of the month
is assumed.
/next:date[,...] Runs the specified command on the next occurrence of the
day (for example, next Thursday). If date is omitted, the
current day of the month is assumed.
"command" Is the Windows NT command, or batch program to be run.

No comments:

Post a Comment