Copying one line output from a text file into a batch file as a variable?
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%
Answers (2)
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
........................................
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"
Comments:
-
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... - 56kGhost 11 years ago
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. - anonymous_9363 11 years ago