VBScript文件重命名:“文件已存在”错误

时间:2019-01-05 12:34:28

标签: vbscript

文件夹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

以下行中的“文件已存在”错误:

recentFile.Name = Replace(recentFile.Name, " 12345", "")

1 个答案:

答案 0 :(得分:0)

重命名文件时,总是有可能新名称已经存在于输出路径中。为了解决这个问题,您可以像Windows一样在新文件名中添加一个序号:

function createDropdownWithTitles(...optionTitles) {
  const dropdown = document.createElement('select')
  const defaultOption = new Option('Choose', null, true, true)
  defaultOption.disabled = true
  dropdown.add(defaultOption)
  for (const title of optionTitles) {
    const option = new Option(title, title)
    dropdown.add(option)
  }
  return dropdown
}

function createDropdownBasedOn(...otherDropdowns) {
  const dropdown = document.createElement('select')
  dropdown.style.visibility = 'hidden'
  const filledInDropdowns = new Array(otherDropdowns.length)
  for (const [index, dropdown] of otherDropdowns.entries()) {
    dropdown.addEventListener('change', event => fillIn(event.currentTarget, index))
    filledInDropdowns[index] = false
  }
  function fillIn(dependency, index) {
    filledInDropdowns[index] = true
    dropdown[index] = new Option(dependency.value, dependency.value)
    for (const filledIn of filledInDropdowns) {
      if (filledIn === false) return
    }
    dropdown.style.visibility = 'visible'
  }
  return dropdown
}

const dropdownA = createDropdownWithTitles('Player 1', 'Player 2')
const dropdownB = createDropdownWithTitles('Player 3', 'Player 4')

document.body.appendChild(dropdownA)
document.body.appendChild(dropdownB)
document.body.appendChild(createDropdownBasedOn(dropdownA, dropdownB))