Jatin
Jatin

Reputation: 1997

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:

enter image description here

hostname-manual.txt

enter image description here

Output from the script:

enter image description here

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.

Upvotes: 3

Views: 2087

Answers (1)

dxiv
dxiv

Reputation: 17678

The following saves the hostname to a file without the trailing CRLF.

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

Upvotes: 4

Related Questions