Add Scripts/Powershell/General Purpose/File Finder.md
This commit is contained in:
37
Scripts/Powershell/General Purpose/File Finder.md
Normal file
37
Scripts/Powershell/General Purpose/File Finder.md
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Reference in New Issue
Block a user