文件改名,请问如何遍历整个文件夹中的所有文件.例:
t20030101.txt
t20030102.txt
t20030103.txt
......
改为:
t20020101.txt
t20020102.txt
t20020103.txt
......
t20030101.txt
t20030102.txt
t20030103.txt
......
改为:
t20020101.txt
t20020102.txt
t20020103.txt
......
解决方案 »
- 关于控件随窗口大小化的问题
- 1取当前位置鼠标形状,2取其他应用插入符(光标)位置
- VSFlexGrid控件行颜色问题
- 文件长度与计算出长度不等的问题(在线等!)
- 用VB实现SQL SERVER DTS 数据导入导出功能 100分
- 将该程序改为能在XP下运行..100分
- 关于MSFlexGrid的问题!!
- 我怎么看不到所有的贴子内容?已经好多天了!
- 请问如何用VBScript生成一个新的浏览和关闭浏览页,就是那种没有任何按钮的网页,一定加分!
- 我想做个程序在WINDOWS退出前运行一下,如何做?
- 急 !!!如何把一个资源文件中的自定义资源(二进制资源)导入到另一个资源文件中?
- 为什么不管我是否打开文本下面的语句都不执行.加下划线的语句?
Dim lFileCount As Integer '有多少个文件要改
Dim sDesFileFirst As String '记录第一个目标文件名
Dim sFileExt As String '保存文件名的后缀
Dim i As Integer
Dim sSourceFilePre As String '记录源文件名前面的不变化的部分
Dim sDesFilePre As String '记录目标文件名前面不变化的部分
Dim sSourceFileTmp As String '每次改名的时候生成的源文件名
Dim sDesFileTmp As String '每次改名的时候生成的目标文件名
Dim sPath As String
Dim iChangedPartLength As Integer
sPath="C:\"
sSourceFileFirst = "10001"
sDesFileFirst = "20001"
lFileCount = 2000
sFileExt = ".dat"
iChangedPartLength = Len(Trim(Str$(lFileCount)))
sSourceFilePre = Mid$(sSourceFileFirst, 1, Len(sSourceFileFirst) - iChangedPartLength)
sDesFilePre = Mid$(sDesFileFirst, 1, Len(sDesFileFirst) - iChangedPartLength)
For i = lFileCount To 1 Step -1
sSourceFileTmp = sPath & sSourceFilePre & Format(i, String(iChangedPartLength, "0")) & sFileExt
sDesFileTmp = sPath & sDesFilePre & Format(i, String(iChangedPartLength, "0")) & sFileExt
List1.AddItem sSourceFileTmp & " ==> " & sDesFileTmp
'Name sSourceFileTmp As sDesFileTmp
Next i
Dim lFileCount As Integer '有多少个文件要改
Dim sDesFileFirst As String '记录第一个目标文件名
Dim sFileExt As String '保存文件名的后缀
Dim i As Integer
Dim sSourceFilePre As String '记录源文件名前面的不变化的部分
Dim sDesFilePre As String '记录目标文件名前面不变化的部分
Dim sSourceFileTmp As String '每次改名的时候生成的源文件名
Dim sDesFileTmp As String '每次改名的时候生成的目标文件名
Dim sPath As String
Dim iChangedPartLength As Integer
sPath="C:\"
sSourceFileFirst = "10001"
sDesFileFirst = "20001"
lFileCount = 2000
sFileExt = ".txt"
iChangedPartLength = Len(Trim(Str$(lFileCount)))
sSourceFilePre = Mid$(sSourceFileFirst, 1, Len(sSourceFileFirst) - iChangedPartLength)
sDesFilePre = Mid$(sDesFileFirst, 1, Len(sDesFileFirst) - iChangedPartLength)
For i = lFileCount To 1 Step -1
sSourceFileTmp = sPath & sSourceFilePre & Format(i, String(iChangedPartLength, "0")) & sFileExt
sDesFileTmp = sPath & sDesFilePre & Format(i, String(iChangedPartLength, "0")) & sFileExt
List1.AddItem sSourceFileTmp & " ==> " & sDesFileTmp
'Name sSourceFileTmp As sDesFileTmp
Next i
For i = 0 To File1.ListCount - 1
Name File1.List(i) As "test2002" & i & ".txt" '改名
Next
File1.Refresh
End SubPrivate Sub Dir1_Change()
File1.Path = Dir1.Path
ChDir Dir1.Path
End SubPrivate Sub Drive1_Change()
Dir1.Path = Drive1.Drive
ChDrive Drive1.Drive
End Sub
ren t???3????.txt t???2????.txt
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
常量 Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const BIF_RETURNONLYFSDIRS = 1
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type 定义类(用于查找文件) Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
定义类(用于浏览文件夹窗口) Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type 自定义函数 Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function 自定义函数 Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _
DirCount As Integer)
Dim FileName As String 文件名
Dim DirName As String 子目录名
Dim dirNames() As String 目录数组
Dim nDir As Integer 当前路径的目录数
Dim i As Integer 循环计数器变量
Dim hSearch As Long 搜索句柄变量
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then path = path & "\"
搜索子目录 nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
If (DirName <> ".") And (DirName <> "..") Then
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 获取下一个子目录
Loop
Cont = FindClose(hSearch)
End If 遍历目录并累计文件总数 hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) 获取下一个文件
Wend
Cont = FindClose(hSearch)
End If
如果子目录存在则遍历之 If nDir > 0 Then
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", _
SearchStr, FileCount, DirCount)
Next i
End If
End Function 查找按钮代码 Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI 设置浏览窗口 .hWndOwner = Me.hWnd
返回选中的目录 .ulFlags = BIF_RETURNONLYFSDIRS
End With 调出浏览窗口 lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0) 获取路径 SHGetPathFromIDList lpIDList, sPath 释放内存 CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If Screen.MousePointer = vbHourglass
List1.Clear SearchPath = sPath 选中的目录为搜索的起始路径
FindStr = "*.*" 搜索所有类型的文件(此处可另作定义)
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text1.Text = "查找到的文件数:" & NumFiles & vbCrLf & "查找的目录数:" & _
NumDirs + 1 & vbCrLf & "文件大小总共为:" & vbCrLf & _ Format(FileSize, "#,###,###,##0") & "字节"
Screen.MousePointer = vbDefault
End Sub
改名 name ... as ...
ren t???3????.txt t???2????.txt
Shell "command.com /c ren t???3????.txt t???2????.txt"
oldpathname 必要参数。字符串表达式,指定已存在的文件名和位置,可以包含目录或文件夹、以及驱动器。
newpathname 必要参数。字符串表达式,指定新的文件名和位置,可以包含目录或文件夹、以及驱动器。而由 newpathname 所指定的文件名不能存在。
说明newpathname 和 oldpathname 必须是在同一个驱动器上。如果 newpathname 所指定的路径存在,且不同于 oldpathname 所指定的路径,则 Name 语句会将文件移到新的目录或文件夹,并在必要时重新命名文件。如果 newpathname 和 oldpathname 有不同的路径和相同的文件名,那么 Name 只会移动文件到新的位置,并保持文件名不变。使用 Name,可以移动文件从一个目录或文件夹到另一处,但是不能移动目录或文件夹。在一个已打开的文件上使用 Name,将会产生错误。必须在改变名称之前,先关闭打开的文件。Name 参数不能包括多字符 (*) 和单字符 (?) 的统配符。==========================本示例使用 Name 语句来更改文件的名称。示例中假设所有使用到的目录或文件夹都已存在。Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' 定义文件名。
Name OldName As NewName ' 更改文件名。 OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' 更改文件名,并移动文件。可以用FILE控件或者DIR函数取得所有的文件!
如:
t20030101.txt
t20030102.txt
t20030103.txt
......
文件中,把2003取出来.替换为2002
附:文件名长度不一致.
ren *2003*.txt *2002*.txt程序:
Shell "command.com /c ren *2003*.txt *2002*.txt"
用fso几行就可以解决了:'***************************************************************************
Set fso = CreateObject("Scripting.FileSystemObject")
sub scann(folder)
set folder=fso.getfolder(folder)
set files=folder.files
for each file in files '遍历文件夹中的各文件
file.name=replace(file.name,"2003","2002") '用2002替换所有文件名中的2003
next
end sub
scann "c:\1\"
'***************************************************************************把以上代码存成.vbs文件,然后运行既可(我的目标文件夹是c:\1\)。
怎么会出错呢?我这里一点问题都没有啊。
你把错误提示给我看!
提示,.vbs文件存在,是不是目录不对?
Set fso = CreateObject("Scripting.FileSystemObject")
sub scann(folder)
set folder=fso.getfolder(folder)
set files=folder.files
for each file in files '遍历文件夹中的各文件
file.name=replace(file.name,"t2003","t2002") '用t2002替换所有文件名中的t2003
next
end sub
scann "c:\1\"
这是个给用户的算是个另加的功能 ,
用户可能不会用,我可不可以用程序调用它!?
2003 2002是用text1.text text2.text输入进去的.
Shell "command.com /c ren XX.txt YY.txt"
就可以了
就等你结帖了