-
Notifications
You must be signed in to change notification settings - Fork 72
/
touch.ahk
134 lines (113 loc) · 3.64 KB
/
touch.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Function : Touch(file = "" [, set = "0", WhichTime = "M", Reference = "0"] )
Language : AutoHotkey 1.1+
Author : hi5
Purpose : Function to set, store and restore the datetime stamp
of a file or folder. Useful when you are modifying
files and if you want to keep the original datetime stamp.
The datetime stamp properties are stored in a static object.
History : Version 0.1 - 29 september 2013
Options :
file = file name to (re)store the datetime stamp of.
If you want to "delete" all stored settings, simply call
Touch() to clear the static object.
Set = 0 Update the datetime stamp of a file or folder to current datetime
making it a simple shorthand for the FileSetTime command.
(this is the default if the parameter is blank or omitted)
Set = 1 Store the datetime stamp of a file or folder
Set = 2 Restore the datetime stamp of a file or folder
Set = 3 Update the datetime stamp of a file or folder which was previously
stored using Set=1
WhichTime = Which timestamp to get/set. Value: M, C, or A
M: Modification time (this is the default if the parameter is blank or omitted)
C: Creation time
A: Last access time
More info here:
- http://www.autohotkey.com/docs/commands/FileGetTime.htm
- http://www.autohotkey.com/docs/commands/FileSetTime.htm
Reference = File, use this file's timestamp instead of current time
Returns ErrorLevel - see AutoHotkey documentation
- http://www.autohotkey.com/docs/misc/ErrorLevel.htm
Example :
MsgBox % Touch("file.txt",1) ; store datetime stap
MsgBox % Touch("file.txt") ; update to current date & time, a shorthand for FileSetTime
MsgBox % "file.txt should be the newest file in the directory."
MsgBox % Touch("file.txt",2) ; restore old datetime stamp
Touch() ; free memory
*/
Touch(file = "" , set = "0", WhichTime = "M", Reference = "0") {
static Files
; Clear all stored settings
If (file = "")
{
Files:=[] ; clear object
Return 0
}
If (Reference = "0")
usetime:=A_Now
else
{
IfExist, %Reference%
FileGetTime, usetime, %Reference%, %WhichTime%
}
Found:=0
If !InStr(file,"\")
file:=A_ScriptDir "\" file
If !IsObject(files)
{
Files:=[]
}
; set = 0 Update the datetime stamp
If (Set = 0)
{
FileSetTime, %usetime%, %file%, %WhichTime%
Return ErrorLevel
}
; set = 1 Store datetime stamp of a file or folder
; set = 3 Update the datetime stamp of a file or folder which was previously
; stored (using 1)
If ((Set = 1) or (Set = 3))
{
Loop, % Files.MaxIndex()
{
If (Files[A_Index].FileName = file)
{
Index:=A_Index
Found++
Break
}
}
If ((Found = 0) or (Set = 3)) ; either new or update info
{
If (Set <> 3) ; it is a new file
{
Index:=Files.MaxIndex() + 1
If (Index = "")
Index:=1
}
If (Reference = "0")
FileGetTime, stamp, %file%, %WhichTime%
else
stamp:=usetime
Files[Index,"FileName"]:=file
Files[Index,"FileStamp"]:=stamp
Files[Index,"WhichTime"]:=WhichTime
}
Return ErrorLevel
}
; set = 2 Restore the datetime stamp of a file or folder
If (Set = 2)
{
Loop, % Files.MaxIndex()
{
If (Files[A_Index].FileName = file)
{
stamp:=Files[A_Index].FileStamp
WhichTime:=Files[A_Index].WhichTime
FileSetTime, %stamp%, %file%, %WhichTime%
Break
}
}
Return ErrorLevel
}
}