0

I want to check if some file exists with multiple extension. I tried this way, it works. But I need the simple way to do this process. Is anyone can give me advice how to improve this process? Thank you so much.

$Data = "D:\Folder"

if (!(Test-Path $Data\*.que))
{
     if (!(Test-Path $Data\*.pro))
     {
          if (!(Test-Path $Data\*.err))
          {
               if (!(Test-Path $Data\*.skip))
               {
                    Write-Host "Complete"
               }
               else {

                    Write-Host "Finished, But Not Complete"
               }
          }
          else {

               Write-Host "Finished, But Not Complete"
          }
     }
     else {

          Write-Host ".pro Exist, Not Finished"
     }
}
else {

     Write-Host ".que Exist, Not Finished"
}

2 Answers 2

1

Since you have some custom messages depending on filetype, you could approach this in a few ways. One way would be to put all into a CSV and iterate over that. To simplify the code it seems logical to check if the file exists rather than not. It could look something like this.

$CSVData = @'
Extension,NotPresent,Present
que,".que Exist, Not Finished",
pro,".pro Exist, Not Finished",
err,"Finished, But Not Complete",
skip,"Complete","Finished, But Not Complete"
'@ | ConvertFrom-Csv

$Data = "D:\Folder"

$CSVData | foreach {
    if(Test-Path "$Data\*.$($_.extension)")
    {
        Write-Host $_.present
    }
    else
    {
        write-host $_.notpresent
    }
}

I left the Present column empty except the one which you actually responded to. If you don't need to fill those in, you may want to check if there is anything in $_.present before Write-Host

$CSVData = @'
Extension,NotPresent,Present
que,".que Exist, Not Finished",
pro,".pro Exist, Not Finished",
err,"Finished, But Not Complete",
skip,"Complete","Finished, But Not Complete"
'@ | ConvertFrom-Csv

$Data = "D:\Folder"

$CSVData | foreach {
    if(Test-Path "$Data\*.$($_.extension)")
    {
        if($null -ne $_.present)
        {
            Write-Host $_.present
        }
    }
    else
    {
        write-host $_.notpresent
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
(Get-ChildItem -Path 'D:\Folder' -File -Recurse).Extension | Group-Object

You can Get-ChildItem to get files in a directory and only grab the extension property of the returned object, then Group-Object to get a list of directories and how many of each exist

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.