这个 VBScript 中的递归有什么问题?

发布于 2024-11-09 18:36:33 字数 762 浏览 0 评论 0原文

尝试递归删除设定大小的 .exe 文件 - 但 VBscript 不是我的强项,有人能看到它不能递归工作的明显原因吗?

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 = objFSO.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

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 = objFSO.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-11-16 18:36:36

这:

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

应该是:

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

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
挽袖吟 2024-11-16 18:36:36

它在做什么或没有做什么告诉你它不起作用?

提示:暂时注释掉 if 语句,然后让它打印访问的每个文件夹和文件,以确保递归正在发生。然后重新启用 if 语句,但注释掉删除语句,并让它打印匹配的文件名。

换句话说,验证它是否正在执行您认为它正在执行的操作。

我看到的其他事情,正如 Ekkehard 刚才提到的,VBScript 不使用双等号来测试相等性。

经过一些测试:

您是否在此处重新输入了此内容?因为这段代码甚至没有按照给定的方式运行。

除了导致 VBScript 编译语法错误的双等号之外,您还在该语句中引用了“file”,但在任何地方都没有声明这样的变量。

我将其更改为 objFile,注释掉了删除语句,并删除了第二个等号,并且此代码确实按照您的预期运行和递归。

我还在文件夹中创建了一些测试文件并重新启用了删除语句,测试文件被删除。

因此,由于我们不知道您遇到的实际问题是什么,所以我现在能建议的就这么多了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文