-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch.cpp
More file actions
373 lines (330 loc) · 10.8 KB
/
touch.cpp
File metadata and controls
373 lines (330 loc) · 10.8 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* touch.cpp - Windows implementation of UNIX touch utility
*
* Usage:
* touch [filespec] - Touch files matching filespec in current directory
* touch -r [filespec] - Recursively touch files matching filespec
* touch -v [filespec] - Verbose mode (show old/new timestamps)
* touch -f [filespec] - Touch folders as well as files
* touch -r -v [filespec] - Recursive and verbose
* touch -r -f [filespec] - Recursive, touching files and folders
* touch - Touch all files in current directory
*
* Sets LastAccessed, DateModified, and Created timestamps to current time
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
/*
* Function prototypes
*/
void TouchFile(const char * filename, BOOL verbose, const FILETIME * newTime) ;
void TouchFolder(const char * foldername, BOOL verbose, const FILETIME * newTime) ;
void ProcessDirectory(const char * filespec, BOOL recurse, BOOL verbose, BOOL touchFolders) ;
void ProcessFilesInDirectory(const char * path, const char * pattern, BOOL recurse, BOOL verbose, BOOL touchFolders) ;
void FormatFileTime(const FILETIME * ft, char * buffer, int bufferSize) ;
void ShowUsage() ;
int main(int argc, char * argv[])
{
BOOL recurse = FALSE ;
BOOL verbose = FALSE ;
BOOL touchFolders = FALSE ;
const char * filespec = "*" ;
/*
* Show usage if no arguments provided
*/
if (argc == 1)
{
ShowUsage() ;
return 0 ;
}
/*
* Parse command line arguments
*/
if (argc > 1)
{
int argIndex = 1 ;
/*
* Check for flags
*/
while (argIndex < argc && (argv[argIndex][0] == '-' || argv[argIndex][0] == '/'))
{
if (strcmp(argv[argIndex], "-r") == 0 || strcmp(argv[argIndex], "/r") == 0)
{
recurse = TRUE ;
argIndex++ ;
}
else if (strcmp(argv[argIndex], "-v") == 0 || strcmp(argv[argIndex], "/v") == 0)
{
verbose = TRUE ;
argIndex++ ;
}
else if (strcmp(argv[argIndex], "-f") == 0 || strcmp(argv[argIndex], "/f") == 0)
{
touchFolders = TRUE ;
argIndex++ ;
}
else
{
printf("Unknown flag: %s\n", argv[argIndex]) ;
ShowUsage() ;
return 1 ;
}
}
/*
* Get filespec if provided
*/
if (argIndex < argc)
{
filespec = argv[argIndex] ;
}
}
/*
* Process the directory
*/
ProcessDirectory(filespec, recurse, verbose, touchFolders) ;
return 0 ;
}
/*
* TouchFile - Set all timestamps on a file to current time
*/
void TouchFile(const char * filename, BOOL verbose, const FILETIME * newTime)
{
HANDLE hFile ;
FILETIME oldCreateTime, oldAccessTime, oldWriteTime ;
char oldTimeStr[64] ;
char newTimeStr[64] ;
/*
* Open the file to read current timestamps first
*/
if (verbose)
{
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hFile != INVALID_HANDLE_VALUE)
{
GetFileTime(hFile, &oldCreateTime, &oldAccessTime, &oldWriteTime) ;
CloseHandle(hFile) ;
/*
* Format old modified time (most meaningful timestamp)
*/
FormatFileTime(&oldWriteTime, oldTimeStr, sizeof(oldTimeStr)) ;
}
else
{
strcpy_s(oldTimeStr, sizeof(oldTimeStr), "Unknown") ;
}
}
/*
* Open the file with write attributes access
*/
hFile = CreateFile(filename, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Error: Cannot touch '%s' - %d\n", filename, GetLastError()) ;
return ;
}
/*
* Set creation time, last access time, and last write time
*/
if (!SetFileTime(hFile, newTime, newTime, newTime))
{
printf("Error: Failed to set time on '%s' - %d\n", filename, GetLastError()) ;
}
else
{
if (verbose)
{
FormatFileTime(newTime, newTimeStr, sizeof(newTimeStr)) ;
printf("%s: %s -> %s\n", filename, oldTimeStr, newTimeStr) ;
}
else
{
printf("Touched: %s\n", filename) ;
}
}
CloseHandle(hFile) ;
}
/*
* TouchFolder - Set all timestamps on a folder to current time
*/
void TouchFolder(const char * foldername, BOOL verbose, const FILETIME * newTime)
{
HANDLE hDir ;
FILETIME oldCreateTime, oldAccessTime, oldWriteTime ;
char oldTimeStr[64] ;
char newTimeStr[64] ;
/*
* Open the directory to read current timestamps first
*/
if (verbose)
{
hDir = CreateFile(foldername, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL) ;
if (hDir != INVALID_HANDLE_VALUE)
{
GetFileTime(hDir, &oldCreateTime, &oldAccessTime, &oldWriteTime) ;
CloseHandle(hDir) ;
/*
* Format old modified time (most meaningful timestamp)
*/
FormatFileTime(&oldWriteTime, oldTimeStr, sizeof(oldTimeStr)) ;
}
else
{
strcpy_s(oldTimeStr, sizeof(oldTimeStr), "Unknown") ;
}
}
/*
* Open the directory with write attributes access
*/
hDir = CreateFile(foldername, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL) ;
if (hDir == INVALID_HANDLE_VALUE)
{
printf("Error: Cannot touch folder '%s' - %d\n", foldername, GetLastError()) ;
return ;
}
/*
* Set creation time, last access time, and last write time
*/
if (!SetFileTime(hDir, newTime, newTime, newTime))
{
printf("Error: Failed to set time on folder '%s' - %d\n", foldername, GetLastError()) ;
}
else
{
if (verbose)
{
FormatFileTime(newTime, newTimeStr, sizeof(newTimeStr)) ;
printf("%s [DIR]: %s -> %s\n", foldername, oldTimeStr, newTimeStr) ;
}
else
{
printf("Touched folder: %s\n", foldername) ;
}
}
CloseHandle(hDir) ;
}
/*
* ProcessDirectory - Process files in directory (and subdirectories if recurse=TRUE)
*/
void ProcessDirectory(const char * filespec, BOOL recurse, BOOL verbose, BOOL touchFolders)
{
char currentDir[MAX_PATH] ;
/*
* Get current directory
*/
GetCurrentDirectory(MAX_PATH, currentDir) ;
/*
* Process files in current directory
*/
ProcessFilesInDirectory(currentDir, filespec, recurse, verbose, touchFolders) ;
}
/*
* ProcessFilesInDirectory - Process files matching pattern in specified directory
*/
void ProcessFilesInDirectory(const char * path, const char * pattern, BOOL recurse, BOOL verbose, BOOL touchFolders)
{
WIN32_FIND_DATA findData ;
HANDLE hFind ;
char searchPath[MAX_PATH] ;
char fullPath[MAX_PATH] ;
FILETIME newTime ;
SYSTEMTIME st ;
/*
* Get current system time once for all files
*/
GetSystemTime(&st) ;
SystemTimeToFileTime(&st, &newTime) ;
/*
* First, process all files matching the pattern
*/
sprintf_s(searchPath, sizeof(searchPath), "%s\\%s", path, pattern) ;
hFind = FindFirstFile(searchPath, &findData) ;
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
sprintf_s(fullPath, sizeof(fullPath), "%s\\%s", path, findData.cFileName) ;
/*
* Touch files
*/
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
TouchFile(fullPath, verbose, &newTime) ;
}
/*
* Touch folders if -f flag is set
*/
else if (touchFolders && strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0)
{
TouchFolder(fullPath, verbose, &newTime) ;
}
}
while (FindNextFile(hFind, &findData)) ;
FindClose(hFind) ;
}
/*
* If recursing, process subdirectories
*/
if (recurse)
{
sprintf_s(searchPath, sizeof(searchPath), "%s\\*", path) ;
hFind = FindFirstFile(searchPath, &findData) ;
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
/*
* Process directories (but skip . and ..)
*/
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0)
{
sprintf_s(fullPath, sizeof(fullPath), "%s\\%s", path, findData.cFileName) ;
if (!verbose) printf("\nEntering directory: %s\n", fullPath) ;
ProcessFilesInDirectory(fullPath, pattern, recurse, verbose, touchFolders) ;
}
}
while (FindNextFile(hFind, &findData)) ;
FindClose(hFind) ;
}
}
}
/*
* FormatFileTime - Convert FILETIME to readable string
*/
void FormatFileTime(const FILETIME * ft, char * buffer, int bufferSize)
{
SYSTEMTIME st ;
FILETIME localFt ;
/*
* Convert to local time
*/
FileTimeToLocalFileTime(ft, &localFt) ;
FileTimeToSystemTime(&localFt, &st) ;
/*
* Format as: YYYY-MM-DD HH:MM:SS
*/
sprintf_s(buffer, bufferSize, "%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond) ;
}
/*
* ShowUsage - Display usage information
*/
void ShowUsage()
{
puts("Windows Touch Utility") ;
puts("Usage:") ;
puts(" touch [options] [filespec]") ;
puts("") ;
puts("Options:") ;
puts(" -r Recursively process subdirectories") ;
puts(" -v Verbose mode (show old/new timestamps)") ;
puts(" -f Touch folders as well as files") ;
puts("") ;
puts("Examples:") ;
puts(" touch *.exe - Touch all .exe files in current directory") ;
puts(" touch -r *.txt - Touch all .txt files recursively") ;
puts(" touch -v readme.md - Touch file with verbose output") ;
puts(" touch -f * - Touch all files and folders in current directory") ;
puts(" touch -r -f - Touch all files and folders recursively") ;
puts(" touch -r -v -f *.log - Touch all .log files and folders recursively (verbose)") ;
puts(" \n Written by Peet Morris.");
}