1

I need to take a CSV file and then create many files out of that in different formats depending on the account number. The account numbers I need are in other files with no other fields (I need to pad them to match). This is just one below.

I know I can cycle through using foreach, but was trying to avoid that. This gives no error, but doesn't work even though I know there are matches.

$sms_accts = import-csv -path "H:\scrap\Positive Pay Feeds\sms_accounts.txt"
$input_file = import-csv -path "H:\scrap\Positive Pay Feeds\CNVDC939.POSITIVE PAY ISSUES.txt"

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

$to_sms_file = $input_file  | where-object { $sms_accts -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0"))  }

$to_sms_file

It returns nothing.

Sample file below.

sms_accounts.txt:

Acct
12345678
23456789


Positive Pay:
    "SPONSOR_BANK_ID","BE_CLIENT_ID","COMPANY_NAME","ACCOUNT_NUMBER","SERIAL","AMOUNT","PAYEE","ISSUE_TYPE","STATUS","ENTRY_DATE","ISSUE_DATE"
    "100741","1004928","Acme","0012345678","11111","2468","","0","1","2/11/2014 5:50:34 PM","2/10/2014"
    "100741","1004928","ABC","009999999","22222","180.34","","0","1","2/20/2014 9:01:38 PM","2/20/2014"
2
  • Can you give example for the CSV files (headers with fake but coherent contents)? Commented Mar 18, 2014 at 16:34
  • See above for sample files. Commented Mar 18, 2014 at 18:03

2 Answers 2

1

You can use Acct property to filter $sms_accts

... | where-object { $sms_accts.Acct -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0"))}

in this way you don't need to use foreach in your code.

Sign up to request clarification or add additional context in comments.

Comments

0

You process $sms_accts into an array of custom objects with the single property Acct:

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

but then check the array for a particular string:

... | where-object { $sms_accts -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0")) }

Since none of your custom objects is a string (even though each has a string property), you don't get a match. Change the two lines

$sms_accts = import-csv -path "H:\scrap\Positive Pay Feeds\sms_accounts.txt"

and

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

into this:

$sms_accts = Import-Csv "H:\scrap\Positive Pay Feeds\sms_accounts.txt" | % {
               $_.Acct.PadLeft(11,"0")
             }

and your check will work as expected.

5 Comments

Thanks. That works. I think I get what you are saying. What if the file had more fields than account number though? How could I use it?
@user1921849 That depends on what you actually want to do with it.
I am saying I want to do the exact thing- look for matches- but if the file had acct+other columns. Like is there a way to use my original custom object in the Where-Object.
@user1921849 Matches of what? For matches of any single field (Acct or whatever) you'd do the same thing: turn it into an array and check if the array -contains a given value. If you want to check multiple properties you'd need to create just as many arrays. If you have PowerShell v3 or newer you could also use @amirhosseinab's suggestion and do something like this: $sms_accts.Acct -contains X -and $sms_accts.PropA -contains Y.
I did upgrade to PS 4.0 and then $sms_accts.Acct works also with my original code with the .Acct added to the where-object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.