0

i am trying to append binary AFP files into one file. When I used my code below the same file gets written three times instead of the three files I have getting appended to one file. Why would the value of $bytes not change? Get-Content was unsuccessful without causing errors in the AFP file.

$dira = "D:\User1\Desktop\AFPTest\"

$list = get-childitem $dira -filter *.afp -recurse | % { $_.FullName } | Sort-Object

foreach($afpFile in $list){

    $bytes = [System.IO.File]::ReadAllBytes($afpFile) 

    [io.file]::WriteAllBytes("D:\User1\Desktop\AFPTest\Content.afp",$bytes)

}

The script below is after I made a change to store the $bytes to a $data variable and then write out $data.

$dira = "D:\User1\Desktop\AFPTest\"

$list = get-childitem $dira -filter *.afp -recurse | % { $_.FullName } | Sort-Object -descending



foreach($afpFile in $list){

    Write-Host $afpFile

    $bytes = [System.IO.File]::ReadAllBytes($afpFile)

    $data += $bytes 

}
    [io.file]::WriteAllBytes("D:\User1\Desktop\AFPTest\Content.afp",$bytes)

I attempted to combine them manually by setting each of the three files to a variable and then adding them to the $data array but the same issue happens of the repeated image. The code is below.

$dira = "D:\User1\Desktop\AFPTest\"

$list = get-childitem $dira -filter *.afp -recurse | % { $_.FullName } | Sort-Object

$file3 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000001.afp")

$file2 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000002.afp")

$file1 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000003.afp")

$data = $file1 + $file2


[io.file]::WriteAllBytes("D:\User1\Desktop\AFPTest\AFP.afp",$data)
cchristie45
  • 43
  • 2
  • 7
  • You can use the FileStream class with FileMode Append. See also [here](http://stackoverflow.com/questions/6862368/c-sharp-append-byte-array-to-existing-file)... – Peter Schneider Aug 19 '15 at 18:50

1 Answers1

2

WriteAllBytes() always creates a new file. You want to append. Try this:

...
$bytes = @()
foreach($afpFile in $list) {
    $bytes += [System.IO.File]::ReadAllBytes($afpFile) 
}
[io.file]::WriteAllBytes("D:\User1\Desktop\AFPTest\Content.afp",$bytes)
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • i tried that by using $data += $bytes and writing $data but the same file was displayed 3 times instead of the three different files. – cchristie45 Aug 19 '15 at 20:16
  • Just to be clear, you're saying that the byte content for one of the files is repeated three times in $bytes? If so, do a `Write-Host $afpFile` just to make sure you're not processing the same file over and over. – Keith Hill Aug 19 '15 at 21:24
  • I did that already and it displayed different files letting me know that it was looping correctly. – cchristie45 Aug 20 '15 at 12:56
  • Can you update your question with the script you are trying now - moving the WriteAllBytes() out of the loop and accumulating the byte data into $data? – Keith Hill Aug 20 '15 at 15:02
  • You should be referencing $data in the call to WriteAllBytes. Also, I would set to `$data = @()` before the foreach loop just to make sure it is initialized to empty. – Keith Hill Aug 20 '15 at 16:24
  • I made that change and I am still getting missing resource errors when opening the afp, this happens when I sort in ascending, when I sort descending the files are combined but the images of one file are repeated, even though the file size of the combined file is the sum of the three different files. I attempted to combine them manually by setting each of the three files to a variable and then adding them to the $data array but the same issue happens of the repeated image. The code is above. – cchristie45 Aug 20 '15 at 16:37
  • When I write out $bytes to a separate file each time it loops, the files are written correctly, so the $afpFile does change, but the problem occurs when combining the files. – cchristie45 Aug 20 '15 at 16:59
  • Is it possible that two of the files have identical content? Looking at your last example, I don't see how this could get fouled up. Although you are not adding in $file3 to $data. – Keith Hill Aug 20 '15 at 17:55
  • I checked that. Is it possible in powershell to combine binary arrays? – cchristie45 Aug 20 '15 at 18:24
  • Yes that is what `+` operators does with arrays e.g. $file1 + $file2 + $file3 concats the three binary arrays into one that is then assigned to $data. Of course, the operation of `+` depends on what $file1 is but as long as it is an array it will then create a new array and copy both arrays (LHS and RHS) into the new array. If you look at the length of $file1, $file2 and $file3 it should add up to the length of $data ($data.Length). Does it? – Keith Hill Aug 20 '15 at 20:48
  • The lengths do add up to the same length. – cchristie45 Aug 21 '15 at 12:28