locked
powershel: script error purge folders RRS feed

  • Question

  • #this script purges files in a folder that are x no.of  days old, in this case this file was 1 day old but is not working.

    $foldername = $args[0]
    $age = $args[1]

    gci $foldername  | where-object { ($_-is [io.fileinfo] ) -and ($_.lastwritetime.days -lt (get-date).adddays(-$age))}
                                 | remove-item -whatif

    The error is below:

    At C:\cmd.practise\finalexam\purge.ps1:6 char:20
    +                    | remove-item -whatif
    +                    ~
    An empty pipe element is not allowed.
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : EmptyPipeElement

    I am calling the script by .\finalexam\purge.ps1

    The script I created is in the finalexam folder I created.

    Thank-you

    SQL 75

    (Learning powershell)

    Monday, April 6, 2015 6:45 PM

Answers

  • The following is working for me:

    $foldername = "c:\temp\test"
    $age = 1
    
    gci $foldername | where-object { ($_-is [io.fileinfo] ) -and ($_.lastwritetime.days -lt (get-date).adddays(-$age))} | remove-item -whatif

    You say you're "calling the script by .\finalexam\purge.ps1" but don't indicate that you're entering the arguments that you assign to $foldername and $age. You make sure those arguments get passed. Otherwise you may have another problem with your syntax that isn't clear because of the copying and pasting going on to share this code.

    • Marked as answer by SQL75 Monday, April 6, 2015 7:27 PM
    Monday, April 6, 2015 7:04 PM
  • When you run the script without arguments, the $args array is empty.

    Why are you using $args array when you can use named parameters?

    Start by reading this:


    PS C:\> help about_Parameters

    After reading and understanding this help topic, you should also read the about_Parameters_Default_Values help topic as well.

    You don't need to use $args. Use named parameters, which can have default values.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by SQL75 Monday, April 6, 2015 7:27 PM
    Monday, April 6, 2015 7:11 PM

All replies

  • The following is working for me:

    $foldername = "c:\temp\test"
    $age = 1
    
    gci $foldername | where-object { ($_-is [io.fileinfo] ) -and ($_.lastwritetime.days -lt (get-date).adddays(-$age))} | remove-item -whatif

    You say you're "calling the script by .\finalexam\purge.ps1" but don't indicate that you're entering the arguments that you assign to $foldername and $age. You make sure those arguments get passed. Otherwise you may have another problem with your syntax that isn't clear because of the copying and pasting going on to share this code.

    • Marked as answer by SQL75 Monday, April 6, 2015 7:27 PM
    Monday, April 6, 2015 7:04 PM
  • When you run the script without arguments, the $args array is empty.

    Why are you using $args array when you can use named parameters?

    Start by reading this:


    PS C:\> help about_Parameters

    After reading and understanding this help topic, you should also read the about_Parameters_Default_Values help topic as well.

    You don't need to use $args. Use named parameters, which can have default values.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by SQL75 Monday, April 6, 2015 7:27 PM
    Monday, April 6, 2015 7:11 PM
  • Thanks Bill!

    Regards

    SQL75

    Monday, April 6, 2015 7:28 PM
  • Start simple:

    $oldest=(get-date).AddDays(-1)
    $foldername='c:\test'
    Get-ChildItem  $foldername  -file |
        where-object { $_.lastwritetime -lt $oldest } | 
        remove-item -whatif

    Now make that work and you are almost there.


    \_(ツ)_/

    Monday, April 6, 2015 7:29 PM