2013-08-16 243 views
0

如何修改以下腳本以避免硬編碼位置,日期戳記和擴展名,並在命令提示符中將這些詳細信息指定爲輸入變量,例如「cscript del.vbs d:\ temp 16/08/2013 jpg「。使用vbs刪除沒有硬編碼變量的文件

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 

回答

0

您的cscript命令看起來沒問題。 然後,將它添加到您的VBScript來捕捉參數

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

注意,它們都是字符串,所以你需要將日期值解析到實際日期。

+0

你能夠通過這個修改建議劇本的外觀嗎? –

+1

@McSenstrum「日期戳記」不是變量名稱;訪問沒有索引檢查的.Arguments集合是危險的。 –