/build/static/layout/Breadcrumb_cap_w.png

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%


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. - anonymous_9363 10 years ago
  • 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. - 56kGhost 10 years ago

Answers (2)

Posted by: anonymous_89149 10 years ago
Yellow Belt
1

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

........................................

Posted by: thanateros 10 years ago
Senior White Belt
1

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 10 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ