Created
June 16, 2024 23:31
-
-
Save verglor/3f76cef34d008abe9f3ec1146efe559f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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