none
How to use variable array in If-Else statement RRS feed

  • Question

  • How can I clean up the below / get it to work?  Goals are listed within comment blocks.  Thank you for the help.

    $Julian = (Get-Date).DayOfYear
    $Path = "C:\Users\Public\Desktop\"
    <# Ultimate Goal (so that I don't have to add/update variable
    Returns files as objects; but haven't figured out how to use
    "Foreach-Object" yet #>
    $uFiles = (get-ChildItem -Path $Path *.$Julian)
    <# Interm Goal - Use variable array to act as the input and
    only run the "if" statement one time (if possible) #>
    $iFiles = "File1", "File2", "File3"
    <# Current process #>
    $cFiles = "File1"
    if ((Get-ChildItem -Path $Path -Filter $cFiles*.$Julian |
    get-content | Measure-Object -line).Lines -gt 1)
    {write-host Use $cFiles} else {write-host Do NOT Use $cFiles}
    $cFiles = "File2"
    if ((Get-ChildItem -Path $Path -Filter $cFiles*.$Julian |
    get-content | Measure-Object -line).Lines -gt 1)
    {write-host Use $cFiles} else {write-host Do NOT Use $cFiles}
    $cFiles = "File3"
    if ((Get-ChildItem -Path $Path -Filter $cFiles*.$Julian |
    get-content | Measure-Object -line).Lines -gt 1)
    {write-host Use $cFiles} else {write-host Do NOT Use $cFiles}

    • Moved by Bill_Stewart Wednesday, September 4, 2019 9:38 PM This is not "fix/debug/rewrite script for me" forum
    Saturday, April 27, 2019 3:41 AM

All replies