VBScript 文件重命名:“文件已存在”错误
VBScript file rename: “File Already Exists” error
文件夹 D:\FOLDER01 中的文件:
- NAME TEST File01 12345.txt
- NAME TEST File02 12345.txt
- NAME TEST File03 12345.txt
- NAME TEST File04 12345.txt
- NAME TEST File05 12345.txt
如何使这个脚本起作用?
Option Explicit
Dim fso, folder, file, recentFile, folderName
folderName = "D:\FOLDER01\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
Set recentFile = Nothing
For Each file In folder.Files
Set recentFile = file
If (InStr(recentFile, "NAME TEST") > 0) Then
recentFile.Name = Replace(recentFile.Name, "NAME TEST ", "")
recentFile.Name = Replace(recentFile.Name, " 12345", "")
End If
Next
"File Already Exists" 行错误:
recentFile.Name = Replace(recentFile.Name, " 12345", "")
重命名文件时,新名称总是可能已存在于输出路径中。为了克服这个问题,您可以向新文件名添加一个序列号,如 Windows 所做的那样:
Option Explicit
Dim fso, folder, file, folderName, path, baseName, extension, newName, count
folderName = "D:\FOLDER01\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
For Each file In folder.Files
If (InStr(file.Name, "NAME TEST") > 0) Then
path = file.ParentFolder & "\" 'the folder this file is in
baseName = fso.GetBaseName(file.Name) 'the file name without extension
extension = fso.GetExtensionName(file.Name) 'the extension without the dot
If Len(extension) Then extension = "." & extension
'removing the strings from the base name.
baseName = Replace(baseName, "NAME TEST", "")
baseName = Trim(Replace(baseName, "12345", ""))
'the new filename with extension could be this
newName = baseName & extension
'to make certain this new name is unique, do the following:
count = 1
'test if this new name already exists and if so, append a sequence number to it "(x)"
While fso.FileExists(path & newName)
newName = baseName & "(" & count & ")" & extension
count = count + 1
Wend
'here we should be quite certain the new name is not already in use, so update the file
file.Name = newName
End If
Next
Set folder = Nothing
Set fso = Nothing
文件夹 D:\FOLDER01 中的文件:
- NAME TEST File01 12345.txt
- NAME TEST File02 12345.txt
- NAME TEST File03 12345.txt
- NAME TEST File04 12345.txt
- NAME TEST File05 12345.txt
如何使这个脚本起作用?
Option Explicit
Dim fso, folder, file, recentFile, folderName
folderName = "D:\FOLDER01\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
Set recentFile = Nothing
For Each file In folder.Files
Set recentFile = file
If (InStr(recentFile, "NAME TEST") > 0) Then
recentFile.Name = Replace(recentFile.Name, "NAME TEST ", "")
recentFile.Name = Replace(recentFile.Name, " 12345", "")
End If
Next
"File Already Exists" 行错误:
recentFile.Name = Replace(recentFile.Name, " 12345", "")
重命名文件时,新名称总是可能已存在于输出路径中。为了克服这个问题,您可以向新文件名添加一个序列号,如 Windows 所做的那样:
Option Explicit
Dim fso, folder, file, folderName, path, baseName, extension, newName, count
folderName = "D:\FOLDER01\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
For Each file In folder.Files
If (InStr(file.Name, "NAME TEST") > 0) Then
path = file.ParentFolder & "\" 'the folder this file is in
baseName = fso.GetBaseName(file.Name) 'the file name without extension
extension = fso.GetExtensionName(file.Name) 'the extension without the dot
If Len(extension) Then extension = "." & extension
'removing the strings from the base name.
baseName = Replace(baseName, "NAME TEST", "")
baseName = Trim(Replace(baseName, "12345", ""))
'the new filename with extension could be this
newName = baseName & extension
'to make certain this new name is unique, do the following:
count = 1
'test if this new name already exists and if so, append a sequence number to it "(x)"
While fso.FileExists(path & newName)
newName = baseName & "(" & count & ")" & extension
count = count + 1
Wend
'here we should be quite certain the new name is not already in use, so update the file
file.Name = newName
End If
Next
Set folder = Nothing
Set fso = Nothing