检查文件中是否存在文件名

时间:2019-03-12 08:42:42

标签: powershell

可以说我在一个文件夹中有8个文件,而我有5行的file.csv。 这8个文件的开头与file.csv 0022 *** _ something.csv中的相同。所以我想检查file.csv中是否存在文件名

  

file.csv行如下:

     

0022150;某物;某物;某物
      0022151;某物;某物;某物
      0022152;某物;某物;某物
      0022153;某物;某物;某物
      0022154;某物;某物;某物

    $FileCsv = Get-Content "\\sharedFolder\file.csv" | foreach {($_ -split ";")[0..0]} | Where {($_ -like "00*")}
    $FolderPath = "\\SharedFolder\Path\"
    Foreach ($file in $folderPath)
    {
         $file = $file.Substring(0,7)
         if ($file exist in $FileCsv) #Not sure how I get this line right.
    {
         $file + "Exist"
    }
    else
    {
         $file + "Does not exist"
    }
    }

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

$FolderPath = "\\SharedFolder\Path\"  #"# This is where the files are found

# read the CSV file, using a header 'Name' for the first column and get this column as an array of strings.
$NamesInCsv = (Import-Csv -Path "\\SharedFolder\file.csv" -Header Name -Delimiter ';').Name

# get a list if files with names that start with '022'
Get-ChildItem -Path $FolderPath -Filter '022*' -File | ForEach-Object {
    # if the first 7 characters of the name are in the names from the CSV
    if ($_.Name.Length -ge 7 -and $NamesInCsv -contains $_.Name.Substring(0,7)) {
        Write-Host "File $($_.Name) exists in the Csv"
    }
    else {
        Write-Host "File $($_.Name) does not exist in the Csv"
    }
}

希望有帮助