功能:把文件夹内的文件修改时间和文件名合并在一起作为新的文件名出现的问题是在有的机上能很好的运行,但在一些机子上出现遍历两次的情况(也就是文件名被改了两次,以致于有两个时间)Sub GetNewFileName()
Dim nchar As Integer
Dim strPathName As String
Dim tFindData As WIN32_FIND_DATA
Dim sFileTime As String
Dim lngresult As Long
Dim lnghandle As Long
Dim closehandle As Long
Dim bll As Boolean
tFindData.cFileName = "" '初始化定长字符串
strPathName = Dir1.Path & "\" & "*.MPG"
'获取句柄
lnghandle = FindFirstFile(strPathName, tFindData)
closehandle = lnghandle
Do While lnghandle <> INVALID_HANDLE_VALUE '如果获得文件句柄成功
strFileName = sGetCrtFileName(tFindData.cFileName)
sFileTime = Dir1.Path & "\" & strFileName
lngresult = CreateFile(sFileTime, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If lngresult = INVALID_HANDLE_VALUE Then
Exit Sub
End If
'获得文件的时间信息
lngresult = GetFileTime(lngresult, ftCreatetime, ftAccesstime, ftModifyTime)
If lngresult <> 0 Then '如果获得文件时间信息成功
lngresult = FileTimeToLocalFileTime(ftModifyTime, ftlocal)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
'转换本地时间到系统时间
lngresult = FileTimeToSystemTime(ftlocal, ftSystem)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
With ftSystem
sMonth = CStr(.wMonth)
sDay = CStr(.wDay)
sHour = CStr(.wHour)
sMinute = CStr(.wMinute)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
If Len(sHour) = 1 Then
sHour = "0" & sHour
End If
If Len(sMinute) = 1 Then
sMinute = "0" & sMinute
End If
ModifyTime = CStr(.wYear) & "." & sMonth & "." & sDay & "." & sHour & "." & sMinute
sPrtTime = sHour & "." & sMinute
End With
End If
End If
End If
If blPrint = True Then
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
sNewName = Dir1.Path & "\" & ModifyTime & sOldName & cmbAddress & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
'命名新文件
bll = ReName(0, sOldName, sNewName)
Else
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
If chkPrtTime.Value Then
sNewName = Dir1.Path & "\" & sPrtTime & sOldName & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
bll = ReName(0, sOldName, sNewName)
Else
sNewName = sOldName
End If
End If
tFindData.cFileName = "" If FindNextFile(lnghandle, tFindData) = 0 Then '查询结束或发生错误
Close lngresult
FindClose (closehandle)
Exit Do
End If
Loop
Exit SubEnd Sub
Dim nchar As Integer
Dim strPathName As String
Dim tFindData As WIN32_FIND_DATA
Dim sFileTime As String
Dim lngresult As Long
Dim lnghandle As Long
Dim closehandle As Long
Dim bll As Boolean
tFindData.cFileName = "" '初始化定长字符串
strPathName = Dir1.Path & "\" & "*.MPG"
'获取句柄
lnghandle = FindFirstFile(strPathName, tFindData)
closehandle = lnghandle
Do While lnghandle <> INVALID_HANDLE_VALUE '如果获得文件句柄成功
strFileName = sGetCrtFileName(tFindData.cFileName)
sFileTime = Dir1.Path & "\" & strFileName
lngresult = CreateFile(sFileTime, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If lngresult = INVALID_HANDLE_VALUE Then
Exit Sub
End If
'获得文件的时间信息
lngresult = GetFileTime(lngresult, ftCreatetime, ftAccesstime, ftModifyTime)
If lngresult <> 0 Then '如果获得文件时间信息成功
lngresult = FileTimeToLocalFileTime(ftModifyTime, ftlocal)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
'转换本地时间到系统时间
lngresult = FileTimeToSystemTime(ftlocal, ftSystem)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
With ftSystem
sMonth = CStr(.wMonth)
sDay = CStr(.wDay)
sHour = CStr(.wHour)
sMinute = CStr(.wMinute)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
If Len(sHour) = 1 Then
sHour = "0" & sHour
End If
If Len(sMinute) = 1 Then
sMinute = "0" & sMinute
End If
ModifyTime = CStr(.wYear) & "." & sMonth & "." & sDay & "." & sHour & "." & sMinute
sPrtTime = sHour & "." & sMinute
End With
End If
End If
End If
If blPrint = True Then
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
sNewName = Dir1.Path & "\" & ModifyTime & sOldName & cmbAddress & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
'命名新文件
bll = ReName(0, sOldName, sNewName)
Else
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
If chkPrtTime.Value Then
sNewName = Dir1.Path & "\" & sPrtTime & sOldName & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
bll = ReName(0, sOldName, sNewName)
Else
sNewName = sOldName
End If
End If
tFindData.cFileName = "" If FindNextFile(lnghandle, tFindData) = 0 Then '查询结束或发生错误
Close lngresult
FindClose (closehandle)
Exit Do
End If
Loop
Exit SubEnd Sub
解决方案 »
- 有什么办法可以改变VB MSCOMM控件的波特率吗?
- 这段程序为什么已经能看到excel一瞬间被打开,但马上又自动关闭了?
- 如何判断鼠标移出了窗体?
- 信誉分被扣 散分高兴高兴。结分方法:几楼几分,直到100分没有了为止!
- DDE的linkpoke问题
- 用FSO,为何本想建目录却成了建未知类型的文件??
- execute 会不会锁死数据库?
- 哎呀!难死我了!众位哥哥救火呀!
- 如何制作Word的插件?(就像金山词霸那样的)
- *** foolishtiger, please come in, urgent!!! Please do not delete this (enmity) ***
- MSHFlexGrid更新
- MSHFlexGrid 数据更新,SQL形式。
Dim nchar As Integer
Dim strPathName As String
Dim tFindData As WIN32_FIND_DATA
Dim sFileTime As String
Dim lngresult As Long
Dim lnghandle As Long
Dim closehandle As Long
Dim bll As Boolean
Dim strMyFile() As String, i As Long tFindData.cFileName = "" '初始化定长字符串
strPathName = Dir1.Path & "\" & "*.MPG"
'获取句柄
i = 0
lnghandle = FindFirstFile(strPathName, tFindData)
closehandle = lnghandle
Do While lnghandle <> INVALID_HANDLE_VALUE '如果获得文件句柄成功
strFileName = sGetCrtFileName(tFindData.cFileName)
sFileTime = Dir1.Path & "\" & strFileName ReDim Preserve strMyFile(i)
strMyFile(i) = sFileTime tFindData.cFileName = ""
If FindNextFile(lnghandle, tFindData) = 0 Then '查询结束或发生错误
Close lngresult
FindClose (closehandle)
Exit Do
End If
Loop For i = 0 To Ubound(strMyFile)
lngresult = CreateFile(strMyFile(i), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If lngresult = INVALID_HANDLE_VALUE Then
Exit Sub
End If
'获得文件的时间信息
lngresult = GetFileTime(lngresult, ftCreatetime, ftAccesstime, ftModifyTime)
If lngresult <> 0 Then '如果获得文件时间信息成功
lngresult = FileTimeToLocalFileTime(ftModifyTime, ftlocal)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
'转换本地时间到系统时间
lngresult = FileTimeToSystemTime(ftlocal, ftSystem)
If lngresult <> 0 Then '如果转换本地时间到系统时间成功
With ftSystem
sMonth = CStr(.wMonth)
sDay = CStr(.wDay)
sHour = CStr(.wHour)
sMinute = CStr(.wMinute)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
If Len(sHour) = 1 Then
sHour = "0" & sHour
End If
If Len(sMinute) = 1 Then
sMinute = "0" & sMinute
End If
ModifyTime = CStr(.wYear) & "." & sMonth & "." & sDay & "." & sHour & "." & sMinute
sPrtTime = sHour & "." & sMinute
End With
End If
End If
End If
If blPrint = True Then
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
sNewName = Dir1.Path & "\" & ModifyTime & sOldName & cmbAddress & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
'命名新文件
bll = ReName(0, sOldName, sNewName)
Else
nchar = Len(strFileName)
sOldName = Left$(strFileName, nchar - 4)
If chkPrtTime.Value Then
sNewName = Dir1.Path & "\" & sPrtTime & sOldName & ".MPG"
sOldName = Dir1.Path & "\" & sOldName & ".MPG"
bll = ReName(0, sOldName, sNewName)
Else
sNewName = sOldName
End If
End If
Next i
Exit SubEnd Sub
http://community.csdn.net/Expert/TopicView3.asp?id=5734883