Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Import / Export CSV results

$
0
0

Hey folks, I’m after some of your expertise on finding some values within a csv file. I’m in a new job and I’m in the process of automating some of their daily tasks and
managed to save them lots of time with other scripts but this one is eluding me!

Very small file size of around 15 rows each with the key fields being “Job Code1”..through to “Job Code6” The other header is “Pay Rate1”..through to “Pay Rate6”

I only have 3 conditions which are that

1: No job code can be duplicated in that particular row. 2: If there is a Job code then there must be a Pay Rate. 3: If there is a Pay Rate then there must be a Job Code.

Basically everytime they get these files, they are manually opening a txt file and trying to spot the error of eg; Code3 has value but rate4 doesnt. Delete the offending rows and then import the file after been cleaned and send back the dodgy rows to other dept.

I was attempting to filter the file, after i had converted it to csv and cut out the rows into a new csv, so the old one could be imported without failing. The new file could simply be sent back for them to enter the correct data. Prob takes them 1 hour every day to do this clean up.

I hold my hands up and know only to well that my code is probably long winded and not efficient but im still learning this.

Import-Csv C:UsersAdminDocumentsCodeTestImporttest1.csv | Where-Object { ($.”Job Code1″ -gt “0”) -and ($.”Pay Rate1″ -eq “0”) -or ($.”Job Code2″ -gt “0”) -and ($.”Pay Rate2″ -eq “0”) -or ($.”Job Code3″ -gt “0”) -and ($.”Pay Rate3″ -eq “0”) -or ($.”Job Code4″ -gt “0”) -and ($.”Pay Rate4″ -eq “0”) -or ($.”Job Code5″ -gt “0”) -and ($.”Pay Rate5″ -eq “0”) -or ($.”Job Code6″ -gt “0”) -and ($.”Pay Rate6″ -eq “0”)

} | Export-Csv C:UsersAdminDocumentsCodeTestImportSample-Sorted.csv -NoTypeInformation

The above actually works fine for me, in that it creates another csv file with the rows that have at least one Job Code but no corresponding Pay rate value (just needs to be >0) Here is a sample of Headers and 1st row “First Name “,”Last Name “,”ID1″,”ID2″,”HireDate”,”LastRaiseDate”,”TDate”,”TReason”,”EmpStatus”,”Job Code1″,”Job Code2″,”Job Code3″,”Job Code4″,”Job Code5″,”Job Code6″,”Pay Rate1″,”Pay Rate2″,”Pay Rate3″,”Pay Rate4″,”Pay Rate5″,”Pay Rate6″ “Carl”,”Sagan”,”xxx-1111″,”111111″,”12/8/2011″,”4/3/2016″,””,””,”A”,”18″,”22″,”29″,”0″,”0″,”0″,”11.85″,”11.85″,”0″,”0″,”0″,”0″

This example would fail as it has Job Code3 but no corresponding Pay Rate3

I would be delighted if anyone can help me here or at least point me in the right direction of how i can cut rows that match the 3 conditions into a new file.

submitted by /u/rondo7
[link] [comments]

The post Import / Export CSV results appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images