Tuesday, March 7, 2017

Move or Delete files with a log. Windows, .BAT

Below are 2 .bat files. One moves files and creates a log of the files moved and the other deletes the files and creates a log.
The way I use this is the backup files go into a folder for offset backups and get copied off site, they then get moved into another folder to store for a longer time and finally deleted after a # of days to preserve space.

MOVE

ECHO OFF

REM Author Daniel Gallant
REM Date Nov, 2016

setlocal ENABLEDELAYEDEXPANSION

REM This program will create a text document with the name of the files older then 'numDays' days, and will then move those files older then 'numDays' days to 'backupArchive'.


REM Change these to:
set "backUpPath=C:\Users\Public\Pictures\Sample Pictures3" REM Specify the folder to move files from
set "logPath=C:\Users\Public\logs" REM Specift the folder to send log file to
set "numDays=2" REM Specify number of days old the files are before they are moved
set "backupArchive=C:\Users\Public\Pictures\archive" REM This is where the files are moved to
set "today=!date:/=-!" REM *DO NOT CHANGE* This is a variable to set todays date in the log file name


REM This lists the files and puts them into a .txt for review after Move.
REM -p= path to backup files; -s= include subdirectiries; -m=type of file to search for; /D=num of days, - means old; /C rum the command in "" from cmd
forfiles -p "%backupPath%" -s -m *.* /D -%numDays% /C "cmd /c echo @path" >> "%logPath%\!today!_Backups-Moved.txt"


REM This command moves the files older then 'numDays'.

forfiles -p "%backupPath%" -s -m *.* /D -%numDays% /C "cmd /c move @path %backupArchive%"



DELETE

@ECHO OFF
REM Author Daniel Gallant
REM Date Nov, 2016

setlocal ENABLEDELAYEDEXPANSION

REM This program will create a text document with the name of the files older then 'numDays' days, and will then delete those files older then 'numDays' days..


REM Change these to:
set "backUpPath=C:\Users\Christian\Pictures" REM Specify the folder to delete files from
set "logPath=C:\Users\Public" REM Specift the folder to send log file to, Folder must be created
set "numDays=30" REM Specify number of days old the files are before they are moved
set "today=!date:/=-!" REM This is a variable to set todays date in the log file name
REM This lists the files and puts them into a .txt for review after delete.
REM -p= path to backup files; -s= include subdirectiries; -m=type of file to search for; /D=num of days, - means old; /C rum the command in "" from cmd
forfiles -p "%backupPath%" -s -m *.* /D -%numDays% /C "cmd /c echo @path" >> "%logPath%\!today!_Backups-Deleted.txt"


REM This command deletes the files older then /D #.

REM forfiles -p "%backupPath%" -s -m *.* /D -%numDays% /C "cmd /c /q del @path"   <- This is for deleting the files.

No comments:

Post a Comment