Duc
Duc

Reputation: 21

Batch script to set a variable from text file not working

I am having an issue with setting a variable from a text file in a batch script. There is only one line in the text file and it is testabc.

The script is

set /p p_password=<c:\temp\passport.txt
echo %p_password%

The echo statement should have testabc, but it is actually having the below ■t.

I even tried it with a for loop

for /f "delims=" %%a in (c:\temp\passport.txt) do set p_password=%%a
echo %p_password%

I still get the same output; ■t

Any help is much appreciated.

Upvotes: 2

Views: 226

Answers (1)

JonathanDavidArndt
JonathanDavidArndt

Reputation: 2732

Yes, I can confirm that using different a file encoding format will fix this problem.

Using the input text file mentioned in your question, and saving the text file using UltraEdit, the results are listed below for the different encoding formats:

  • ANSI/ASCII = testabc
  • UTF-8 = ∩╗┐testabc
  • UTF-16 =  ■t
  • UTF-8 - NO BOM = testabc
  • UTF-16 - NO BOM = t
  • UTF-16 - Big Endian = ■ 
  • UTF-16 - Big Endian - NO BOM = (empty string)
  • Unicode - ASCII Escaped = testabc

As mentioned earlier by the commentor, it seems your text file was saved with a Byte order mark.

Upvotes: 1

Related Questions