Saturday, May 11, 2013

Power Shell Script for Moving the File from Source Location to Destination Location based on File extension and Tenure wise

Hope this will help.

$SrcFolderPath="D:\SourceFolder"
$DestFolderPath="D:\DestinationFolder"
$Filter=$Filter=".exe",".dll"
function MoveFiles($str)
{
if($str.Attributes -ne "Directory")
{
if ($Filter -contains $str.Extension)
{
if (![string]::IsNullOrEmpty($str.DirectoryName))
{
$a=$str.DirectoryName.Replace($SrcFolderPath,$DestFolderPath)
$b=[string]::Concat($str.DirectoryName,"\$str")
$null= mkdir -Force $a
Write-Host $b
Move-Item -Force $b $a\$str
}
else
{
$a=$DestFolderPath
$b=[string]::Concat($str.DirectoryName,"\$str")
$null= mkdir -Force $a
Write-Host $b
Move-Item -Force $b $a\$str
}
}
}
}
Get-ChildItem -Path $SrcFolderPath -Recurse |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} |ForEach-Object {MoveFiles($_)}

No comments:

Post a Comment