Need a powershell script and I'm stumped

  • I need a script that will reiterate through a folder looking at PDF files, if the corresponding CSXML file (same name different extension) is not found, then delete the PDF file.

    Thanks.

    Ed Watson aka SQLGator
    Microsoft SQL Server MVP

    Follow me on Twitter!
    Go Gators!

  • I imagine it is possible to do this using the pipeline with far less code but this will get the job done:

    $dir = "C:\@\1"

    foreach($file in (ls "$dir\*.pdf"))

    {

    $csxml = (Join-Path $dir ($file.BaseName + ".csxml"))

    if(Test-Path $csxml)

    {

    Remove-Item $file

    }

    }

    Edit: Good catch Gary! I updated the code above to delete the PDF file, not the CSXML file.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (12/21/2011)


    I imaging it is possible to do this using the pipeline with far less code but this will get the job done:

    $dir = "C:\@\1"

    foreach($file in (ls "$dir\*.pdf"))

    {

    $csxml = (Join-Path $dir ($file.BaseName + ".csxml"))

    if(Test-Path $csxml)

    {

    Remove-Item $csxml

    }

    }

    I think that this removes the wrong file. The above script, unless I am mistaken, will remove the CSXML file as opposed to the PDF file.

    Apart from that it should be a great starter for you!!! (This is me ensuring credit goes to opc.three)

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • That is awesome, thanks. I will test it later this morning. I really need to read the two powershell books on my desk 😛

    Ed Watson aka SQLGator
    Microsoft SQL Server MVP

    Follow me on Twitter!
    Go Gators!

  • I think the script is backward, it is deleting the PDF when the CSXML is there. I need it to do the opposite, if there is no CSXML then delete the PDF. They have to exist as a pair before I can process them in another load process. Thanks for your help.

    Ed Watson aka SQLGator
    Microsoft SQL Server MVP

    Follow me on Twitter!
    Go Gators!

  • I got it with the ! operator, books are handy LOL. Thanks for the script!

    Ed Watson aka SQLGator
    Microsoft SQL Server MVP

    Follow me on Twitter!
    Go Gators!

  • Ed.Watson (12/21/2011)


    I think the script is backward, it is deleting the PDF when the CSXML is there. I need it to do the opposite, if there is no CSXML then delete the PDF. They have to exist as a pair before I can process them in another load process. Thanks for your help.

    That's how I coded it initially, but Gary rightfully corrected me based on your initial post which I [apparently properly] misread 😛 I am happy you found a way forward!

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • You are the man!

    Ed Watson aka SQLGator
    Microsoft SQL Server MVP

    Follow me on Twitter!
    Go Gators!

  • I guess I should have been more thorough in my code review 😉

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply