Skip to content

Instantly share code, notes, and snippets.

@michael-milette
Created December 16, 2018 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-milette/ef4edd5870a85d4eb72438a826871fc8 to your computer and use it in GitHub Desktop.
Save michael-milette/ef4edd5870a85d4eb72438a826871fc8 to your computer and use it in GitHub Desktop.
Touch / Sync file and/or folder (directory) date and time timestamp between two file paths without copying files or folders.
# Filename: touchsync.ps1
# Purpose: To touch / Sync file and/or folder date and time timestamp between two file paths without copying.
# files or folders. The default is to just touch folders. See comment in code to change this.
# Language: PowerShell for Windows.
# Version: 0.1
# Author: Michael Milette
# Copyright 2018 TNG Consulting Inc.
# License: GNU GPL 3.
# Configuration section. Note: Trailing slash must be included in the following paths. Example: "C:\DEV\":
$origpath="G:\";
$newpath="D:\";
# Search the directory tree.
# get-childitem -Path $newpath -recurse | ForEach-Object {
# Searches just folders in the target directory structure tree. To search for files and folders (directories),
# comment line below and uncomment the "get-childitem..." line above.
get-childitem -Directory -Path $newpath -recurse | ForEach-Object {
$newfile = $_.FullName;
$fname = $newfile -replace [regex]::Escape($newpath);
$origfile = "$origpath$fname";
If (Test-Path "$origfile") {
If ( (Get-Item "$newfile") -Is [System.IO.DirectoryInfo]) {
"Dir: Setting ""$newfile"" Last Modified Time to the Last Modified Time of ""$origfile""";
(Get-Item "$newfile").LastWriteTime = (Get-Item "$origfile").LastWriteTime;
} Else {
"File: Setting ""$newfile"" Last Modified Time to the Last Modified Time of ""$origfile""";
(Get-ChildItem "$newfile" ).LastWriteTime = ( Get-ChildItem "$origfile" ).LastWriteTime;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment