|
Answer» I know you can pull data from a text file using the following:
Code: [Select]FOR /f %%I in (file location) DO command %%I
but I am trying to gather more than one set of data. I will need to have 5 variables pulled from a text file. The text file will have the following format
73130 815000 1285000 820000 1280000
this will continue for around 700 entries or so (amount varies). I am looking for the ability to pull the first line of data then set each value to a different variable. use the variables then continue to the next next line.
thank you, WayneCode: [Select] for /f "tokens=5" %%I in ('type file.txt') do ( echo %%I %%J %%K %%L %%M rem your data is now in the variables %%I %%J %%K %%L %%M ) FBDoesn't anybody ever test before posting?
Code: [Select][emailprotected] off for /f "tokens=1-5" %%I in (file.txt) do ( echo %%I %%J %%K %%L %%M )
%%I = 73130 %%J = 815000 %%K = 1285000 %%L = 820000 %%M = 1280000
sorry still not working for me. Code: [Select]@echo off CLS for /f "tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( echo %%I %%J %%K %%L %%M ) pause
the test.txt contains the following: 73130 -80.508384995 27.867908653 -80.492973668 27.854099953 73131 -80.492909668 27.867852625 -80.477500339 27.854042223
when I run I GET just the pause and no ECHO. Imagine that, quotes in a file name. Must be a new concept. Using quotes in a file name is FINE, but you need to let the for command that there are new semantics. Try using the usebackq parameter where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.
Code: [Select]@echo off CLS for /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( echo %%I %%J %%K %%L %%M ) pause
Good luck.
OK got it. I also got it to work with the 8-dot-3 file names.
thanks!is there a way to step through the text file pulling one line at a time. Set the response to vars and then use them. When done go back to the text file and get the next line of data?
example: Code: [Select]@echo off CLS FOR /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do GOTO ECHO
:ECHO ECHO %I% ECHO %J% ECHO %K% ECHO %L% ECHO %M% pause What are you trying to do? If you are planning arithmetic operations on the columns, keep in mind batch code only does integer math. To keep the number of variable names unique and to a minimum you might set up a loop with compound (x.y) variable names.
Otherwise in keeping with your posted code, this may help:
Code: [Select]@echo off CLS FOR /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( ECHO %I% ECHO %J% ECHO %K% ECHO %L% ECHO %M% ) pause
Happy Coding
thanks!
I jot a different work around for it. Code: [Select]@echo off CLS FOR /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( SET I=%%I && SET J=%%J && SET K=%%K && SET L=%%L && SET M=%%M && CALL :ECHO %%I )
ECHO JOB COMPLETED! PAUSE GOTO END
:ECHO ECHO %I% ECHO %J% ECHO %K% ECHO %L% ECHO %M%
:END
basically I am taking a list of tile numbers and Decimal Degrees Coordinates and I am going to make XML files with the vars that are set.
a few more minor tweaks and it should be up and running.
thanks again for everyone that helped out with this. WayneQuote from: wbrost on January 08, 2009, 02:14:02 PM thanks!
I jot a different work around for it. Code: [Select]@echo off CLS FOR /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( SET I=%%I && SET J=%%J && SET K=%%K && SET L=%%L && SET M=%%M && CALL :ECHO %%I )
ECHO JOB COMPLETED! PAUSE GOTO END
:ECHO ECHO %I% ECHO %J% ECHO %K% ECHO %L% ECHO %M%
:END
basically I am taking a list of tile numbers and Decimal Degrees Coordinates and I am going to make XML files with the vars that are set.
a few more minor tweaks and it should be up and running.
thanks again for everyone that helped out with this. Wayne
This will only work for the last set of data pulled from the file, If you want to work with the data one line at a time, it's going to have to be within the for loop. If you're convinced it needs to be set to different variables you'll need 'setlocal enabledelayedexpansion' e.g.
Code: [Select]@echo off setlocal enabledelayedexpansion CLS FOR /f "usebackq tokens=1-5" %%I in ("C:\Documents and Settings\ss947bw\Desktop\test2\test.txt") do ( SET I=%%I && SET J=%%J && SET K=%%K && SET L=%%L && SET M=%%M echo !I! echo !J! echo !L! echo !L! echo !M!)
ECHO JOB COMPLETED! Doesn't anybody ever reread before posting? Quote from: Sidewinder on January 08, 2009, 11:42:47 AMCode: [Select][emailprotected] off
FBdid you even try my code before you posted? if you did then you would know that it works. Please let me know if you are having TROUBLE getting it to work. I have one last question (crossing FINGERS)
why will this code work correctly: Code: [Select]ECHO ^<horizpa^> >> "%CD%\xml_files\%name%%I%.xml"=
and this code will not? Code: [Select]ECHO ^<horizpar^>Orthophotography complies with NMAS, National Map Accuracy Standard for 1"=200` maps.^</horizpar^> >> "%CD%\xml_files\%name%%I%.xml"
when I run the batch it gets to this point and I get the following error: the filename directory name or volume label syntax is incorrect
the batch still processes through but I will not ADD the above line to the xml file. If I rem out the code the batch works perfectly.
any ideas?
thanks, Wayne
|