1: #----------------------------------------------------------------
2: # Touch.ps1
3: #----------------------------------------------------------------
4: param
5: (
6: [string]$filespec = $null, # Files to touch
7: [DateTime]$datetime = ([DateTime]::Now), # Use DateTime STRING instead of current time.
8: [int]$forward = 0, # Modify the time by going forward SECONDS s.
9: [string]$reference = $null, # Use this file's time instead of current time.
10: [bool]$only_modification = $false, # Change only the modification time.
11: [bool]$only_access = $false # Change only the access time
12: );
13:
14: #----------------------------------------------------------------
15: # Function Do-Touch
16: #----------------------------------------------------------------
17: function Do-Touch {
18: param(
19: [string]$filespec = $null,
20: [datetime]$datetime = ([DateTime]::Now),
21: [int]$forward = 0,
22: [string]$reference = $null,
23: [bool]$only_modification = $false,
24: [bool]$only_access = $false
25: );
26:
27: $touch = $null;
28:
29: if ( $filespec )
30: {
31: $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue)
32: if ( !$files )
33: {
34: # If file doesn't exist, attempt to create one.
35: # A wildcard patter will fail silently.
36: Set-Content -Path $filespec -value $null;
37: $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue);
38: }
39:
40: if ( $files )
41: {
42: if ( $reference )
43: {
44: $reffile = Get-ChildItem -Path $reference -ErrorAction SilentlyContinue;
45: if ( $reffile )
46: {
47: [datetime]$touch = $reffile.LastAccessTime.AddSeconds($forward);
48: }
49: }
50: elseif ( $datetime )
51: {
52: [datetime]$touch = $datetime.AddSeconds($forward);
53: }
54:
55: if ( $touch )
56: {
57: [DateTime]$UTCTime = $touch.ToUniversalTime();
58: foreach ($file in $files)
59: {
60: if ( $only_access )
61: {
62: $file.LastAccessTime=$touch
63: $file.LastAccessTimeUtc=$UTCTime
64: }
65: elseif ( $only_modification )
66: {
67: $file.LastWriteTime=$touch
68: $file.LastWriteTimeUtc=$UTCTime
69: }
70: else
71: {
72: $file.CreationTime = $touch;
73: $file.CreationTimeUtc = $UTCTime;
74: $file.LastAccessTime=$touch
75: $file.LastAccessTimeUtc=$UTCTime
76: $file.LastWriteTime=$touch
77: $file.LastWriteTimeUtc=$UTCTime
78: }
79: $file | select Name, *time*
80: }
81: }
82: }
83: }
84: }
85:
86: Do-Touch -filespec $filespec -datetime $datetime -forward $forward -reference $reference `
87: -only_modification $only_modification -only_access $only_access;