PoshCode Archive  Artifact [366d813be5]

Artifact 366d813be5e85248eeb13ae29b71bc9aa4338fbb4f06ece0afce84df5c13c86b:

  • File Recursive-File-Update.ps1 — part of check-in [8b9e039c6a] at 2018-06-10 13:52:20 on branch trunk — 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. (user: redsolar size: 2191)

# 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