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

3

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

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

我的计算机主机名是: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:

输入图像描述

hostname-manual.txt:

输入图像描述

脚本输出:

输入图像描述

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

1
稍微挑剔一下:你并不是得到了一个CRLF和一个换行符;你只是得到了一个CRLF,这个CRLF就是换行符。 - k314159
2
尝试 < nul > "hostname.txt" set /P ="%ComputerName%"... - aschipfl
1个回答

4
以下内容将主机名保存到文件中,不包含尾随的 CRLF。
for /f %%h in ('hostname') do (>hostname.txt <nul set /p unused=%%h)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接