Management

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 772 other subscribers

Social

  • LinkedIn
  • RSS Feed for Posts
  • Twitter
  • StumbleUpon

Move files to sub folders based upon year and month LastWriteTime [Solved]

Very often I hear my colleagues say that zipping, deleting or archiving a folder takes to many time, and resources of the server they are working on.

This is due to the large amount of files in these folders.

 

In order to help then I write this simple script, to move files based upon their LastWriteTime.

The scripts looks at the LastWriteTime, and creates subfolders based upon year and month.

 

Note:

When working with a large amount of files, the server can give the message: ‘Not responding’

This is due to excessive use of resources of the server.

 

Always be aware of the server you’re working on, and the time you’re using this script.

 

Copy the powershell script, to a location on your server, for example c:\scripts

 

In this example the folder with the large amount files is F:\Test

 

Start Powershell as an administrator

 

Change the directory to the location where the script is located C:\scripts\Copy-files-Year-Month

Execute the script :

.\Copy-files-Year-Month-v11.ps1

 

Fill in the location of the folder which contains the files.

 

This action can take a while based upon the amount of files to be moved.

 

All of the files are moved to the sub folders based upon year and month

Your now able to zip,archive of delete the files in smaller groups based upon one or more folders.

Download:

Copy-files-Year-Month-v11-uk

<#
.SYNOPSIS
Move file to sub folder

.DESCRIPTION
This scripts moves files from a folder, to newly created folders. These folders are created based upon the Year and month of the last write time of the file.
You'll have to put in the folder name when executing this script.

.INPUTS
Only the name of the folder to-be moved

.OUTPUTS
Thes script creates the folders by itself, based upon year and month of the last write time.

.Prerequisites
Nothing

.EXAMPLE
Roep deze module aan dmv :
.\Copy-files-Year-Month-v11.ps1

.EXAMPLE
Roep deze module aan dmv :
.\Copy-files-Year-Month-v11.ps1 -Verbose

.LINK
http://www.vspbreda.nl

.NOTES
Written By: Richard Voogt
Website:	http://www.vspbreda.nl
Twitter:	http://twitter.com/rvoogt

Change Log
V1.0, 26-6-2015 - First version
V1.1, 28-6-2015 - Second version => Changed $Monthpath

#>

$Filepath = ""
$file = ""
$date  = ""
$month   = ""
$year    = ""
$MonthPath	 = ""
	
$FilePath = Read-Host "Place the directory which contains the files."

Write-Warning "Note: This action can take several minutes, depending on the amount of files in $FilePath."

get-childitem $FilePath | % {

    $file = $_.FullName 
	$date = Get-Date ($_.LastWriteTime)
	
    $month = $date.month
    $year = $date.year
	$MonthPath = "$FilePath\$year\$month"
	Write-Verbose "month = $month"
	Write-Verbose "Date = $date"
	Write-Verbose "year = $year"
	Write-Verbose "FilePath = $FilePath" 
	Write-Verbose "Filename = $file"
	Write-Verbose "MonthPath = $MonthPath"

	if(!(Test-Path -Path "$MonthPath" )){
        Write-Verbose "Creating log location $MonthPath."
        #Write-Host -backgroundcolor green -ForegroundColor black "Creating log location $MonthPath."
		Write-Verbose "MonthPath inside path test = $MonthPath"
        New-Item -ItemType directory -Path $MonthPath | Out-null
    }
    ELSE {
        #Write-Host -backgroundcolor green -ForegroundColor black "Log location exists already exist $MonthPath"
        Write-Verbose "Log location exists already exist $MonthPath" 
        }
	move-item "$file" "$MonthPath" | Out-null
}

Write-Warning "All files are sorted now based upon year and month."