Skip to content

Instantly share code, notes, and snippets.

@TheBigBear
Created February 9, 2025 07:45
Show Gist options
  • Save TheBigBear/c691fc57af046b26e8faa49f0853bd38 to your computer and use it in GitHub Desktop.
Save TheBigBear/c691fc57af046b26e8faa49f0853bd38 to your computer and use it in GitHub Desktop.
Fix timestamp time travel between win RDS shared folder files and Mac files
# Script 2: Apply UTC Timestamps on Mac
# Save this as `Apply-Timestamps.ps1`
# Get the current directory
$currentDirectory = Get-Location
# Define the input file
$inputFile = Join-Path -Path $currentDirectory -ChildPath "my_files_timestamps_in_utc.txt"
# Check if the input file exists
if (-not (Test-Path -Path $inputFile)) {
Write-Host "Timestamp file not found: $inputFile"
exit
}
# Read the input file line by line
Get-Content -Path $inputFile | ForEach-Object {
# Split the line into file name and timestamps
$fileName, $lastWriteTimeUtc, $creationTimeUtc, $lastAccessTimeUtc = $_ -split '\|'
# Check if the file exists in the current directory on the Mac
$filePath = Join-Path -Path $currentDirectory -ChildPath $fileName
if (Test-Path -Path $filePath) {
# Convert the UTC timestamps to DateTime objects
$lastWriteTime = [datetime]::ParseExact($lastWriteTimeUtc, "yyyy-MM-dd HH:mm:ss.fff", $null)
$creationTime = [datetime]::ParseExact($creationTimeUtc, "yyyy-MM-dd HH:mm:ss.fff", $null)
$lastAccessTime = [datetime]::ParseExact($lastAccessTimeUtc, "yyyy-MM-dd HH:mm:ss.fff", $null)
# Format timestamps for the 'touch' command
$lastWriteTimeFormatted = $lastWriteTime.ToString("yyyyMMddHHmm.ss")
$lastAccessTimeFormatted = $lastAccessTime.ToString("yyyyMMddHHmm.ss")
# Use 'touch' to set last write and access times
Invoke-Expression "touch -t $lastWriteTimeFormatted '$filePath'"
Invoke-Expression "touch -at $lastAccessTimeFormatted '$filePath'"
Write-Host "Timestamps set for $fileName"
} else {
Write-Host "File not found in current directory: $fileName"
}
}
# Script 1: Capture UTC Timestamps
# Save this as `Capture-Timestamps.ps1`
# Get the current directory
$currentDirectory = Get-Location
# Define the output file
$outputFile = Join-Path -Path $currentDirectory -ChildPath "my_files_timestamps_in_utc.txt"
# Clear the output file if it already exists
if (Test-Path -Path $outputFile) {
Clear-Content -Path $outputFile
}
# Get all files in the current directory and capture their UTC timestamps
Get-ChildItem -Path $currentDirectory -File | ForEach-Object {
$fileName = $_.Name
$lastWriteTimeUtc = $_.LastWriteTimeUtc.ToString("yyyy-MM-dd HH:mm:ss.fff")
$creationTimeUtc = $_.CreationTimeUtc.ToString("yyyy-MM-dd HH:mm:ss.fff")
$lastAccessTimeUtc = $_.LastAccessTimeUtc.ToString("yyyy-MM-dd HH:mm:ss.fff")
# Write the file name and timestamps to the output file
"$fileName|$lastWriteTimeUtc|$creationTimeUtc|$lastAccessTimeUtc" | Out-File -FilePath $outputFile -Append
}
Write-Host "Timestamps captured and saved to $outputFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment