【问题标题】:How to remove CRLF and a new line from text file using batch command如何使用批处理命令从文本文件中删除 CRLF 和新行
【发布时间】:2020-09-10 06:57:22
【问题描述】:

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

  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.

主机名.txt:

主机名-manual.txt

脚本的输出:

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

【问题讨论】:

  • 有点挑剔:你没有得到 CRLF 换行符;你只是得到一个 CRLF,它 换行符。
  • 试试&lt; nul &gt; "hostname.txt" set /P ="%ComputerName%"...

标签: windows batch-file newline


【解决方案1】:

以下内容将主机名保存到不带尾随 CRLF 的文件中。

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

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2018-12-25
    相关资源
    最近更新 更多