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

发布于 2024-08-30 19:19:53 字数 605 浏览 4 评论 0原文

我尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神魇的王 2024-09-06 19:19:53

好吧,这很棘手,而且解决方案很丑陋,但它有效。

您可以使用批处理文件本身作为包含要复制的空字符的文件。

此批处理文件名为 null.bat:(

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

其中最后一行仅包含空字符)将空字符附加到 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:

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

汹涌人海 2024-09-06 19:19:53

我不喜欢无法轻松复制/粘贴到文本文件的解决方案。所以还有几种方法:

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

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

2 ) certutil (需要临时文件)

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

3) ma​​kecab 只需检查此处的子例程 - 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):

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

-残月青衣踏尘吟 2024-09-06 19:19:53

这里有一篇文章,描述了如何使用批处理文件写入任意字节(搜索对于 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).

习ぎ惯性依靠 2024-09-06 19:19:53

不涉及将空字符放入批处理文件中的已接受答案的替代方案如下:

@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%

这显然更冗长,并且还有一个缺点,即它无法在 Win64 计算机上工作(由于 debug 在这些环境中不再可用)。

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

笑忘罢 2024-09-06 19:19:53

创建大小为 的空文件:

fsutil file createnew <file> <size>

create null file of size <size>:

fsutil file createnew <file> <size>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文