开发者

What's wrong with the recursion in this VBScript?

开发者 https://www.devze.com 2023-03-09 00:55 出处:网络
Trying to delete .exe files of a set size, recursively - but VBscript is not my forte, Can anyone see an obvious reason for it not working recursively?

Trying to delete .exe files of a set size, recursively - but VBscript is not my forte, Can anyone see an obvious reason for it not working recursively?

OPTION EXPLICIT
DIM strFolder
DIM objFSO

strFolder = "C:\TESTFOLDER"

set objFSO = createobject("Scripting.FileSystemObject")

RecursiveDelete strFolder

wscript.echo "Finished"

sub RecursiveDelete(byval strDirectory)
    DIM objFolder, objSubFolder, objFile

    set objFolder = o开发者_JAVA技巧bjFSO.GetFolder(strDirectory)
    for each objFile in objFolder.Files
            if ( RIGHT(UCASE(objFile.Path),4) = ".EXE" ) AND (file.Size == 47232 ) then
                wscript.echo "Deleting:" & objFile.Path
                objFile.Delete
            end if
    next

    for each objSubFolder in objFolder.SubFolders
        RecursiveDelete objSubFolder.Path
    next
end sub


This:

if ( RIGHT(UCASE(objFile.Path),4) = ".EXE" ) AND (file.Size == 47232 ) then

should be:

if ( RIGHT(UCASE(objFile.Path),4) = ".EXE" ) AND (objFile.Size = 47232 ) then


What is it doing or not doing that tells you it's not working?

One tip: comment out the if statement for the moment and just have it print each folder and file visited, to ensure that the recursion is happening. Then reenable the if statement but comment out the delete statement, and have it print the matching filenames.

In other words, verify that it's doing what you think it's doing.

Something else I see, as Ekkehard just mentioned, VBScript doesn't use the double equal sign for testing equality.

After some testing:

Did you retype this here? Because this code doesn't even run as given.

In addition to the double equal sign which causes a VBScript compilation syntax error, you also refer to "file" in that statement, but there is no such variable declared anywhere.

I changed it to objFile, commented out the delete statement, and removed the second equal sign, and this code does run and recurse as you would expect.

I also created some test files in a folder and reenabled the delete statement, and the test files were deleted.

So since we don't know what actual trouble you're having with it, that's about as much as I can suggest right now.

0

精彩评论

暂无评论...
验证码 换一张
取 消