Home > Mobile >  PowerShell Check file exists, update list based on response
PowerShell Check file exists, update list based on response

Time:04-14

I'm sorry if this is considered basic but I have just started using PowerShell and I am trying to check on a filesystem whether a particular file name exists or not. All filenames are contained within a CSV.

I'd like to update the CSV with a 'true' or 'false' based on whether the directory and subfolders contains the name. Can someone please pass over any pointers? I am not a programmer so I am finding it difficult.

Editing to add some information:

The file system to search is: S:\hosted_files (and all subfolders)

The CSV containing the filenames is called located in C:\Users\jroberts\Desktop\powershell and callede 'Scanned_Listing.CSV' and the filename is contained within a field called 'Image_Name' (file name contains the extension, eg .tif)

Field to update on the listing would be 'Present'.

If any more information is needed please let me know.

CodePudding user response:

Here's one way of doing that:

$sourceFolder = 'S:\hosted_files'
$data = Import-Csv -Path 'C:\Users\jroberts\Desktop\powershell\Scanned_Listing.CSV'
foreach ($item in $data) {
    # look recursively for the filename from the CSV
    # piping through to Select-Object -First 1 makes the search stop when the first file is found
    $search = Get-ChildItem -Path $sourceFolder -Filter $item.Image_Name -File -Recurse -ErrorAction SilentlyContinue | 
              Select-Object -First 1
    # now add a new column to your CSV
    $item | Add-Member -MemberType NoteProperty -Name Present -Value (@($search).Count -gt 0)
}
# and write out the updated CSV file
$data | Export-Csv -Path 'C:\Users\jroberts\Desktop\powershell\Scanned_Listing_New.CSV' -NoTypeInformation

Another approach could be to first determine from the data in the CSV file what filetypes you're looking for, and use that to get a list of all those files in the folders.
Using that only leaves the loop to see if a filename from the CSV can also be found in the array of existing file names.
Depending on the number of files in the csv and the number of files found in the sourcefolder and all of its subfolders, this may or may not be faster:

$sourceFolder = 'S:\hosted_files'
$data = Import-Csv -Path 'C:\Users\jroberts\Desktop\powershell\Scanned_Listing.CSV'
# create an array of unique file extensions to look for
$extensions = $data.Image_Name | ForEach-Object {('*{0}' -f [System.IO.Path]::GetExtension($_))} | Select-Object -Unique
# now get an array of file names by searching the folders on those extensions
$existingFiles = (Get-ChildItem -Path $sourceFolder -File -Include $extensions -Recurse).Name

# loop through the rows in the CSV
foreach ($item in $data) {
    # add a new column to your CSV
    $item | Add-Member -MemberType NoteProperty -Name Present -Value ($existingFiles -contains $item.Image_Name)
}
# and write out the updated CSV file
$data | Export-Csv -Path 'C:\Users\jroberts\Desktop\powershell\Scanned_Listing_New.CSV' -NoTypeInformation
  • Related