如何使用Windows批处理脚本将空ASCII字符(nul)写入文件? [英] How can I write a null ASCII character (nul) to a file with a Windows batch script?

查看:478
本文介绍了如何使用Windows批处理脚本将空ASCII字符(nul)写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Windows批处理脚本向文件写入ASCII空字符(nul),但未成功.我最初尝试像这样使用echo:

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>

似乎应该可以工作(在命令窗口中键入<Alt+2+5+6>确实会写一个空字符-或显示为^@),但是echo然后输出:

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?

并挂起,直到我按<Return>.作为替代,我尝试使用:

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

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

完全可以满足我的需要,但前提是我必须在命令窗口中手动键入它.如果我从批处理文件运行它,它将挂起,直到按<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.

推荐答案

好的,这很棘手,解决方案很丑陋,但是可行.

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.

此批处理文件称为null.bat:

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

(最后一行仅包含空字符)将空字符附加到myfile.txt.

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

findstr /v /r显示所有与正则表达式不匹配的行,即仅最后一行,因为没有换行符.

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.

注意: findstr最初随Windows 2000一起提供,因此在早期版本的Windows上可能不可用

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

这篇关于如何使用Windows批处理脚本将空ASCII字符(nul)写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆