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)
1
  • You can use the FileStream class with FileMode Append. See also here... Commented Aug 19, 2015 at 18:50

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

11 Comments

i tried that by using $data += $bytes and writing $data but the same file was displayed 3 times instead of the three different files.
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.
I did that already and it displayed different files letting me know that it was looping correctly.
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?
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.
|

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.