0

I have text file having content like

8.4.0.154

newline

I have written below command in a batch file to read the first line means 8.4.0.154

set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%

here it is just printing 8 prefixes with some special symbol as shown in below image

printing only 8 prefixes with some special symbol

could anyone help me out here to figure it out that why am not able to read complete number in batch file and print it out.

Thanks in advance

Community
  • 1
  • 1
Piyush
  • 5,145
  • 16
  • 49
  • 71
  • 2
    You usually see something like this when the file you're reading isn't saved with ANSI encoding. – SomethingDark Feb 20 '19 at 06:15
  • 2
    Probably your file has a leading BOM – jeb Feb 20 '19 at 06:16
  • @GerhardBarnard - Nothing is getting printed. – Piyush Feb 20 '19 at 06:25
  • @picnic4u yeah, ignore that comment, I did not format correctly. Do you have notepad++? Did you perhaps test other encoding yet? the line before your `set` do `chcp 65001` – Gerhard Feb 20 '19 at 06:26
  • Please read Wikipedia article about [character encoding](https://en.wikipedia.org/wiki/Character_encoding), first two chapters of UltraEdit power tip [Working with Unicode in UltraEdit/UEStudio](https://www.ultraedit.com/support/tutorials-power-tips/ultraedit/unicode.html) and the two referenced articles, the Wikipedia pages about [UTF-16](https://en.wikipedia.org/wiki/UTF-16) and [UTF-8](https://en.wikipedia.org/wiki/UTF-8). Windows command processor interprets by default any text file as being encoded with one byte per character using code page as displayed on running `chcp` in cmd window. – Mofi Feb 20 '19 at 06:44
  • The OEM code page used by Windows command processor `cmd.exe` depends on country configured for the user account used on running the batch file. The code page can be modified within a batch file using command `chcp` (change code page). For even more details see on Stack Overflow [CMD can't read Danish characters when I execute .bat file](https://stackoverflow.com/a/43052862/3074564) and [Using another language (code page) in a batch file made for others](https://stackoverflow.com/a/48982681/3074564). – Mofi Feb 20 '19 at 06:51
  • 1
    You could try the following to read and convert your Unicode file properly: `for /F %I in ('type "%scriptLocation%\assemblyVersion.txt"') do @echo %I` – aschipfl Feb 20 '19 at 09:25

1 Answers1

0

I was creating this text file using PowerShell script using below command:

param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version > $textFile

The text file was not getting created with ANSI encoding hence, unable to read using a batch file.

Now, I changed the above code as below and it is working.

param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version | Out-File $textFile -Encoding Ascii

I am able to read the text file content using below command

set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%
double-beep
  • 5,031
  • 17
  • 33
  • 41
Piyush
  • 5,145
  • 16
  • 49
  • 71