I have been trying to figure out how to make this work, and I have been working with the following line: (MAYBE POSSBILE WITH POWERSHELL OR VBSCRIPT???)

@echo off

SET var=

for /f "delims=" %%i in ("\\%COMPUTERNAME%\C$\Data\Info.txt") do set content=%content% %%i

::echoing to confirm that the text data was correctly set as "var"

echo %var%

2 Comments   [ + ] Show Comments


  • Use the right tool for the job.

    Batch was never intended for the kind of work you're trying to put it to. Yes, PS and VBS would be a much better option. Apart from any other consideration, the error-trapping available is an order of magnitude greater.

    You'll find quadzillions of sample scripts to open and read text files in either language.
  • True, I guess I am somewhat partial to Batch because it is the easiest for me to use most of the time....except for in this situation, LOL...I got it working in batch but only if the script is run locally and for some reason it only will pull some of the time...probably looking at a differnt route is the preferable method.
Please log in to comment



Batch can do it, but the variable assignment needs to be a sub routine of the FOR loop. See below:

@echo off
SET var=

for /f "delims=" %%i in (source.txt) do call :setVar %%i
echo final var = %var%
goto exit :SetVar
Set var=%var% %1%
goto :eof :exit
echo this is the end


Answered 10/09/2013 by: Utterly
Yellow Belt

Please log in to comment

If you are trying to read one line at a time then yes batch is quite capable of doing this.

If you are trying to read in an entire file into a variable then no, batch can't do this but it can be done by reading in the file one line at a time into an array...

For more information and help with Windows Shell Scripting (e.g.; Batch) check out the book by Tim Hill; "Windows NT Shell Scripting"

Answered 10/09/2013 by: thanateros
Senior White Belt

  • Yes, it is one line...one line text file...and I will check out that book, I managed to get it working kind of with batch, but it will return some file cannot be found error even when its in the same folder...
Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja