0

如何修改以下脚本以避免硬编码位置、日期戳和扩展名,并将这些详细信息指定为命令提示符中的输入变量,例如“cscript del.vbs d:\temp 16/08/2013 jpg” .

    OPTION EXPLICIT
    DIM strExtensionsToDelete,strFolder
    DIM objFSO, MaxAge, IncludeSubFolders

    ' ************************************************************
    ' Setup
    ' ************************************************************

    ' Folder to delete files
    strFolder = "d:\test\"

    ' Delete files from sub-folders?
    includeSubfolders = TRUE
    ' A comma separated list of file extensions
    ' Files with extensions provided in the list below will be deleted
    strExtensionsToDelete = "jpg"
    ' Max File Age (in Days).  Files older than this will be deleted.
    maxAge = 1

    ' ************************************************************

    SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")

    DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

    wscript.echo "Finished"

    SUB DeleteFiles(BYVAL strDirectory,BYVAL strExtensionsToDelete,BYVAL maxAge,includeSubFolders)
        DIM objFolder, objSubFolder, objFile
        DIM strExt

        SET objFolder = objFSO.GetFolder(strDirectory)
        FOR EACH objFile in objFolder.Files
            FOR EACH strExt in SPLIT(UCASE(strExtensionsToDelete),",")
                IF RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt THEN
                    IF objFile.DateLastModified < (NOW - MaxAge) THEN
                        wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified 
                        objFile.Delete
                        EXIT FOR
                    END IF
                END IF
            NEXT
        NEXT    
        IF includeSubFolders = TRUE THEN ' Recursive delete
            FOR EACH objSubFolder in objFolder.SubFolders
                DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubFolders
            NEXT
        END IF
END SUB
4

1 回答 1

0

您的 cscript 命令看起来不错。然后,将此添加到您的 vbscript 以捕获参数

location = WScript.Arguments.Item(0)
date-stamp = WScript.Arguments.Item(1)
extension = WScript.Arguments.Item(2)

请注意,它们都是字符串,因此您需要将日期值解析为实际日期。

于 2013-08-16T13:53:05.090 回答