如何批量修改文件创建时间?
比如我有文件名为10012623.doc的文件意思是10年1月26号23点生成的文件,我想把文件的创建时间就改成这个时间,如何来操作?最好用C,C++或C#来实现。要求是批量修改,因为有大量的文件。谢谢
[解决办法]
到我的资源http://download.csdn.net/zhao4zhong1里面下载“修改文件或文件夹日期时间的命令行工具”这里特给出源码
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main(ByVal CmdArgs() As String)
Dim ArgNum As Integer ' Index of individual command-line argument.
Dim dt As DateTime
Dim path As String
Dim offset as integer
Dim opt as string
Dim s as string
Dim isGet as boolean
isGet=False
offset=0
opt="/W"
dt=DateTime.Now
If CmdArgs.Length > 0 Then ' See if there are any arguments.
For ArgNum = 0 To CmdArgs.Length-1
if ArgNum=0+offset Then
s=CmdArgs(ArgNum)
if s="/?" then
Console.WriteLine("File or directory Datetime(WAC:lastWrite|lastAccess|Creation)")
Console.WriteLine("Set to Now or YY-MM-DD [hh:mm:ss] or Get")
Console.WriteLine(" FD [/WAC] File_or_Dir_name [[[20]YY-]MM-DD[ hh:mm:ss]]|get")
return
end if
if s.Chars(0)="/" then
opt=s.ToUpper()
if opt.IndexOf("C")=-1 and opt.IndexOf("A")=-1 then
opt=opt+"W"
end if
offset=1
else
path=s
end if
End if
if ArgNum=1+offset Then
s=CmdArgs(ArgNum)
if s.ToUpper()="GET" then
isGet=True
else
Try
dt=DateTime.Parse(s)
Catch e As Exception
Console.WriteLine("Error: Invalid datetime format")
return
End Try
end if
End if
if ArgNum=2+offset Then
Try
dt=DateTime.Parse(CmdArgs(ArgNum-1)+" "+CmdArgs(ArgNum))
Exit for
Catch e As Exception
Console.WriteLine("Error: Invalid datetime format")
return
End Try
End if
Next ArgNum
else
Console.WriteLine("File or directory Datetime(WAC:lastWrite|lastAccess|Creation)")
Console.WriteLine("Set to Now or YY-MM-DD [hh:mm:ss] or Get")
Console.WriteLine(" FD [/WAC] File_or_Dir_name [[[20]YY-]MM-DD[ hh:mm:ss]]|get")
return
End If
Try
If File.Exists(path) = True Then
if opt.IndexOf("W")>-1 then
if isGet then
dt=File.GetLastWriteTime(path)
Console.WriteLine("File Datetime(LastWrite ) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetLastWriteTime(path, dt)
Console.WriteLine("File Datetime(LastWrite ) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("C")>-1 then
if isGet then
dt=File.GetCreationTime(path)
Console.WriteLine("File Datetime(Creation ) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetCreationTime(path, dt)
Console.WriteLine("File Datetime(Creation ) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("A")>-1 then
if isGet then
dt=File.GetLastAccessTime(path)
Console.WriteLine("File Datetime(LastAccess) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetLastAccessTime(path, dt)
Console.WriteLine("File Datetime(LastAccess) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
return
end if
Catch e As Exception
Console.WriteLine("Error: {0}", e.ToString())
return
End Try
Try
If File.Exists(path+".lnk") = True Then
if opt.IndexOf("W")>-1 then
if isGet then
dt=File.GetLastWriteTime(path+".lnk")
Console.WriteLine("File Datetime(LastWrite ) of [{0}] is {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetLastWriteTime(path+".lnk", dt)
Console.WriteLine("File Datetime(LastWrite ) of [{0}] set to {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("C")>-1 then
if isGet then
dt=File.GetCreationTime(path+".lnk")
Console.WriteLine("File Datetime(Creation ) of [{0}] is {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetCreationTime(path+".lnk", dt)
Console.WriteLine("File Datetime(Creation ) of [{0}] set to {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("A")>-1 then
if isGet then
dt=File.GetLastAccessTime(path+".lnk")
Console.WriteLine("File Datetime(LastAccess) of [{0}] is {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
File.SetLastAccessTime(path+".lnk", dt)
Console.WriteLine("File Datetime(LastAccess) of [{0}] set to {1}",path+".lnk",dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
return
end if
Catch e As Exception
Console.WriteLine("Error: {0}", e.ToString())
return
End Try
Try
If Directory.Exists(path) = True Then
if opt.IndexOf("W")>-1 then
if isGet then
dt=Directory.GetLastWriteTime(path)
Console.WriteLine("Directory Datetime(LastWrite ) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
Directory.SetLastWriteTime(path, dt)
Console.WriteLine("Directory Datetime(LastWrite ) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("C")>-1 then
if isGet then
dt=Directory.GetCreationTime(path)
Console.WriteLine("Directory Datetime(Creation ) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
Directory.SetCreationTime(path, dt)
Console.WriteLine("Directory Datetime(Creation ) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
if opt.IndexOf("A")>-1 then
if isGet then
dt=Directory.GetLastAccessTime(path)
Console.WriteLine("Directory Datetime(LastAccess) of [{0}] is {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
else
Directory.SetLastAccessTime(path, dt)
Console.WriteLine("Directory Datetime(LastAccess) of [{0}] set to {1}",path,dt.ToString("yyyy-MM-dd HH:mm:ss"))
end if
end if
End If
return
Catch e As Exception
Console.WriteLine("Error: {0}", e.ToString())
return
End Try
Console.WriteLine("Error: Can not find File or File.lnk or Dir [{0}]",path)
return
End Sub
End Class