Copy files with certain extension from on path to to another with folder structure ( File and folder exclusions)


Another unique requirement from one of my friend. He was trying move some build files as part of his deployment, process include stopping services and moving files between the folders and backup the old folders etc.

He tried automating many of the process, requested my help with part of one segment that require copying the files and folders with some folder structure to be copied as it is to a remote server, but with some exclusion and inclusion of folders and files.

  1. Copy all folder with only DLLs, with overwrite any files in it.

#————————————————————
#Purpose : COPY Only DLL files with folder structure- Over write
# Author Vipin Vasudevan
#————————————————————

$sourcefolder = read-host “Enter Source path from  DLLs to copy”
$destination = read-host “Enter Destination path”
$FileType = ‘*.dll’

Get-ChildItem $sourcefolder -Recurse -force | Where-Object {$_.PSIsContainer} |ForEach-Object {
# Copy all ‘*.txt’ files in it to the destination folder
$despath = ($_.Fullname).replace($sourcefolder, $destination)
$destfolder = ($despath).replace($_.Name, “”)

New-Item -Name $_.Name -path $destfolder -ItemType directory
$DestName = ($_.FullName).replace($sourcefolder, $destination)
Copy-Item -Path (Join-Path -Path $_.FullName -ChildPath ‘\*’) -Filter $FileType -Destination $DestName -Force
}

Get-ChildItem $sourcefolder | ?{$_.name -like “*.dll”}| %{copy-item -path $_.FullName -Destination $destination -force}
#———————-Script End————————————–

2. Copy all files and folders except the folder named “Configs” and any files end with *.Config

#————————————————————
#Purpose : Copy all files and folders except Config folder and *.config files – Over write
# Author Vipin Vasudevan
#————————————————————

$sourcefolder = read-host “Enter Source path for copying files”
$destination = read-host “Enter Destination path”
$FileType = ‘*.Config’

Get-ChildItem $sourcefolder -Exclude Configs -Recurse -force | Where-Object {$_.PSIsContainer} |ForEach-Object {
# Copy all ‘*.txt’ files in it to the destination folder
$despath = ($_.Fullname).replace($sourcefolder, $destination)
$destfolder = ($despath).replace($_.Name, “”)

New-Item -Name $_.Name -path $destfolder -ItemType directory
$DestName = ($_.FullName).replace($sourcefolder, $destination)
Copy-Item -Path (Join-Path -Path $_.FullName -ChildPath ‘\*’) -Exclude $FileType -Destination $DestName -Force
}
Get-ChildItem $sourcefolder -Exclude Configs | ?{$_.name -notlike “*.config”}| %{copy-item -path $_.FullName -Destination $destination -force}

#———————-Script End————————————–

Backup segment as follows

#————————————————————
#Purpose : #Backup of Destination Path before copying the data
# Author Vipin Vasudevan
#————————————————————
Write-Host “Taking backup of ”

$date = Get-Date -Format d-MMMM-yyyy
New-Item -Type Directory -Path “\Backup” -Name $date

$source = read-host “Enter Source path from Backup ”
$destination = “\Backup\$date”
copy-Item -Recurse $source -Destination $destination

Hope this will be useful for some one with similar requirement. I would be able to explain each steps if any one interested.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s