How can I write a null ASCII character (nul) to a file with a Windows batch script?

前端 未结 4 786
一整个雨季
一整个雨季 2020-12-31 12:58

I\'m attempting to write an ASCII null character (nul) to a file from a Windows batch script without success. I initially tried using echo like this:

         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 13:17

    Okay, this was tricky, and the solution is ugly, but it works.

    You can use the batch file itself as the file containing the null character to be copied.

    This batch file, called null.bat:

    findstr /v /r \n null.bat >> myfile.txt
    [NULL]
    

    (where the last line contains only the null character) appends the null character to myfile.txt.

    findstr /v /r shows all lines that aren't matched by the regex, i.e. only the last one, because there's no newline character.

    I have tried several other things, but this was the only one I could find that didn't strip the null character.

    Note: findstr was first shipped with Windows 2000 so may not be available on prior versions of Windows

提交回复
热议问题