VBS批量更名问题
发布网友
发布时间:2022-04-25 21:15
我来回答
共3个回答
热心网友
时间:2022-06-17 14:32
Option Explicit
On Error Resume Next
Dim myfso,myfiles,myfile,myfolders,myfolder,f
Dim k,l
Dim strNewName
Const strCurrentPath = "."
k = 0
Set myfso = Wscript.CreateObject("Scripting.FileSystemObject")
Set myfiles = myfso.GetFolder(strCurrentPath).Files '遍历本目录文件
For Each f In myfiles
If LCase(right(f.name,3))="txt" Then
Set myfile = myfso.OpenTextFile(f,1,false)
For l=1 To 5 '循环5次,跳过前5行
myfile.ReadLine
Next
strNewName = Trim(myfile.ReadLine) '读一行,这里指读第6行
myfile.Close
strNewName = Replace(strNewName, "\", "")
strNewName = Replace(strNewName, "/", "")
strNewName = Replace(strNewName, ":", "")
strNewName = Replace(strNewName, "*", "")
strNewName = Replace(strNewName, "?", "")
strNewName = Replace(strNewName, """", "")
strNewName = Replace(strNewName, ">", "")
strNewName = Replace(strNewName, "<", "")
strNewName = Replace(strNewName, "|", "")
strNewName = left(strNewName,30)
f.name = strNewName & ".txt"
Set f = Nothing
k = k + 1
End if
Next
Set myfiles = Nothing
Set myfolders = myfso.GetFolder(strCurrentPath).SubFolders '遍历本目录下的子目录
For Each myfolder In myfolders '遍历所有子目录的所有文件
Set myfiles = myfso.GetFolder(myfolder.Path).Files
For Each f In myfiles
If LCase(right(f.name,3))="txt" Then
Set myfile = fso.OpenTextFile(f,1,false)
For l=1 To 5 '循环5次,跳过前5行
myfile.ReadLine
Next
strNewName = Trim(myfile.ReadLine) '读一行,这里指读第6行
myfile.Close
strNewName = Replace(strNewName, "\", "")
strNewName = Replace(strNewName, "/", "")
strNewName = Replace(strNewName, ":", "")
strNewName = Replace(strNewName, "*", "")
strNewName = Replace(strNewName, "?", "")
strNewName = Replace(strNewName, """", "")
strNewName = Replace(strNewName, ">", "")
strNewName = Replace(strNewName, "<", "")
strNewName = Replace(strNewName, "|", "")
strNewName = left(strNewName,30)
f.Name = strNewName & ".txt"
Set f = Nothing
k = k + 1
End if
Next
Set myfiles = Nothing
Set myfolder = Nothing
Next
Set myfolders = Nothing
Set myfso = Nothing
msgbox "总共修改了" & k & "个txt文件" & vbcrlf & vbcrlf & "(不记重名但记重复修改)"
再次修改。。。。。。。
For l=1 To 5
myfile.ReadLine
Next
以上FOR循环表示跳过前5行。
例如要读第6行,就要跳过前5行,代码为
For l=1 To 5
myfile.ReadLine
Next
要读第n行,就要跳过前n-1行,代码为
For l=1 To n-1
myfile.ReadLine
Next
如果是第1行,以上代码去掉即可,还有不明白吗?
热心网友
时间:2022-06-17 14:32
那个1不是表示第几行,所以不行
############################################
你的前七行或者前六行都是空行吗?就是没任何内容?空格也不行,这样的话就好办一点。
而且上面的代码只能遍历当前目录,子目录是*为力的。
热心网友
时间:2022-06-17 14:33
111111