如何使用 Windows 批处理脚本将空 ASCII 字符 (nul) 写入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2730732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:22:06  来源:igfitidea点击:

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

windowsnullbatch-fileascii

提问by Matthew Murdoch

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

我试图将 ASCII 空字符 (nul) 从 Windows 批处理脚本写入文件,但没有成功。我最初尝试这样使用echo

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 echothen outputs:

看起来它应该可以工作(<Alt+2+5+6>在命令窗口中输入确实写了一个空字符 - 或者^@它出现时),但echo随后输出:

More?

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

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

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.

这正是我需要的,但前提是我在命令窗口中手动键入它。如果我从批处理文件运行它,它会挂起,直到我按下,<Ctrl+Z>但即使如此,输出文件已创建但仍为空。

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.

我真的希望批处理文件独立存在,而不需要(例如)包含空字符的单独文件,可以在需要时复制该文件。

采纳答案by balpha

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:

这个批处理文件,称为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.

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

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

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

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

我已经尝试了其他几种方法,但这是我能找到的唯一一种没有去除空字符的方法。

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

注意:findstr最初随 Windows 2000 一起提供,因此可能不适用于之前版本的 Windows

回答by Matthew Murdoch

Here's an articlewhich 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).

这是一篇介绍如何使用批处理文件写入任意字节的文章(搜索h2b.com)。这有效地解决了使用批处理脚本(包括空值)写入任何不可打印数据的问题。

回答by npocmaka

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):

1)mshta(可以直接在批处理文件中或从命令行使用):

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

2) certutil(requires a temp file)

2)certutil(需要一个临时文件)

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

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

3) makecab只是在这里检查子程序 - http://ss64.com/nt/syntax-genchr.html

回答by Matthew Murdoch

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 debugno longer being available in those environments).

这显然更冗长,而且还有一个缺点,那就是它不能在 Win64 机器上运行(因为debug在这些环境中不再可用)。