Skip to content

Instantly share code, notes, and snippets.

@verglor
Created June 16, 2024 23:31
Show Gist options
  • Save verglor/3f76cef34d008abe9f3ec1146efe559f to your computer and use it in GitHub Desktop.
Save verglor/3f76cef34d008abe9f3ec1146efe559f to your computer and use it in GitHub Desktop.
# Read the directory from the first command line parameter, fallback to current directory if omitted
param (
[string]$inputDir = (Get-Location).Path
)
# Function to update file and folder dates
function Update-Date {
param (
[string]$path
)
$currentDate = Get-Date
# Update creation and write date for file or folder
Set-ItemProperty -LiteralPath $path -Name CreationTime -Value $currentDate
Set-ItemProperty -LiteralPath $path -Name LastWriteTime -Value $currentDate
}
# Function to traverse files and folders depth-first
function Traverse {
param (
[string]$path
)
# Get directories and files ordered by name
$items = Get-ChildItem -LiteralPath $path | Sort-Object Name
foreach ($item in $items) {
# Print out the current item path
Write-Output "Updating: $($item.FullName)"
# Update dates for the current item
Update-Date -path $item.FullName
if ($item.PSIsContainer) {
# Recursively traverse directories
Traverse -path $item.FullName
}
}
}
# Start traversal from the specified or current directory
Traverse -path $inputDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment