I have a folders structure where I store my scripts, backups, documents as shown below
The folders becomes cluttered with old versions of the files. So I decided to create a script that will move old files from each of the folders beneath the root one (e.g., C:\WP) to an archive sub-folder (if it exists at the particular level).
So I decided to archive old files in the Backups, Exchange, and SharePoint folders.
I created sub-folders called 'ARCHIVE' under each of those three folders as shown below
And then I created the following batch file
@ECHO OFF
SETLOCAL
SET ArchiveFolder=Archive
SET OlderThan=2
SET Extensions=TXT CSV BAK LOG
REM starts from the current folder
REM [when it's a scheduled task this is configured as "START IN"]
REM CD C:\WP
SET debug=ECHO
SET LogFile="%~dpn0.LOG"
FOR /F %%m in ('CD') Do SET StartDir=%%m
%debug%.>>%LogFile%
%debug%.>>%LogFile%
%debug%.>>%LogFile%
%debug% ***** Started on [%date%] at [%time%] ***** >>%LogFile%
%debug% The Top Directory [%StartDir%] >>%LogFile%
%debug% Moving files older than [%OlderThan%] days >>%LogFile%
%debug% with extensions [%Extensions%] >>%LogFile%
%debug% into [.\%ArchiveFolder%] subfolder >>%LogFile%
%debug% ********************************************** >>%LogFile%
%debug%.>>%LogFile%
FOR /F %%i in ('DIR . /AD /B /S') DO (
if EXIST "%%i\%ArchiveFolder%" (
%debug% Moving files from [%%i] to [.\]%ArchiveFolder% >>%LogFile%
Call :MoveFiles %%i %Extensions%
)
)
%debug%.>>%LogFile%
%debug% ********************************************** >>%LogFile%
%debug% *** Complete on [%date%] at [%time%] ***** >>%LogFile%
Goto :EOF
REM ------------------------------------------------------------------
REM MoveFiles SourcePath Ext1 Ext2 .... Ext4
REM from [SourcePath] to [SourcePath\%ArchiveFolde%]
REM older than %OlderThan% days
REM
:MoveFiles
SET CurP="%1"
SET CurA="%1\%ArchiveFolder%"
:MainLoop
SHIFT
rem Now %1 referes to the next parameter (Ext1 or Ext2 or ...)
if [%1]==[] Goto :EOF
FORFILES 1>nul 2>nul /P %CurP% /M *.%1 /D -%OlderThan% /C "CMD /c %debug% ** Moving @path >>%LogFile% "
FORFILES 1>nul 2>nul /P %CurP% /M *.%1 /D -%OlderThan% /C "CMD /c MOVE /Y @path %CurA%"
goto :MainLoop
REM --------------------------------------------------------------
and scheduled it to run daily and configured the 'Start in' as C:\WP.
This script enumerates all sub-folders (recursively) for the current folder and if the current folder has a subfolder called 'ARCHIVE' it will move all files from the current folder into ARCHIVE provided the files are older than 2 days (configured in the script SET OlderThan=2) and their extensions are one within the list (SET Extensions=TXT CSV BAK LOG) also configured.
If you need to run this script manually then un-rem REM CD C:\WP and change it to point to your top level folder.
The folders becomes cluttered with old versions of the files. So I decided to create a script that will move old files from each of the folders beneath the root one (e.g., C:\WP) to an archive sub-folder (if it exists at the particular level).
So I decided to archive old files in the Backups, Exchange, and SharePoint folders.
I created sub-folders called 'ARCHIVE' under each of those three folders as shown below
And then I created the following batch file
@ECHO OFF
SETLOCAL
SET ArchiveFolder=Archive
SET OlderThan=2
SET Extensions=TXT CSV BAK LOG
REM starts from the current folder
REM [when it's a scheduled task this is configured as "START IN"]
REM CD C:\WP
SET debug=ECHO
SET LogFile="%~dpn0.LOG"
FOR /F %%m in ('CD') Do SET StartDir=%%m
%debug%.>>%LogFile%
%debug%.>>%LogFile%
%debug%.>>%LogFile%
%debug% ***** Started on [%date%] at [%time%] ***** >>%LogFile%
%debug% The Top Directory [%StartDir%] >>%LogFile%
%debug% Moving files older than [%OlderThan%] days >>%LogFile%
%debug% with extensions [%Extensions%] >>%LogFile%
%debug% into [.\%ArchiveFolder%] subfolder >>%LogFile%
%debug% ********************************************** >>%LogFile%
%debug%.>>%LogFile%
FOR /F %%i in ('DIR . /AD /B /S') DO (
if EXIST "%%i\%ArchiveFolder%" (
%debug% Moving files from [%%i] to [.\]%ArchiveFolder% >>%LogFile%
Call :MoveFiles %%i %Extensions%
)
)
%debug%.>>%LogFile%
%debug% ********************************************** >>%LogFile%
%debug% *** Complete on [%date%] at [%time%] ***** >>%LogFile%
Goto :EOF
REM ------------------------------------------------------------------
REM MoveFiles SourcePath Ext1 Ext2 .... Ext4
REM from [SourcePath] to [SourcePath\%ArchiveFolde%]
REM older than %OlderThan% days
REM
:MoveFiles
SET CurP="%1"
SET CurA="%1\%ArchiveFolder%"
:MainLoop
SHIFT
rem Now %1 referes to the next parameter (Ext1 or Ext2 or ...)
if [%1]==[] Goto :EOF
FORFILES 1>nul 2>nul /P %CurP% /M *.%1 /D -%OlderThan% /C "CMD /c %debug% ** Moving @path >>%LogFile% "
FORFILES 1>nul 2>nul /P %CurP% /M *.%1 /D -%OlderThan% /C "CMD /c MOVE /Y @path %CurA%"
goto :MainLoop
REM --------------------------------------------------------------
and scheduled it to run daily and configured the 'Start in' as C:\WP.
This script enumerates all sub-folders (recursively) for the current folder and if the current folder has a subfolder called 'ARCHIVE' it will move all files from the current folder into ARCHIVE provided the files are older than 2 days (configured in the script SET OlderThan=2) and their extensions are one within the list (SET Extensions=TXT CSV BAK LOG) also configured.
If you need to run this script manually then un-rem REM CD C:\WP and change it to point to your top level folder.
Comments
Post a Comment