Combine Content of multiple files with "." as a seperator RRS feed

  • Question

  • Hello,

    We have 3 separate files that has been through encryption, base 64 encoding and as a last step they need to be concatenated. I have been using PowerShell for every other step in the process and now am trying to use PowerShell for concatenation as well.

    I was successfully able to do so by using the following command:

    Get-ChildItem path1\'+filname+'ph.enc.b64,  path1\'+filname+'.enc.b64 | ForEach-Object {gc $_; "."} | out-file path1\'+left(filname,7)+ ';
    type path1\'+left(filname,7)+' , path1\'+filname+'_pse.enc.signed.b64  > path2\'+Filname--+'.enc'

    Problem with this process is it creates a newline and carriage return characters before and after every "." separator. When I created the 3 source files individually I explicitly converted them to ASCII using "[System.Text.Encoding]::ASCII.GetBytes" before converting into base 64 format. But when I ran the above code for some reason the contents became UTF8 encoded. This was pointed out by the data host to whom we send the files. 

    my powershell version is 2 ,s o I could not use "-nonewline" option after out file option. 

    if the contents of 3 files are essentially in the following order:

    firstfile secondfile thirdfile

    I need an output that will look like :


    Any help or guidance will be highly appreciated. Thanks.

    Kindly note that on my personal laptop I have PowerShell 5 and I could write the following command which works but on our server it is Powershell2 hence the following will not work there.

    $fileName1 = "path1\xyz.tar.gz_passphrase.enc.b64"  ;
    $fileContent1 = get-content $fileName1;
    #$fileContentBytes1 = [System.Text.Encoding]::ASCII.GetBytes($fileContent1);
    $fileName2 = "path1\abc.tar.gz.enc.b64"  ;
    $fileContent2 = get-content $fileName2;
    #$fileContentBytes2 = [System.Text.Encoding]::ASCII.GetBytes($fileContent2);
    $fileName3 = "path1\def.tar.gz_passphrase.enc.signed.b64"   ;
    $fileContent3 = get-content $fileName3;
    #$fileContentBytes3 = [System.Text.Encoding]::ASCII.GetBytes($fileContent3);
    New-Item -Path path2\newfile.tar.gz -ItemType FIle -force
    #[System.IO.File]::WriteAllText("path2\newtest" + ".b64",$fileContentBytes)
    Add-Content path2\newfile.tar.gz $fileContent1 -NoNewline
    Add-Content path2\newfile.tar.gz -Value "." -NoNewline;
    Add-Content path2\newfile.tar.gz $fileContent2   -NoNewline;
    Add-Content path2\newfile.tar.gz -Value "." -NoNewline;
    Add-Content path2\newfile.tar.gz -Value $fileContent3 -NoNewline;


    Gaurav Mathur

    • Edited by XM1984 Tuesday, February 20, 2018 1:39 PM
    • Moved by Bill_Stewart Monday, April 30, 2018 9:16 PM This is not "scripts on demand"
    Tuesday, February 20, 2018 1:34 PM

All replies

  • You cannot cat base64 files.  They will not decode properly.

    To cat text files just add them together.

    cmd /c copy file1.txt+file2.txt+file3.txt allfiles.txt


    Get-Content file1.txt,.file2.txt,file3.txt | Out-File allfiles.txt -Append -Encoding ascii


    Tuesday, February 20, 2018 1:49 PM