powershell检查是否存在多个输入

powershell检查是否存在多个输入,powershell,input,file-exists,Powershell,Input,File Exists,我需要检查输入中的文件是否存在 我拆分多个输入,例如BWMDL.VML BWMDL.STA等,并写出文件夹中已经存在的文件 我检查输入文件是否存在于文件夹中 但我得到的是真的,即使文件不存在,测试路径的输出也会打印两次,结果不同 Set-Variable -Name Files -Value (Read-Host "instert file name") Set-Variable -Name FromPath -Value ("C:\Users\Desktop\A

我需要检查输入中的文件是否存在

  • 我拆分多个输入,例如BWMDL.VML BWMDL.STA等,并写出文件夹中已经存在的文件
  • 我检查输入文件是否存在于文件夹中
  • 但我得到的是真的,即使文件不存在,测试路径的输出也会打印两次,结果不同

    Set-Variable -Name Files -Value (Read-Host "instert file name") 
    Set-Variable -Name FromPath -Value ("C:\Users\Desktop\AP\AP\parser\*.VML" , "C:\Users\Desktop\AP\AP\parser\*.STA")
    Set-Variable -Name NameOfFiles (Get-ChildItem -Path $FromPath "-Include *.VML, *.STA" -Name)
    
    Write-Host "FILES IN FOLDER:"
    $NameOfFiles
    
    Write-host "---------------------"
    Write-host "FILES FROM INPUT: "
    Splitted
    Write-host "---------------------"
    
    Write-host "FILE EXISTS: "
    ForEach ($i in Splitted) {
        FileToCheck
    }
    
    function Splitted {
        $Files -Split " "
    }
    
    function FileToCheck {
        Test-Path $FromPath -Filter $Files -PathType Leaf
    }
    

    例如,我变得像这样

    你把事情复杂化了。
    一旦获得数组中扩展名为.VML或.STA的所有文件的名称,就不必再使用
    测试路径
    ,因为您知道数组
    $NameOfFiles
    中的文件确实存在,否则
    get ChildItem
    就不会列出它们

    这意味着您可以去掉已定义的助手函数,顺便说一句,这些函数应该写在代码的顶部,所以在调用它们之前先编写

    试一试

    输出应该是

    instert file name(s) separated by space characters: BWMDL.VML BWMDL.STA
    FILES IN FOLDER:
    BWMDL.STA
    BWMDL.VML
    ---------------------
    FILES FROM INPUT: 
    BWMDL.VML
    BWMDL.STA
    ---------------------
    FILE EXISTS: 
    True
    True
    
    instert file name(s) separated by space characters: BWMDL.VML BWMDL.STA
    FILES IN FOLDER:
    BWMDL.STA
    BWMDL.VML
    ---------------------
    FILES FROM INPUT: 
    BWMDL.VML
    BWMDL.STA
    ---------------------
    FILE EXISTS: 
    True
    True