No announcement yet.

Help with out-file command

  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with out-file command

    I have a script that I am developing. It reads a text file and generates output records for a new file.

    It is a text file and I am using substring function to check for records that I want (must start with "A " and I know because the count is getting incremented correctly that it is correct in the flow...but no records...or actually what appears to only be one record, rather than the 396 that it is supposed to have...

    Here is the script... with some path info X'd out.

    $srcpath = "c:\xxxxxx\Documents\projects\NCOA\"
    $destpath ="\\xxxxxxxx\DATA_Con\NCOA updates"
    $filein = "cstacs01.txt"
    $fileout = "testout.txt"
    $myrecs = Get-Content ($srcpath + $filein)  
    $outcnt = 0
    ForEach ($m in $myrecs)
    	if ($check_a = $m.StartsWith("A    ")) {
           $addr1 = $m.substring(27,48 )
    	$city = $m.Substring(75,28 )
    	$st = $m.Substring(103,2)
    	$zip = $m.Substring(106,5)
    	$letter = $m.Substring(342,4)
    	$debtor = $m.Substring(497,6)
    	$outrec = "Debtor #" + $debtor + "-NCOA forwarded Address:" + $add1
    	out-file -filepath  ($srcpath + $fileout) -inputobject $outrec -force 

    so the $outcnt variable is getting incremented to the correct count and I have stepped though in debug and see the variables are getting populated properly...

    but when I open the output file - I only have one record and it is incomplete. I am going to check but I also think it is not one that meets the "starts with" criteria

    Debtor #223892-NCOA forwarded Address:

    I am also posted an image of the screen which shows an excerpt of the variables
    Attached Files
    Last edited by Rems; 24th January 2015, 05:26. Reason: Fix 8) smilie issue

  • #2
    Re: Help with out-file command

    Would I be correct in assuming that the only single record you see in your text file is the last entry from source? If so, the reason is your out-file statement. You're writing only the one entry, each time, as if it's the first one. That action wipes the previous entry.

    Using our buddy , we find that there's a switch you should be using in your out-file command, '-Append'. Put that switch in and take '-Force' out. You should only need to force it if there's a permissions issue. But without the '-Append' switch, each output line will destroy the previous line. Appending the single line adds to the file, which is what you want.
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **