如何用VB批量给文件重命名??
如题
[解决办法]
这个题目我做了一些小的研究,下面是我的代码:批量重命名文件
窗体控件:
DriveListBox
DirListBox
FileListBox
Text1box
Text2box
Text3box
Combobox
Command1 (确定)
Command2 (退出)
label1.caption=选中目录
label2.caption=新文件名
label3.caption=扩展名
'=========================
代码内容
Private Sub Combo1_Click()
Text3.Text = Combo1.List(Combo1.ListIndex)
Text3.Refresh
End Sub
Private Sub Form_Load() '选择默认路径
Dim nn As String '将扩展名存为变量调用
nn = "*. " & Text3.Text
Dir1.Path = App.Path & "\ml "
File1.Pattern = nn
File1.Path = Dir1.Path
Text2.Text = File1.Path
End Sub
Private Sub Drive1_Change() '选择驱动器
Dir1.Path = Drive1.Drive
End Sub
Private Sub Dir1_Change() '选择文件夹
File1.Path = Dir1.Path
Text2.Text = File1.Path
End Sub
Private Sub File1_Click()
Text2.Text = File1.Path & "\ " & File1.FileName
End Sub
Private Sub Command1_Click() '批量重命名文件
Dim tt As String
Dim dd As String
For i = o To File1.ListCount - 1
File1.ListIndex = i
dd = File1.Path & "\ " & File1.FileName
tt = File1.Path & "\ " & Text1.Text & i & ". " & Text3.Text
Name dd As tt '重命名文件
Next i
File1.Refresh
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
nn = "*. " & Combo1.List(Combo1.ListIndex)
File1.Pattern = nn
File1.Refresh
End Sub
[解决办法]
根目录下要有个ml目录
combo1控制的List属性,加上几个后缀,比如*.* jpg bmp....
[解决办法]
我曾經就有個類似的需求,就是把用數碼相機照好的相片根據拍攝時間批次更新文件名
如果需要的話可以把原代碼發給你
jie.vb@hotmail.com
[解决办法]
用vbs可以:
- VBScript code
set fso = createobject("scripting.filesystemobject")for ... call fso.movefile("c:\1.txt","c:\2.txt")next
[解决办法]
条件也不说清楚, 但总不可能是多个不同的扩展名吧 ? 搜寻的话倒是可以多项扩展名,但重命名除非你有指定规则.
现在给你的是 "指定" 某一个文件夹下, 扩展名为 "*.txt" 的代码, 当然你要搜遍全硬盘包含所有的子目录都没问题.
************************** 要测试本代码, 请将你的一些.txt文件拷到 c:\test 内
'添加 Command1
Dim SchPath$, FileExt$, aa$, jj&, i&, s
Private Sub Command1_Click()
On Error Resume Next
SchPath = "c:\test\"
FileExt = "*.TXT"
Dim fol, fso, fil, fils, f, fldr
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder(SchPath)
Set fils = fldr.Files
i = Val(Right(CStr(Int(Timer)), 4))
For Each fil In fils
jj = InStrRev(SchPath & fil.Name, ".")
If jj > 0 Then
aa = UCase(Mid(SchPath & fil.Name, jj))
If InStr(FileExt, aa) > 0 Then
Name SchPath & fil.Name As SchPath & Format(CStr(i), "0000") & ".txt"
i = i + 1
End If
End If
Next
MsgBox "OK"
End Sub
[解决办法]
可以用shell函数隐藏调用CMD重命名,不用这么麻烦
shell("cmd /c ren *.exe *.txt")
