如何使用批处理命令从文本文件中删除 CRLF 和新行

How to remove CRLF and a new line from text file using batch command

目标是使用批处理脚本执行以下操作:

  1. 从 windows 机器中提取主机名。
  2. 对主机名值应用 sha256 哈希(从步骤 1 获得)。

我的系统主机名是:W0000000000ZQ

我的批处理脚本如下所示:

@echo off
hostname>hostname.txt
CertUtil -hashfile hostname.txt SHA256 //<- this is generating wrong sha256 string because
                                       //   the above generated file contains CRLF and a new line
                                       //   screenshot of this file provided below.
CertUtil -hashfile hostname-manual.txt SHA256 //<- this is generating correct sha256 string because
                                              //   i manually created this file and pasted hostname
                                              //   only (W0000000000ZQ) without CRLF and new line.
                                              //   screenshot of this file provided below.

hostname.txt:

主机名-manual.txt

脚本输出:

能否请您建议如何从“hostname.txt”中删除 CRLF 和换行符,以便 Certutil 将选择正确的字符串来生成 sha256 值。

以下将主机名保存到一个没有尾随 CRLF 的文件中。

for /f %%h in ('hostname') do (>hostname.txt <nul set /p unused=%%h)