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

风流意气都作罢 提交于 2019-12-30 03:12:45

问题


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:

echo <Alt+2+5+6>

which seems like it should work (typing <Alt+2+5+6> in the command window does write a null character - or ^@ as it appears), but echo then outputs:

More?

and hangs until I press <Return>. As an alternative I tried using:

copy con tmp.txt >nul
<Alt+2+5+6><Ctrl+Z>

which does exactly what I need, but only if I type it manually in the command window. If I run it from a batch file it hangs until I press <Ctrl+Z> but even then the output file is created but remains empty.

I really want the batch file to stand alone without requiring (for example) a separate file containing a null character which can be copied when needed.


回答1:


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




回答2:


Here's an article which describes how to write arbitrary bytes with a batch file (search for h2b.com). This effectively solves the problem of writing any non-printable data using a batch script (including null).




回答3:


I don't like the solution that cannot be easy copy/paste-d to text files.So few more ways:

1) mshta (can be directly used in batch file or from command line):

mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write( Chr(00) ):Close")>testfile

2) certutil (requires a temp file)

echo 00>null.hex
certutil -decodehex null.hex null.bin

3) makecab just check the subroutine here - http://ss64.com/nt/syntax-genchr.html




回答4:


An alternative to the accepted answer which doesn't involve having to put null characters into the batch file is as follows:

@echo off

set NULL_FILE=null.txt
set DEBUG_COMMANDS=write-null.dbg

echo e 100 >%DEBUG_COMMANDS%
echo 0 >>%DEBUG_COMMANDS%
echo n %NULL_FILE% >>%DEBUG_COMMANDS%
echo rbx >>%DEBUG_COMMANDS%
echo 0 >>%DEBUG_COMMANDS%
echo rcx >>%DEBUG_COMMANDS%
echo 1 >>%DEBUG_COMMANDS%
echo w >>%DEBUG_COMMANDS%
echo q >>%DEBUG_COMMANDS%

debug < %DEBUG_COMMANDS% >nul

del %DEBUG_COMMANDS%

This is obviously more verbose and also has the downside that it won't work on Win64 machines (due to debug no longer being available in those environments).



来源:https://stackoverflow.com/questions/2730732/how-can-i-write-a-null-ascii-character-nul-to-a-file-with-a-windows-batch-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!