我创建了一个重命名文件夹中文件的vbs文件。但是,如果要重命名的文件已存在,则会在重命名之前删除该文件。
如果我直接从Windows资源管理器运行它,我的脚本工作正常,但如果我从批处理文件或任务计划程序运行脚本,则deletefile方法不会执行。重命名部分仍然有效。
脚本是:
On Error Resume Next
Set objFS = CreateObject("Scripting.FileSystemObject")
' folder containing files to be renamed
strFolder="\\FullNetworkFilePath\rename"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
' Look at pdf files only
If LCase(objFS.GetExtensionName(strFile)) = "pdf" Then
strFileName = strFile.Name
' Look for files prefixed with ODP
If InStr(strFileName,"ODP") > 0 Then
' substitute ODP with Ope in the file name
strNewFileName = Replace(strFileName,"ODP","Ope")
If objFS.fileexists(strNewFileName) Then
' if the Ope version of the file already exists, delete the ODP version of the file
' MsgBox strNewFileName & " already exists. " & strFilename & " will be deleted."
objFS.deletefile(strFilename)
Else
' if the Ope version of the file does not exist, rename the ODP version of the file
' MsgBox strNewFileName & " does not exist. " & strFilename & " will be renamed."
strFile.Name = strNewFileName
End If
End If
End If
Next
我尝试过的批处理文件脚本是:
cscript "\\FullNetworkFilePath\rename\rename.vbs"
和
c:\windows\syswow64\cscript.exe "\\FullNetworkFilePath\rename\rename.vbs"
任务计划程序运行带有最高权限的vbs脚本。
有人知道为什么当从批处理文件或任务调度程序调用vbs时,deletefile方法不起作用(但名称方法有效)?最终我需要它从Task Scheduler开始工作。