我每周收到一堆PDF文件。
其中一些文件的扩展名错误,或者根本没有。
有时,它就像file1.pdd,file.ptf等
下面的脚本应该遍历所有文件,寻找那些没有.pdf扩展名的文件。
如果找到,请使用正确的扩展名进行修复。
例如,代码应该已将filed.pdd更改为file1.pdf。
修复扩展程序后,将文件移动到其他位置。
它没有修复扩展,也没有移动文件。
非常感谢任何帮助。
Set FSO = CreateObject("Scripting.FileSystemObject")
Set pdfFolder = FSO.GetFolder( "C:\Folder1\Subfolder\")
For Each fil In pdfFolder.Files
' check each file to be sure it fits the pattern
fname = fil.Name
suffix = LCase( Right( fname, 4 ) )
prefix = Left( fname, 8 )
' so suffix has to be right, prefix has to have two slashes, and prefix has to be a date:
If UBound( Split(prefix,"/") ) = 2 AND IsDate(prefix) AND suffix = ".pdf" Then
newName = Mid( fname, 9 )
' Response.Write "Renaming '" & fname & "' to '" & newName & "'<br/>" & vbNewLine
fil.Move "E:\Folder2\Fixed\" & newName
End If
Next
答案 0 :(得分:1)
此代码可以解决您的问题:
Set Fso = CreateObject("Scripting.FileSystemObject")
Set PdfFolder = Fso.GetFolder("C:\Folder1\Subfolder\")
For Each x In PdfFolder.Files
Fname = x.Name
If LCase(Right(Fname, 4)) <> ".pdf" Then
Fso.MoveFile Fname, "E:\Folder2\Fixed\" & Fso.GetBaseName(Fname) & ".pdf"
End If
Next
我希望我有所帮助。
答案 1 :(得分:1)
您应该使用FileSystemObject的专用方法,而不是将文件规范视为另一个字符串:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim aTests : aTests = Array(_
"good.pdf" _
, "pddext.pdd" _
, "ucase.PDf" _
, "noext" _
, "more tests please.pdf" _
)
Dim sTest
For Each sTest In aTests
WScript.Echo " ", qq(sTest)
WScript.Echo "=>", qq(goFS.GetBaseName(sTest) & ".pdf")
Next
Function qq(sText)
qq = """" & sText & """"
End Function
输出:
cscript 02.vbs
"good.pdf"
=> "good.pdf"
"pddext.pdd"
=> "pddext.pdf"
"ucase.PDf"
=> "ucase.pdf"
"noext"
=> "noext.pdf"
"more tests please.pdf"
=> "more tests please.pdf"