比较文件夹文件名和数组

时间:2020-03-02 18:31:25

标签: vbscript

这是我的第一篇文章,我是脚本的新手。直到我终于弄清头绪,我才决定在这里注册并寻求帮助,因为我经常在寻找答案时使用这个网站。

我正在尝试编写一个vbscript,将给定文件夹中的所有文件名与包含该文件夹“批准的”文件名的数组进行比较。如果文件夹中的文件名与数组中的任何名称都不匹配,则应删除文件夹中的文件。我的脚本似乎比较好,但是当它找到不匹配的文件时,似乎要删除它检查的最后一个文件名,而不是不匹配的文件名。这就是我所拥有的。您的输入将不胜感激。谢谢。

有12个“可接受的”文件名。 4个文件名,每个文件名具有3个不同的扩展名。


    DIM objFSO, objUser, userName, strFolder, objFolder, objFiles, sigTxt1, sigTxt2, sigTxt3, 
    sigCust, sigExt1, sigExt2, sigExt3

    SET objFSO = CreateObject("Scripting.FileSystemObject")
    SET objUser = CreateObject("WScript.Network")
    userName = objUser.UserName
    strFolder = "C:\Users\" & userName & "\AppData\Roaming\Microsoft\Signatures\"
    SET objFolder = objFSO.GetFolder(strFolder)
    SET objFiles = objFolder.files
    sigTxt1 = "FSignature"
    sigTxt2 = "PSignature"
    sigTxt3 = "PWSignature"
    sigExt1 = ".htm"
    sigExt2 = ".rtf"
    sigExt3 = ".txt"
    sigCust = "CustomSignature"
    DIM SigName
    SigName=Array(sigTxt1 & sigExt1, sigTxt1 & sigExt2,  sigTxt1 & sigExt3, sigTxt2 & sigExt1, sigTxt2 &
    sigExt2, sigTxt2 & sigExt3, sigTxt3 & sigExt1, sigTxt3 & sigExt2, sigTxt3 & sigExt3, 
    sigCust & sigExt1, sigCust & sigExt2, sigCust & sigExt3)

    DIM i, sigFoldFile, sigArrName, sName1, sName2, noMatch
    i = 1
    noMatch = "True"
    DO WHILE i  0 THEN
            noMatch = "True"
            wscript.echo "Match = NO" & vbNewline & sname1 & "  " & sName2 & vbNewline & "noMatch = " & 
    noMatch
        ELSE
            noMatch = "False"
            wscript.echo "Match = YES" & vbNewline & sname1 & " = " & sName2 & vbNewline & "noMatch = " & 
    noMatch
            EXIT FOR
        NEXT
    If noMatch = "True" THEN
        wscript.echo "Deleteing file " & sName2
    '   objFSO.DeleteFile sName2
    END IF
    i = i + 1
    wscript.echo i               'temporary line for checking that value of i
    NEXT
    LOOP

更新 我现在有这个工作。谢谢大家。我将发布该脚本,以防它可以帮助其他人。我意识到我的objFSO.DeleteFile行需要包含strFolder,以便指定完整路径。我还添加了一个文件计数变量,因为“ DO WHILE i <13”语句中的“ <”数字不能为静态数字。这是因为用户文件夹中文件的数量可能会更多或更少。


    Option Explicit
    DIM objFSO, objUser, userName, strFolder, objFolder, objFiles, sigTxt1, 
    sigTxt2, sigCust, sigExt1, sigExt2, sigExt3
    SET objFSO = CreateObject("Scripting.FileSystemObject")
    SET objUser = CreateObject("WScript.Network")
    userName = objUser.UserName
    strFolder = "C:\Users\" & userName & 
    "\AppData\Roaming\Microsoft\Signatures\"
    SET objFolder = objFSO.GetFolder(strFolder)
    SET objFiles = objFolder.files
    sigTxt1 = "FSignature"
    sigTxt2 = "PSignature"
    sigCust = "CustomSignature"
    sigExt1 = ".htm"
    sigExt2 = ".rtf"
    sigExt3 = ".txt"
    DIM SigName
    SigName=Array(sigTxt1 & sigExt1, sigTxt1 & sigExt2,  sigTxt1 & sigExt3, 
    sigTxt2 & sigExt1, sigTxt2 & sigExt2, sigTxt2 & sigExt3, sigCust & 
    sigExt1, sigCust & sigExt2, sigCust & sigExt3)
    '------------------------------------------LINE 19
    DIM f, i, j, sigFoldFile, sigArrName, sName1, sName2, noMatch
    f = objFolder.Files.count
    i = 1
    j = f + 1
    DO WHILE i < j
    FOR EACH sigFoldFile in objFiles
        noMatch = "True"
        sName2 = sigFoldFile.Name
        FOR EACH sigArrName in SigName
            sName1 = sigArrName
        IF sName2 <> sName1 THEN
            noMatch = "True"
        ELSE
            noMatch = "False"
            EXIT FOR
        END IF
        NEXT
    IF noMatch = "True" THEN
        wscript.echo "Deleteing file " & sName2
        sigFoldFile.Attributes = 0
        objFSO.DeleteFile strFolder & sName2
    END IF
    i = i + 1
    NEXT
    LOOP

0 个答案:

没有答案