1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development An easy one for you VBS nutters

Discussion in 'Software' started by Shirty, 25 Sep 2015.

  1. Shirty

    Shirty W*nker! Super Moderator

    Joined:
    18 Apr 1982
    Posts:
    12,938
    Likes Received:
    2,058
    Really simple one here: I have the following code to strip out small files in a folder:

    Code:
    Dim strPath
    Dim lngSize
    
    Dim fso
    Dim fold
    Dim files
    Dim file
    
    lngSize = 12
    strPath = "C:\FOLDER" 
    
    Set fso = CreateObject("scripting.filesystemobject")
    Set fold = fso.GetFolder(strPath)
    Set files = fold.files
    
    For Each file In files
        If file.Size <= lngSize Then file.Delete True
    Next
    
    My question is: If I want to add in another check so that it only deletes files under 12 bytes that begin with shirty_is_sexy what do I need to do to my script?

    Internet handjob cookie may be available for the first answer. I am not a coder so I don't know jack about this sort of stuff.
     
  2. Flibblebot

    Flibblebot Smile with me

    Joined:
    19 Apr 2005
    Posts:
    4,830
    Likes Received:
    299
    This might work:
    Code:
    For Each file In files
        If StrComp(Left(file.Name, Len("shirty_is_sexy")),"shirty_is_sexy") = 0) Then
            If file.Size <= lngSize Then file.Delete True
    Next
    
     
  3. Shirty

    Shirty W*nker! Super Moderator

    Joined:
    18 Apr 1982
    Posts:
    12,938
    Likes Received:
    2,058
    Looks good, but returns an error:

    Line: 17
    Char:78
    Error: Expected 'Then'
    Code: 800A03F9
    Source: Microsoft VBScript compilation error

    Any thoughts?
     
  4. MadGinga

    MadGinga oooh whats this do?

    Joined:
    19 Mar 2009
    Posts:
    2,706
    Likes Received:
    525
    remove the bracket after the 0 in the second line (of that code snippet)...?
     
  5. Atomic

    Atomic Gerwaff

    Joined:
    6 May 2002
    Posts:
    9,646
    Likes Received:
    94
    I could write you that as a function in powershell if that helps?
     
  6. Atomic

    Atomic Gerwaff

    Joined:
    6 May 2002
    Posts:
    9,646
    Likes Received:
    94
    This should do what you want
    Code:
    $path = "C:\folder"
    $bytes = "12"
    $namematch = "shirty_is_sexy*"
    Get-ChildItem $path | ? {$_.PSIsContainer -eq $false -and $_.length -lt $bytes -and $_.name -like $namematch} | % Remove-Item -Confirm:$false -Force
    You can test it without deleting anything by using WhatIf like so:
     
  7. Flibblebot

    Flibblebot Smile with me

    Joined:
    19 Apr 2005
    Posts:
    4,830
    Likes Received:
    299
    Oops, yes that should work - or add an open bracket in front of StrComp.
     
  8. Shirty

    Shirty W*nker! Super Moderator

    Joined:
    18 Apr 1982
    Posts:
    12,938
    Likes Received:
    2,058
    Thanks for your input chaps, will report back on Monday!
     

Share This Page