trying to set token read in windows batch to variable.

Greetings,
I have a file that I need to read.
I am using a windows batch to manipulate the file.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SET PARAMETER VARIABLES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set BaseDIR="./"
REM set ZIP="test.zip"
set usetext=false
set table=
set line=
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: UNZIP INFADS ZIP FILE TO OBTAIN MANIFAST FILE
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM unzip -j %BaseDIR%/%ZIP%
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: READ MANIFEST FILE AND CREATE SCHEMA FILES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::rm %BaseDIR%/tablelist.txt
for /f "tokens=* delims=" %%f in (master_index.txt) do (
	set line=%%f
	echo %line%
	if "%line:~0,5%" == "TABLE" (
		for /f "tokens=1,2 delims=: " %%a in (%line%) do (
			set nulltext=%%a
			set usetext=true
			set table=%%b
			echo record > %BaseDIR%/output/%table%.txt
			echo   {final_delim=end, record_delim='\n', delim=',', quote=double, padchar='#'} >> %BaseDIR%/output/%table%.txt
			echo ( >> %BaseDIR%/output/%table%.txt
		)
	) else (
		if %usetext% == "true" (
			for /f "tokens=1,2 delims=  " %%a in (%line%) do (
				set field=%%a
				set datatype=%%b
			)
		) else (
			if "%line%" == "" (
				set usetext=false
				echo ) >> %BaseDIR%/output/%table%.txt
			)
		)
	)
)

Open in new window


my problem is this:
for /f "tokens=* delims=" %%f in (master_index.txt) do (
	set line=%%f
	echo %line%

Open in new window

returns: "ECHO is off"

but this:
for /f "tokens=* delims=" %%f in (master_index.txt) do (
	set line=%%f
	echo %%f

Open in new window

returns the value of %%f

in the code snippet above, I am trying to use the variable %line% in multiple places.  I also need to use a substring of %line% in the first if/then statement, and I've been told you can't substring a %%f output.

Is there something I am missing here?
Thanks Everyone.
LVL 9
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasMcA2Commented:
To start with, try fixing your parenthesis. See attached screen shot. If you use a programming editor like Notepad++, the syntax highlighting (colors) make it a lot easier to recognize problems.

Mismatched parens
As you move your cursor through the code, the editor can colorize the matching parens/brackets.
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
HI, so I thought the same thing at first,
so I reduced the code:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SET PARAMETER VARIABLES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set BaseDIR="./"
REM set ZIP="test.zip"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SET GLOBAL VALUES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set usetext=false
set table=
set line=
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: UNZIP INFADS ZIP FILE TO OBTAIN MANIFAST FILE
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM unzip -j %BaseDIR%/%ZIP%
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: READ MANIFEST FILE AND CREATE SCHEMA FILES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if EXIST %BaseDIR%/tablelist.txt (
	rm %BaseDIR%/tablelist.txt
)
for /f "tokens=* delims=" %%f in (master_index.txt) do (
	set list=%%f
	echo %list%
)

Open in new window


and I still get the same type of response.
Can you see anything else?
Thanks
0
Bill PrewCommented:
Your problem is "delayed expansion" of environment variables.  Anytime you set the value of a variable inside a loop, and then want to reference the new value, you have to use delayed expansion.  Basically you add the following line near the top of your script:

setlocal EnableDelayedExpansion

Open in new window

and then change your FOR loop to this:

for /f "tokens=* delims=" %%f in (master_index.txt) do (
	set list=%%f
	echo !list!
)

Open in new window

Further info on this here:

http://www.robvanderwoude.com/variableexpansion.php
http://ss64.com/nt/delayedexpansion.html

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
hi bp...
Thank you so much,
if I have to reference !list! from that point forward,
how do I do the substring line:       if "%line:~0,5%" == "TABLE" (

Thanks
Evan
0
Bill PrewCommented:
Just use ! Where you would normally use %. Once you are outside the loop you can go back to using % again.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.