简体   繁体   中英

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

Goal is to do the following using batch script:

  1. Extract hostname from windows machine.
  2. apply sha256 hash on hostname value (obtained from step1).

My system's hostname is: W0000000000ZQ

My batch script looks like this:

@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

在此处输入图片说明

Output from the script:

在此处输入图片说明

Can you please suggest how to remove CRLF and a newline from "hostname.txt" so that Certutil will pick correct string to generate sha256 value.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM