如何使用 Windows 批处理脚本将空 ASCII 字符 (nul) 写入文件?
我尝试从 Windows 批处理脚本将 ASCII 空字符 (nul) 写入文件,但没有成功。我最初尝试像这样使用 echo
:
echo <Alt+2+5+6>
这看起来应该可以工作(在命令窗口中输入
确实会写入一个空字符 -或 ^@
(如出现的那样),但 echo
然后输出:
More?
并挂起,直到我按
。作为替代方案,我尝试使用:
copy con tmp.txt >nul
<Alt+2+5+6><Ctrl+Z>
它完全符合我的需要,但前提是我在命令窗口中手动键入它。如果我从批处理文件运行它,它会挂起,直到我按
,但即使如此,输出文件也会创建但仍为空。
我真的希望批处理文件能够独立存在,而不需要(例如)包含空字符的单独文件,可以在需要时复制该文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,这很棘手,而且解决方案很丑陋,但它有效。
您可以使用批处理文件本身作为包含要复制的空字符的文件。
此批处理文件名为
null.bat
:(其中最后一行仅包含空字符)将空字符附加到
myfile.txt
中。findstr /v /r
显示正则表达式不匹配的所有行,即仅最后一行,因为没有换行符。我尝试了其他几种方法,但这是我能找到的唯一一种没有删除空字符的方法。
注意:
findstr
最初随 Windows 2000 一起提供,因此可能无法在早期版本的 Windows 上使用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
:(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我不喜欢无法轻松复制/粘贴到文本文件的解决方案。所以还有几种方法:
1)mshta(可以直接在批处理文件中或从命令行使用):
2 ) certutil (需要临时文件)
3) makecab 只需检查此处的子例程 - http://ss64.com/nt/syntax-genchr.html
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):
2) certutil (requires a temp file)
3) makecab just check the subroutine here - http://ss64.com/nt/syntax-genchr.html
这里有一篇文章,描述了如何使用批处理文件写入任意字节(搜索对于
h2b.com
)。这有效地解决了使用批处理脚本写入任何不可打印数据(包括null)的问题。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).不涉及将空字符放入批处理文件中的已接受答案的替代方案如下:
这显然更冗长,并且还有一个缺点,即它无法在 Win64 计算机上工作(由于
debug
在这些环境中不再可用)。An alternative to the accepted answer which doesn't involve having to put null characters into the batch file is as follows:
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).创建大小为
的空文件:create null file of size
<size>
: