我正在尝试向VBScript添加子例程。简而言之,我试图查看是否存在一种类型的文件,它将删除另一个文件。
会有以下文件:
SOCAL_CU59_res.dxf SOCAL_CU59_main.dxf SOCAL_CU59_mot.dxf SOCAL_CU59_motl.dxf
但是在某些情况下,可能会有一个带有" x"的文件。在文件名的末尾:
SOCAL_CU59_resx.dxf SOCAL_CU59_mainx.dxf SOCAL_CU59_motx.dxf SOCAL_CU59_motlx.dxf
他们都会在同一个文件夹中。 " x"文件优先。因此,如果它存在,我想删除匹配的文件文件而不使用" x"。
这是我到目前为止所遇到的错误。我添加的check filesize例程效果很好,但在此之后我没有运气:
Dim oFSO, sDirectoryPath, oFOLDER, oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "S:\SOCAL\Section_11\Road DXFs\"
RecurseFolders sDirectoryPath
Sub RecurseFolders(sFolder)
'Here we set the oFolder object, note that its variable scope is within
'this sub, so you can set it many times and it's value will only be
'that of the sub that's currently running.
Set oFolder = oFSO.GetFolder(sFolder)
'Here we are looping through every file in the directory path.
For Each oFile In oFolder.Files
'This just checks for a file size less than 100Kb
If oFile.Size <= 1085 And Right(LCase(oFile.Name),3) = "dxf" Then
oFile.Delete True
End If
Next
For Each oFile In oFolder.Files
'This checks if there is a file with an 'x' at the end of filename
If FileExists (Right(oFile.Name),1) = "x" Then
oFile.Delete True
End If
Next
'Here we do the recursive bit. We need to loop through each folder in
'the directory too and call the same sub to ensure we check every folder
'in the path.
For Each oFolder In oFolder.SubFolders
RecurseFolders oFolder.Path
Next
End Sub
该脚本会创建两个文件,但不会删除没有&#34; x&#34;的文件。第204行,Char 5:
出错错误的参数数量或无效的属性分配:&#39;对&#39;
错误引用的行是:If FileExists (Right(oFile.Name),1) = "x" Then
。
答案 0 :(得分:1)
为了正确执行此操作,您需要纠正一些固有问题。首先,您需要进行Ansgar Wiechers提到的括号修正。其次,您应该删除重复循环。没有必要多次遍历所有文件。最后,您应该存储要删除的文件,直到循环结束。删除当前正在循环的文件集中的文件可能会产生意外结果或无法解释的错误。
话虽如此,这就是我如何处理这个问题。您会注意到我提到的所有更正。
Dim oFSO, sDirectoryPath, oFOLDER, oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "S:\SOCAL\Section_11\Road DXFs\"
Dim arrFilesToDelete() 'an empty dynamic array to hold files to be deleted later
Dim i = 0 'an iterator used to track the array pointer
RecurseFolders sDirectoryPath
DeleteExtraFiles arrFilesToDelete
Sub RecurseFolders(sFolder)
'Here we set the oFolder object, note that its variable scope is within
'this sub, so you can set it many times and it's value will only be
'that of the sub that's currently running.
Set oFolder = oFSO.GetFolder(sFolder)
'Here we are looping through every file in the directory path.
For Each oFile In oFolder.Files
'Is the file a "dxf" file
If LCase(Right(oFile.Name)) = "dxf" Then
'This just checks for a file size less than 100Kb
If oFile.Size <= 1085 And Right(LCase(oFile.Name),3) = "dxf" Then
End If
'This checks if there is an 'x' at the end of filename
If LCase(Right(oFile.Name) 5) = "x.dxf" Then
'if so, store its counterpart for deletion later
sBadFile = Replace(oFile.Name, "x.dxf", ".dxf")
ReDim Preserve arrFilesToDelete(i)
arrFilesToDelete(i) = oFile.Path & "\" & sBadFile
i = i + 1
End If
End If
Next
'Here we do the recursive bit. We need to loop through each folder in
'the directory too and call the same sub to ensure we check every folder
'in the path.
For Each oFolder In oFolder.SubFolders
RecurseFolders oFolder.Path
Next
End Sub
Sub DeleteExtraFiles(arrFiles)
For Each sFile in arrFiles
If oFSO.FileExists(sFile) Then
oFSO.DeleteFile sFile
End If
Next
End Sub
答案 1 :(得分:0)
您将内部右括号放在错误的位置。参数1
属于函数Right
。改变这个:
If FileExists (Right(oFile.Name),1) = "x" Then
进入这个:
If FileExists (Right(oFile.Name,1)) = "x" Then
话虽如此,该行可能还有其他问题。 VBScript没有内置函数FileExists
,并且您的代码片段不会显示该函数是否在代码中的其他位置实现,因此是否将字符传递给它并将其返回值与字符x
实际上很有意义很难说。
如果您打算使用FileSystemObject
方法FileExists
,则需要从实际FileSystemObject
实例中调用它:
If oFSO.FileExists(...) Then
并传递文件名或路径,而不是单个字符或布尔值。
如果您要测试是否存在任何给定文件foo.ext
另一个文件foox.ext
,并且在这种情况下删除foo.ext
您执行以下操作:
For Each oFile In oFolder.Files
xFilename = oFSO.GetBaseName(oFile) & "x." & oFSO.GetExtensionName(oFile)
If oFSO.FileExists(oFSO.BuildPath(oFile.Parent, xFilename)) Then
oFile.Delete True
End If
Next