locked
Check file exists and write to csv RRS feed

  • Question

  • I have a CSV file that with a column in it called filename that contains the complete UNC path to the file

    Something like this
    RowsCount,record_number,image_number,application_number,description,filename,created_date,image_type,last_modified
    1,8,6,8/02/0453,History File,\\HOST-01\Document Store\Planning\8-02-0453\8-02-0453.PDF,00:00.0,HF,NULL
    2,12,12,8/07/0420,History File,\\HOST-01\Document Store\Planning\8-07-0420\8-07-0420.PDF,00:00.0,HF,NULL
    3,172,1,8/07/0027,History File,\\HOST-01\Document Store\Planning\8-07-0027\8-07-0027.PDF,00:00.0,HF,NULL
    4,196,8,8/07/0174,History File,\\HOST-01\Document Store\Planning\8-07-0174\8-07-0174.PDF,00:00.0,HF,NULL
    5,207,10,8/07/0307,History File,\\HOST-01\Document Store\Planning\8-07-0307\8-07-0307.PDF,00:00.0,HF,NULL

    I have been trying to write a power-shell script that will check if the file exists using test-path but it seems to report that none of the files exist yet I can confirm that most of the files do exist.

    Would appreciate if somebody can show me a better way of doing this.

    $FileList = Import-Csv C:\TEMP\DEF.csv | select filename
    
    foreach( $file in $FileList) {
        If (Test-Path -Path "$file") {
            write-host "$file exists"
        } 
        Else { 
            write-warning "$file does not exists"
        }
    }

    An added bonus would be if running the script could simply add a new column to the csv file that could easily be filtered in Excel to show which files do or don't exist.

    Thanks in advance for any help whilst I continue trying to work this out.

    Thursday, September 28, 2017 9:39 AM

Answers

  • Hi,

    opps ... forgot the give the cmdlet its input object ^^:

    $FileList = Import-Csv C:\TEMP\DEF.csv
    
    foreach( $file in $FileList) {
        Add-Member -InputObject $file -MemberType NoteProperty -Name Exists -Value (Test-Path $file.Filename)
    }
    
    $filelist | Export-Csv C:\TEMP\GHI.csv -NoTypeInformation

    Sorry for the inconvenience.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by GTEM Friday, September 29, 2017 7:50 AM
    Thursday, September 28, 2017 11:52 AM

All replies

  • Hi GTEM,

    well, that sure is possible:

    $FileList = Import-Csv C:\TEMP\DEF.csv
    
    foreach( $file in $FileList) {
        Add-Member -MemberType NoteProperty -Name Exists -Value (Test-Path $file.Filename)
    }
    
    $filelist | Export-Csv C:\TEMP\GHI.csv -NoTypeInformation

    Your main issue was not using the Filename property - it got a whole object, not just the path string

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, September 28, 2017 9:48 AM
  • Thank you for your prompt and interesting response.
    I tried it but get the following

    cmdlet Add-Member at command pipeline position 1
    Supply values for the following parameters:
    InputObject:

    I have not used Add-Member before. just trying to work out what I need to do now.

    Thursday, September 28, 2017 10:34 AM
  • Hi,

    opps ... forgot the give the cmdlet its input object ^^:

    $FileList = Import-Csv C:\TEMP\DEF.csv
    
    foreach( $file in $FileList) {
        Add-Member -InputObject $file -MemberType NoteProperty -Name Exists -Value (Test-Path $file.Filename)
    }
    
    $filelist | Export-Csv C:\TEMP\GHI.csv -NoTypeInformation

    Sorry for the inconvenience.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by GTEM Friday, September 29, 2017 7:50 AM
    Thursday, September 28, 2017 11:52 AM