locked
Powershell script is not bring in colums data from a CSV RRS feed

  • Question

  • Hi,
    The JobNo is coming in
    What am I missing in the PS code
    Thanks for your help

    Powerscript code:

    $totalSize = 0
    Import-Csv c:\fileListing.csv | ForEach {
        $fileDetails =
        Get-Item $_.FilePath
                $props = @{
             File = $fileDetails.FullName
            'Size(B)' = $fileDetails.Length
            'JobNo' = $fileDetails.JobNo
             }
            
        $totalSize = $totalSize + $fileDetails.Length
        New-Object PsObject -Property $props
      
    # export to csv
    } | Select File,'Size(B)','JobNo'| Sort File | Export-Csv C:\fileReport.csv -NoTypeInformation

    (Get-Content C:\fileReport.csv) + "`nTotal Size,$totalSize" | Set-Content c:\fileReport.csv


    File Listing file:
    FilePath                                       ,name     ,JobNo
    \\shared\SHAREDSPACE\For\1.jpg ,HELOO1 ,000001
    \\shared\SHAREDSPACE\For\2.jpg ,HELOO2 ,000002

    CSV result:
    File                                               Size(B)      JobNo
    \\shared\SHAREDSPACE\For\1.jpg  467812 
    \\shared\SHAREDSPACE\For\2.jpg  123432

    I need the JobNo as well
    Thanks


    Nick M

    Thursday, April 25, 2019 2:06 PM

Answers

  • This works.

    Import-Csv c:\fileListing.csv |
        ForEach-Object{
            $file = [io.fileinfo]$_.FilePath
            [pscustomobject]@{
                Fullname = $file.Fullname
                'Size(B)' = $file.Length
                JobNo = $_.JobNo
            }
        }
    


    \_(ツ)_/

    • Marked as answer by NickMangioros Thursday, April 25, 2019 3:19 PM
    Thursday, April 25, 2019 3:12 PM

All replies

  • What does this show?

    Import-Csv c:\fileListing.csv 


    \_(ツ)_/

    Thursday, April 25, 2019 2:30 PM
  • The result is in the bottom of my original post

    C:\fileReport.csv


    File                                               Size(B)      JobNo
    \\shared\SHAREDSPACE\For\1.jpg  467812 
    \\shared\SHAREDSPACE\For\2.jpg  123432

    Thanks for your reply


    Nick M


    Thursday, April 25, 2019 2:46 PM
  • Is "JobNo" part of the JPG file's metadata? Or did you make a mistake and should be using the "JobNo" from the CSV file? E.g., 'JobNo' = $_.JobNo

    If it's part of the file's metadata then this should help:

    https://gallery.technet.microsoft.com/scriptcenter/get-file-meta-data-function-f9e8d804


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, April 25, 2019 2:48 PM
  • No it is not part of the metadata of the JPEG
    It is in the original csv file

    This is what I have in the code:
    'JobNo' = $fileDetails.JobNo

    Thanks


    Nick M


    Thursday, April 25, 2019 2:50 PM
  • That is the original CSV?  I thought that was the one you were creating.  You have to  be clear and accurate.

    JobNo = $_.JobNo


    \_(ツ)_/

    • Proposed as answer by jrv Thursday, April 25, 2019 3:01 PM
    Thursday, April 25, 2019 2:58 PM
  • I am showing/creating the Bytes file and total and not displaying the name

    My Current Input file
    File Listing file:
    FilePath                                       ,name     ,JobNo
    \\shared\SHAREDSPACE\For\1.jpg ,HELOO1 ,000001
    \\shared\SHAREDSPACE\For\2.jpg ,HELOO2 ,000002

    My Current Output File:
    CSV result:
    File                                               Size(B)      JobNo
    \\shared\SHAREDSPACE\For\1.jpg  467812 
    \\shared\SHAREDSPACE\For\2.jpg  123432

    My Output File should be:

    CSV result:
    File                                               Size(B)      JobNo
    \\shared\SHAREDSPACE\For\1.jpg  467812      000001
    \\shared\SHAREDSPACE\For\2.jpg  123432      000002

    Thanks


    Nick M


    Thursday, April 25, 2019 3:00 PM
  • the code is confusing, I would rewrite it.  If you start with this it works fine

    $totalSize = 0
    $rows = Import-Csv c:\fileReport.csv 
    
    foreach($row in $rows)
    {
        $row.'FilePath '
        $row.JobNo
        $row.'name '
    }

    The contents of the file are correctly output

    Thursday, April 25, 2019 3:02 PM
  • This would be easier:

    Import-Csv c:\fileListing.csv |
        Select *,@{n='Length';e={([io.fileinfo]$_.FilePath).Length}}


    \_(ツ)_/

    Thursday, April 25, 2019 3:06 PM
  • I am not an expert so I am totally confusedPossible show where in my code I would do thisThanks for your help

    CODE:

    $totalSize = 0
    Import-Csv c:\fileListing.csv | ForEach {
        $fileDetails =
        Get-Item $_.FilePath
                $props = @{
             File = $fileDetails.FullName
            'Size(B)' = $fileDetails.Length
            'JobNo' = $fileDetails.JobNo
             }
            
        $totalSize = $totalSize + $fileDetails.Length
        New-Object PsObject -Property $props
      
    # export to csv
    } | Select File,'Size(B)','JobNo'| Sort File | Export-Csv C:\fileReport.csv -NoTypeInformation

    (Get-Content C:\fileReport.csv) + "`nTotal Size,$totalSize" | Set-Content c:\fileReport.csv




    Nick M


    Thursday, April 25, 2019 3:08 PM
  • This works.

    Import-Csv c:\fileListing.csv |
        ForEach-Object{
            $file = [io.fileinfo]$_.FilePath
            [pscustomobject]@{
                Fullname = $file.Fullname
                'Size(B)' = $file.Length
                JobNo = $_.JobNo
            }
        }
    


    \_(ツ)_/

    • Marked as answer by NickMangioros Thursday, April 25, 2019 3:19 PM
    Thursday, April 25, 2019 3:12 PM
  • Thanks that worked

    Have a nice day


    Nick M

    Thursday, April 25, 2019 3:20 PM
  • Yes, I saw what you have in your code. That's why I suggested the correction: 'JobNo' = $_.JobNo

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, April 25, 2019 3:22 PM