About the author

Related Articles

5 Comments

  1. 1
    S

    S Techy Tse

    just fyi.. i tried it and didn’t work.. But I realized finally that I had to re-produce the text file (originally the text file is generated by a powershell script) so when I did this and it works.

    Type Text.txt > text2.txt
    (output to different file)
    Then apply the same code (change text.txt to text2.txt)

    set /p var= <Text2.txt
    echo %var%

    If I use the original one, I would either get garbage in %var%, not sure if the text file has not or what. Anyway, by reproducing the text file, it seems working fine.

    Reply
  2. 2
    Lưu

    Lưu Vĩnh Phúc

    If delayed expansion has been enabled before (Setlocal EnableDelayedExpansion) then there’s a shorter (one-liner) version

    set /pz=<filename.txt&echo !z!
    
    Reply
  3. 3
    user3110750@email.null'

    user3110750

    I couldn’t get this to work under Windows 7; the variable was simply not set. But here’s a slightly longer solution:

    for /F "delims=" %%i in (Text.txt) do (  
      set Z=%%i
      goto BREAK1
    )
    :BREAK1
    echo %Z%
    
    Reply
  4. 4
    nephi12@email.null'

    nephi12

    this is just a slightly shorter version:

    set/pz=<file
    echo %z%
    
    Reply
  5. 5
    Joe@email.null'

    Joe

    set /p var= <Text.txt
    echo %var%
    

    Referenced from: http://forums.techguy.org/software-development/551867-batch-file-read-line-text.html
    Ironically the poster is able to get the first line but wanted to loop through the whole file!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2017 SolutionMmyself.com