0

So here is my code:

@echo off
set WAL=wallpaper.txt
echo ‰PNG >> %WAL%
echo  >> %WAL%
echo     >> %WAL%
echo IHDR   I      ZòRã     pHYs  Ä  Ä•+  iIDATX…í—­®â@Çÿws áÈî Qh’TµO@RU[GÕ¸6!iUÈ…ìŒÅ…jæ >> %WAL%
echo véÉL/_eÙÝ\²?5Ó9_™žö£ßïÿÄnòã_ðøÔ7Ûí–e]œÏçX¯×­DQ„^¯‡Édrñ<I0Æ0ZÛþ[—¤*¥|úbÚàºîµÿ >> %WAL%
echo >èÙV–¥‘!RJZçyŽN§Û¶é¬)˜™Æ9GÇHÓŽãžëºd[)e¼Ì$IÈGóìRexž!„¡ÀX7iÕ“²,CY–°m›ŒFQDÁJ)éÌ÷}¸®‹<ÏIçZÉ5‡°mžçÁ¶mH)i­ûŒ¢UU]G)Ev€sËB Š"0ÆH/ÏsdYv5ž‡3‰sÆ9€Ýn‡ñxL{ÆØ£æn†! @¥6› >> %WAL%
echo „ €¢(Ðét  ¾ïzeYÒ™eY8†n· Øl6¤·Z­Ç18çäG§u¹Åqlì•R ν¥Î&àvúþ.z 5KJ/Ë^¯àü‚-ËÂñx$9Çq¨¤ïÑú’êš¾DÝ„“$A–e—׳Ôå¯ûÕaŒÑ¥åyn¡4M±X,òópOB ,KÌf³»²UUÑút:]ý¬xº/={c˜ÏçF¬)ŠÂh÷h•I“ÉÛíÖ˜bõôi¦}ðb±€ã8R’ì«ÃqS<z\ËåA zVgïûFk ¾NFwý-i~çM§SAðT¯|ËßÎ9 `¿ßÓ³n·KC¦-­÷w@4M¿Lâg'îÛ–Û+yËr{5¿ ÇË<ÐÉ•~    IEND®B`‚ >> %WAL%
pause
ren C:\Users\Moi\Desktop\test\wallpaper.txt wallpaper.png
pause
exit

What it does is it writes something into a text file and then converts it to a .png (the text is what I get when I convert a .png to a .txt)

But it only writes

‰PNG

in the file and doesn't convert it to a .png

Is there another way to do it? Am I doing it wrong? All help is appreciated.

2
  • Search for topic "How to keep binary data in batch file and write into a file"
    – Daemon-5
    Mar 30, 2022 at 11:25
  • Unless you are using codepage 437 (by chcp 437), many extended characters may become converted to something else. Besides that, you won't be able to echo out null-bytes. So a batch file is probably the worst choice to create a binary file…
    – aschipfl
    Mar 30, 2022 at 11:35

1 Answer 1

1

That won't work, because echo will append a CRLF at each end of line, and won't tolerate control characters globally.

The "good" method, with only native tools, is:

Encoding image to batch:

  • Using Powershell, encode your file (the original image, in your case) in Base64.
  • Split the B64 file into lines of maximum 4096 characters.
  • Assign that to consecutive variables in your batch.

Decoding image to disk:

  • Do an echo of all previously encoded variables in a temporary file (i.e. in %TEMP% folder).
  • Using Powershell, convert the B64 file to a binary file.
  • Delete temporary B64 file.
  • Do what you want with the binary file.

Reference: Base64 encoding/decoding with Powershell

2
  • You can also use certutil -encode for most images instead if you don't want to make a hybrid script. Mar 30, 2022 at 11:33
  • 2
    @SomethingDark Script is not really hybrid: that's a single call to powershell, a one-liner. Nothing more than what certutil would do: you wouldn't call script "hybrid" if it calls a standard Windows executable, so it isn't different with calling one-liner Powershell. The annoying part remains to encode/split/reassemble the file in Base64.
    – Wisblade
    Mar 30, 2022 at 11:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.