From 62fdb9106dfa54306acb51904506678cc7a36eb9 Mon Sep 17 00:00:00 2001 From: Nicole Rappe Date: Thu, 11 Jan 2024 20:36:27 -0700 Subject: [PATCH] Add Scripts/Powershell/General Purpose/File Finder.md --- .../Powershell/General Purpose/File Finder.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Scripts/Powershell/General Purpose/File Finder.md diff --git a/Scripts/Powershell/General Purpose/File Finder.md b/Scripts/Powershell/General Purpose/File Finder.md new file mode 100644 index 0000000..4fb1db6 --- /dev/null +++ b/Scripts/Powershell/General Purpose/File Finder.md @@ -0,0 +1,37 @@ +**Purpose**: +Locate specific files, and copy them with a renamed datestamp appended to a specific directory. + +``` powershell +# Define an array of objects, each having a prefix and a suffix +$files = @( + @{Prefix="name"; Suffix="Extension"}, + @{Prefix="name"; Suffix="Extension"} +) + +# Define the destination directory +$destination = "C:\folder\to\copy\to" + +# Loop over the file name patterns +foreach ($file in $files) { + # Search for files that start with the current prefix and end with the current suffix + $matches = Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue -Filter "$($file.Prefix)*.$($file.Suffix)" + + # Loop over the matching files + foreach ($match in $matches) { + # Get the file's last modified date + $lastModifiedDate = $match.LastWriteTime + + # Get the file's owner + $owner = (Get-Acl -Path $match.FullName).Owner + + # Output the file name, last modified date, and owner + Write-Output "File: $($match.FullName), Last Modified Date: $lastModifiedDate, Owner: $owner" + + # Generate a unique name for the copied file by appending the last modified date and time + $newName = "{0}_{1:yyyyMMdd_HHmmss}{2}" -f $match.BaseName, $lastModifiedDate, $match.Extension + + # Copy the file to the destination directory with the new name + Copy-Item -Path $match.FullName -Destination (Join-Path -Path $destination -ChildPath $newName) + } +} +``` \ No newline at end of file