Option Explicit '獲取某目錄下的所有子目錄路徑及名稱和檔的路徑及名稱 Public Sub SeachFile(ByVal strPath As String, strSourceEx As String, strObjectEx As String) On Error Resume Next Dim Fso As Object Dim Fol As Object Dim Fil As Object Dim DisFileName As String Dim OldName As String '更新前的名称
Set Fso = CreateObject("Scripting.FileSystemObject") Set Fol = Fso.GetFolder(strPath)
Dim sFileName As String '檔案名(不含副檔名) Dim sFileNameEx As String '副檔名 Dim sFilePath As String '檔路徑
For Each Fil In Fol.Files sFileNameEx = UCase(CreateObject("Scripting.FileSystemObject").GetExtensionName(Fil.Name)) '副檔名 If Trim(sFileNameEx) = "" Then '如果副檔名為空,則跳出本次迴圈 GoTo NoEx End If sFileName = Left(Fil.Name, Len(Fil.Name) - Len(sFileNameEx) - IIf(Len(sFileNameEx) = 0, 0, 1)) OldName = sFileName sFilePath = Fil.ParentFolder '取得父階文件夾路徑 sFilePath = sFilePath & "\" labMsg.Caption = sFilePath & Chr(13) & Fil.Name DoEvents DisFileName = GetFileCreatedTime(CStr(Fil)) If DisFileName = OldName Then '如果更新前后名称一样,则不做修改 GoTo NoEx End If
CheckFileName: If Dir(sFilePath & DisFileName & "." & sFileNameEx, vbDirectory) <> "" Then '存在同名文件 DisFileName = DisFileName & "A" GoTo CheckFileName End If
Fso.MoveFile Fil, sFilePath & DisFileName & "." & sFileNameExNoEx: Next
'掃描子目錄 If SubCheck.Value = 1 Then For Each Fol In Fol.subfolders SeachFile Fol, strSourceEx, strObjectEx Next End If End SubPrivate Sub btnStart_Click()
Call SeachFile(txtPath.Text, "FDD", "FD") MsgBox "OK" End Sub Private Sub DriveList_Change() 'On Error Resume Next DirList.Path = DriveList.Drive End SubPrivate Sub DirList_Change() txtPath.Text = DirList.Path End SubPrivate Function GetFileCreatedTime(lpFileNmae As String) As String Dim fs, f Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile(lpFileNmae) Dim GetTime As String GetFileCreatedTime = Format(f.DateLastModified, "YYYYMMDDHHMMSS") End Function
既然LIST里已经有了指定目录中所有文件名了, 并且有了查找和替换两个TEXT, 那么就用: DIM TMP AS STRING FOR I=0 TO LIST.LISTCOUNT TMP=LIST.LISTITEM(I) IF INSTR(1,TMP,TXT查找.TEXT)>0 THEN TMP=REPLACE(1,TMP,TXT查找.TEXT,TXT替换.TEXT) RENAME LIST.LISTITEM(I),TMP END IF NEXT I 这里没VB,不能试验,函数也记不清了,自己调一下
'改名 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.ListBox1.Items.Clear() If str = "" Then Exit Sub If Me.TextBox1.Text <> "" Then col = System.IO.Directory.GetFiles(str) For i As Integer = 0 To col.Length - 1 oldName = col(i).ToString.Trim bol = oldName.Split(".") newName = str & "/" & Me.TextBox1.Text.ToString.Trim & "." & bol(1) If oldName.Contains(Me.TextBox2.Text.ToString.Trim) Then Rename(oldName, newName) End If Next i End If col = System.IO.Directory.GetFiles(str) For i As Integer = 0 To col.Length - 1 Me.ListBox1.Items.Add(col(i)) Next i End Sub '选择目录 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.ListBox1.Items.Clear() Dim fb As New FolderBrowserDialog fb.ShowNewFolderButton = False fb.ShowDialog() str = fb.SelectedPath If str = "" Then Exit Sub col = System.IO.Directory.GetFiles(str) For i As Integer = 0 To col.Length - 1 Me.ListBox1.Items.Add(col(i)) Next i End Sub '查找文件 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.ListBox2.Items.Clear() If str = "" Then Exit Sub If Me.TextBox2.Text <> "" Then col = System.IO.Directory.GetFiles(str) For i As Integer = 0 To col.Length - 1 If col(i).ToString.Trim.Contains(Me.TextBox2.Text.ToString.Trim) Then Me.ListBox2.Items.Add(col(i)) End If Next i End If End Sub
Option Explicit
'獲取某目錄下的所有子目錄路徑及名稱和檔的路徑及名稱
Public Sub SeachFile(ByVal strPath As String, strSourceEx As String, strObjectEx As String)
On Error Resume Next
Dim Fso As Object
Dim Fol As Object
Dim Fil As Object
Dim DisFileName As String
Dim OldName As String '更新前的名称
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(strPath)
strSourceEx = UCase(strSourceEx)
strObjectEx = UCase(strObjectEx)
Dim sFileName As String '檔案名(不含副檔名)
Dim sFileNameEx As String '副檔名
Dim sFilePath As String '檔路徑
For Each Fil In Fol.Files
sFileNameEx = UCase(CreateObject("Scripting.FileSystemObject").GetExtensionName(Fil.Name)) '副檔名
If Trim(sFileNameEx) = "" Then '如果副檔名為空,則跳出本次迴圈
GoTo NoEx
End If
sFileName = Left(Fil.Name, Len(Fil.Name) - Len(sFileNameEx) - IIf(Len(sFileNameEx) = 0, 0, 1))
OldName = sFileName
sFilePath = Fil.ParentFolder '取得父階文件夾路徑
sFilePath = sFilePath & "\"
labMsg.Caption = sFilePath & Chr(13) & Fil.Name
DoEvents
DisFileName = GetFileCreatedTime(CStr(Fil))
If DisFileName = OldName Then '如果更新前后名称一样,则不做修改
GoTo NoEx
End If
CheckFileName:
If Dir(sFilePath & DisFileName & "." & sFileNameEx, vbDirectory) <> "" Then '存在同名文件
DisFileName = DisFileName & "A"
GoTo CheckFileName
End If
Fso.MoveFile Fil, sFilePath & DisFileName & "." & sFileNameExNoEx:
Next
'掃描子目錄
If SubCheck.Value = 1 Then
For Each Fol In Fol.subfolders
SeachFile Fol, strSourceEx, strObjectEx
Next
End If
End SubPrivate Sub btnStart_Click()
Call SeachFile(txtPath.Text, "FDD", "FD")
MsgBox "OK"
End Sub
Private Sub DriveList_Change()
'On Error Resume Next
DirList.Path = DriveList.Drive
End SubPrivate Sub DirList_Change()
txtPath.Text = DirList.Path
End SubPrivate Function GetFileCreatedTime(lpFileNmae As String) As String
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(lpFileNmae)
Dim GetTime As String
GetFileCreatedTime = Format(f.DateLastModified, "YYYYMMDDHHMMSS")
End Function
并且有了查找和替换两个TEXT,
那么就用:
DIM TMP AS STRING
FOR I=0 TO LIST.LISTCOUNT
TMP=LIST.LISTITEM(I)
IF INSTR(1,TMP,TXT查找.TEXT)>0 THEN
TMP=REPLACE(1,TMP,TXT查找.TEXT,TXT替换.TEXT)
RENAME LIST.LISTITEM(I),TMP
END IF
NEXT I
这里没VB,不能试验,函数也记不清了,自己调一下
两个text文本框,一个查找(包括通配符*),一个替换;
文件名显示在 list中;
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Clear()
If str = "" Then Exit Sub
If Me.TextBox1.Text <> "" Then
col = System.IO.Directory.GetFiles(str)
For i As Integer = 0 To col.Length - 1
oldName = col(i).ToString.Trim
bol = oldName.Split(".")
newName = str & "/" & Me.TextBox1.Text.ToString.Trim & "." & bol(1)
If oldName.Contains(Me.TextBox2.Text.ToString.Trim) Then
Rename(oldName, newName)
End If
Next i
End If
col = System.IO.Directory.GetFiles(str)
For i As Integer = 0 To col.Length - 1
Me.ListBox1.Items.Add(col(i))
Next i
End Sub
'选择目录
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.ListBox1.Items.Clear()
Dim fb As New FolderBrowserDialog
fb.ShowNewFolderButton = False
fb.ShowDialog()
str = fb.SelectedPath
If str = "" Then Exit Sub
col = System.IO.Directory.GetFiles(str)
For i As Integer = 0 To col.Length - 1
Me.ListBox1.Items.Add(col(i))
Next i
End Sub
'查找文件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.ListBox2.Items.Clear()
If str = "" Then Exit Sub
If Me.TextBox2.Text <> "" Then
col = System.IO.Directory.GetFiles(str)
For i As Integer = 0 To col.Length - 1
If col(i).ToString.Trim.Contains(Me.TextBox2.Text.ToString.Trim) Then
Me.ListBox2.Items.Add(col(i))
End If
Next i
End If
End Sub