我一直试图找到答案但与我的问题没有完全匹配。
下面是脚本片段只是为了重命名文件夹,这不会给我一个错误并继续它只是停止。
在脚本的开头,我的大多数脚本都有“Option Explicit” 所以我想也许停止它并且我使用了“On Error Resume Next”但它仍然停止。
我知道我是如何得到错误的,因为我在目录中打开了一个文件我正在尝试“重命名”我正在尝试做的事情让脚本说'对不起你有一个文件打开了目录'并继续到下一个文件夹......
你能帮我解决一下吗,
objFSO.MoveFolder (folder1),(folder2)
If Err.Number <> 0 Then
WScript.Echo Err.Description
WScript.Echo Err.Number
End If
干杯,
帕乌
答案 0 :(得分:0)
您是否将On Error Resume Next
放在Sub
下方?您还应该清除错误。
我相信您正在使用cscript运行命令提示符中的vbs:
Sub RenameFolders()
On Error Resume Next
' Add your codes
objFSO.MoveFolder (folder1),(folder2)
If Err.Number <> 0 Then
WScript.Echo "sorry you have a file open in that directory"
WScript.Echo Err.Description
WScript.Echo Err.Number
Err.Clear ' Clear the ERROR!
End If
End Sub
答案 1 :(得分:0)
Dim SPath 'As String
Dim DPath 'As String
SPath = "d:\test1"
DPath = "E:\test1"
Call MoveFolders(SPath ,DPath)
Sub MoveFolders(PSPath,PDPath)
'-----------------------------
PSPath = Trim(PSPath)
PDPath = Trim(PDPath)
'-----------------------------
Dim objFso 'AS Object
Dim objFil 'As Object
Dim objMFld 'As Object
Dim objSFld 'As Object
'/*----------------------------
Dim DestFullPath 'As String
Dim DestFullFilePath 'As String
'----------------------------------------------------
Set objFso = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------
If objFso.FolderExists(PSPath) Then
Set objMFld = objFso.GetFolder(PSPath)
'----------------------------------------------------
If Not objFso.FolderExists(PDPath) Then
objFso.CreateFolder(PDPath)
End If
'----------------------------------------------------
For Each objSFld In objMFld.SubFolders
DestFullPath = Replace(objSFld, PSPath, PDPath ,1, 1, vbTextCompare)
'/*------------------------
Call MoveFolders(objSFld,DestFullPath)
'/*------------------------
Next
'/*------------------------
For Each objFil In objFso.GetFolder(PSPath).Files
'/*------------------------
DestFullFilePath = PDPath & "\" & objFil.Name
'/*------------------------
If objFso.FileExists(DestFullFilePath) Then
objFSO.DeleteFile(DestFullFilePath)
End If
'/*------------------------
objFso.MoveFile objFil , PDPath & "\"
Next
'/*------------------------
If objFso.GetFolder(PSPath).Files.Count = 0 And objFso.GetFolder(PSPath).SubFolders.Count = 0 Then
objFso.DeleteFolder PSPath
End If
'------------------------------
End If
End Sub