Проверьте, существует ли имя файла в файле - PullRequest
0 голосов
/ 12 марта 2019

Допустим, у меня есть 8 файлов в папке, и у меня есть файл .csv с 5 строками. 8 файлов начинаются с того же, что и в файле .csv 0022 *** _thing.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 голосов
/ 12 марта 2019

Я бы сделал что-то вроде этого:

$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"
    }
}

Надеюсь, что поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...