My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
//*[cli-fixwdate.cs]*********************[http://code.google.com/p/obremsdk/]*
//
// C# .NET 2.0 CLI Tool: Fix LastWriteTime (DateLastModified)
//
//-[Usage]--------------------------------------------------------------------
//
// Single file:
//
// fixwdate <filename> <newdate>
//
// Multiple files (names must contain new date):
//
// fixwdate /<regular-expression>/
//
// For example:
//
// fixwdate example.txt "2008-01-16 1:15 PM"
//
// Example of RAZR picture files having date fixed:
//
// fixwdate "/^(?<month>\d{2})(?<day>\d{2})(?<year>\d{2})_(?<hour>\d{2})(?<minute>\d{2})(?<second>)/"
//
//-[History]------------------------------------------------------------------
// 2008-01-16 by NeilO .... Created.
//
// (C)opyright 2008++ by Neil C. Obremski; New BSD License
//***********************[http://www.opensource.org/licenses/bsd-license.php]*
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("FixLastWriteTime")]
[assembly: AssemblyDescription("Fixes the LastWriteTime (DateLastModified) property of a single file OR a group of files whose names indicate a date.")]
[assembly: AssemblyCompany("Neil C. Obremski")]
[assembly: AssemblyProduct("FixLastWriteTime")]
[assembly: AssemblyCopyright("Copyright © 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("80a695da-70e4-4861-b18e-abee1840f470")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]

public class FixLastWriteTime
{
static int Main(string[] args)
{
if (0 == args.Length)
{
Console.WriteLine("Missing arguments; " +
"use 'fixwdate <file> <date>' for single file.");
return -1;
}
else if (2 == args.Length)
{
DateTime newdate = DateTime.Parse(args[1]);

if (!File.Exists(args[0]))
{
Console.WriteLine("File not found: {0}", args[0]);
return -2;
}

FileInfo fi = new FileInfo(args[0]);
Console.WriteLine("Set \"{0}\" Modified Date to {1}",
fi.Name, newdate.ToString());
fi.LastWriteTime = newdate;
}
else if (1 == args.Length)
{
// parse the regular expression
string rp = args[0];
if ('/' != rp[0] || '/' != rp[rp.Length - 1])
{
Console.WriteLine("Invalid regular expression; " +
"specify as /<pattern>/");
return -1;
}

Regex re = new Regex(rp.Substring(1, rp.Length-2),
RegexOptions.IgnoreCase);
string[] groups = re.GetGroupNames();

//
// enumerate files in current directory
//
foreach (string fn in Directory.GetFiles(
Directory.GetCurrentDirectory()))
{
FileInfo fi = new FileInfo(fn);
Match m = re.Match(fi.Name);
if (!m.Success)
continue;

// figure out new date time by adding each component to a base
// where each component is relative to TODAY
DateTime d = DateTime.Now;
int year = d.Year;
int month = d.Month;
int day = d.Day;
int hour = d.Hour;
int minute = d.Minute;
int second = d.Second;
for (int i = 0; i < groups.Length; i++)
{
string v = m.Groups[groups[i]].Value;

switch (groups[i])
{
case "year":

if ("" == v)
break;

// year can be 1, 2, or 4 digits
year = Convert.ToInt32(v);
if (year < 80)
year += 2000;
else if (year < 100)
year += 1900;
break;

case "month":

if ("" == v)
break;

month = Convert.ToInt32(v);
break;

case "day":
if ("" == v)
break;

day = Convert.ToInt32(v);
break;

case "hour":
if ("" == v)
hour = 0;
else
hour = Convert.ToInt32(v);
break;

case "minute":
if ("" == v)
minute = 0;
else
minute = Convert.ToInt32(v);
break;

case "second":
if ("" == v)
second = 0;
else
second = Convert.ToInt32(v);
break;
}
}

DateTime newdate = new DateTime(year, month, day,
hour, minute, second);
Console.WriteLine("Set \"{0}\" Modified Date to {1}",
fi.Name, newdate.ToString());
fi.LastWriteTime = newdate;
}
}
else
{
Console.WriteLine("Invalid argument count; " +
"use 'fixwdate <file> <date>' for single file.");
return -1;
}

return 0;

} // Main()

} // FixLastWriteTime class

Change log

r29 by weenie on Jan 16, 2008   Diff
fixwdate (v0.10)
- new utility built from /projects/vs2005/
and put into /bin/windows/
- fixes Date Modified property of single
file or multiple files whose name contains
the date

obrem-http.cs
- corrected name in header comment
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 6041 bytes, 183 lines
Powered by Google Project Hosting