# encoding: ascii
# api: powershell
# title: Recursive File Update
# description: Asks for full path of source file and target directory to recursively update all instances of file within target directory and its sub-directories. There is also an optional prompt to configure whether you want to be prompted for every file copy. No parameters are passed to the script. Run it, type/paste in your paths, and choose your overwrite confirmation choice.
# version: 0.1
# author: redsolar
# license: CC0
# x-poshcode-id: 5241
# x-archived: 2014-07-01T09:07:07
# x-published: 2014-06-14T06:06:00
#
# I’m new to PS scripting. So, I apologize if the script isn’t the best written script.
#
# Write a blank new line for easy, spaced reading
Write-Host
# Full file path location of source file
$SourceFilePath = Read-Host "Source File Path"
# Target directory to compare source file to
$TargetDirectory = Read-Host "Target Directory"
Write-Host
# Prompt user whether they want a prompt for every match before overwriting
$Prompt = Read-Host "Prompt before overwriting (Y or N)?"
# Default confirmation prompt to [Y]es if input is invalid
$Prompt = $Prompt.ToUpper()
if ($Prompt -eq "" -or ($Prompt -ne "Y" -and $Prompt -ne "N")) {
$Prompt = "Y"
}
# Extract just the file name from source file path
$SourceFileName = $SourceFilePath.Substring($SourceFilePath.LastIndexOf('\')+1)
# Get source file modified date to compare to other files
$SourceFileModified = (Get-Item $SourceFilePath).LastWriteTime
Write-Host
Write-Host "Source file modified date is $($SourceFileModified)"
Write-Host
# Recursively find matches
$matches = Get-ChildItem -Path $TargetDirectory -Filter $SourceFileName -Recurse |
Where { $_.LastWriteTime -lt $SourceFileModified } |
Foreach-object {
if ($Prompt -eq "N") {
Copy-Item $SourceFilePath $_.FullName.Substring(0, $_.FullName.LastIndexOf('\'))
Write-Host "$($_.FullName) [$($_.LastWriteTime)] [updated]"
} else {
Write-Host "Match found > $($_.FullName) [$($_.LastWriteTime)]"
Copy-Item -Confirm $SourceFilePath $_.FullName.Substring(0, $_.FullName.LastIndexOf('\'))
}
}
Write-Host