declare var inside var &text format

Asus / -
January 17, 2009 at 07:37:24
Specs: XP, P4 & 1GB
here goes the code:
set /a n=0
for /f "skip=1 tokens=1-4 delims=	" %%a in (gp.csv) do (
	set /a n+=1
	set +!n!=%%~a
	set -!n!=%%~b
	set Q!n!=%%c
	set V!n!=%%~d
	set D!n!=N/A
	for /f "skip=4 tokens=3 delims=	" %%e in ('reg query %%c /v %%d 2^>nul') do set D!n!=%%e 
	echo !n!. %%~nc	[%%~d]	= !D!n!!


<+DATA>		<-DATA>	<QUERY>									<VALUE>
"0x1"		"0x0"	"HKLM\SYSTEM\CURRENTCONTROLSET\CONTROL\StorageDevicePolicies"		"WriteProtect"
"0x5"		"0x2"	"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet\Settings"	"MaxConnectionsPerServer"

here is my problem:
1. the last line of code. i need to echo out the var D1,D2,D3. how do i write it? i try a couple of combination, !D!n!!, %D!n!%, %D%n%% and none working. i know i can write another for loop to do it, but i want to know if i can do it inside the original loop.
2. i need to display the menu in properly aligned, like the TABLE,TR,TD code in html?
something like below, how do i achieve it?

1. UserList     		[]      			= n
2. StorageDevicePolicies   [WriteProtect]  		= n
3. Explorer     		[NoRecentDocsNetHood]   	= n
4. Explorer     		[UseDesktopIniCache]   	= n
5. Paramaters   		[SizRegBuf]     		= n
6. Settings     		[MaxConnectionsPerServer]       = n

please help. thanks.

See More: declare var inside var &text format

Report •

January 17, 2009 at 13:57:20
You can't use a dynamic variable inside a previous one, i.e. !D!n!! is not allowed. You need to code !D%n%! and to do so you have to transfer the control to an internal subroutine replacing

echo !n!. %%~nc [%%~d] = !D!n!!


call :ECHO %%~nc %%~d

and coding the subroutine just at the bottom of your script

goto :EOF
echo %n%. %1 [%2] = !D%n%!
goto :EOF

where goto :EOF means "return to the calling sequence or to OS" if issued by the main.

About aligning results it is quite hard in batch scripting.

Report •

January 17, 2009 at 14:42:34
"i know i can write another for loop to do it, but i want to know if i can do it inside the original loop."

I think it's either another for loop or a call:

for %%z in (!n!) do echo !n!. %%~nc	[%%~d]	= !D%%z!

Although this loop can reside inside your loop, replacing your echo statement.

Report •

January 17, 2009 at 19:20:15
Formatting your columns, I have something(that you may recognise) that may do the trick for you, but you will need to make a couple of changes to your script.

You will need to dump the output to a text file with your echo statement modified:

for %%z in (!n!) do >>textfile.txt echo !n!. %%~nc@[%%~d]@= !D%%z!

Then this should clean it up:

set /a colcnt+=1
set max=0
>temp$$$$.txt type nul
for /f "usebackq tokens=1,* delims=@" %%a in ("textfile.txt") do (
set colx=%%a
set col=
call :len in te rn
if "%~1%~2%~3"=="intern" (
if defined colx (
set /a col+=1
set colx=!colx:~1!
goto len
) else (
goto :eof
if !col! gtr !max! (
set max=!col!
goto :loop
) else (
set /a pad=max-col
set colx=%%a
for /l %%d in (1,1,!pad!) do set colx=!colx! 
>>temp$$$$.txt echo !colx! %%b
move "%cd%\temp$$$$.txt" "%cd%\textfile.txt"
if %colcnt%==1 (
goto nextcol
) else (
goto :eof

If @ conflicts with your data find a delimiter that doesn't and put it in it's place.

Report •

Related Solutions

January 18, 2009 at 22:52:28
thanks ivo & judago for clarifiying that.

i was thinking to write another for loop outside the original loop which would mean more coding. didnt thought that i can use a call statement or for %%z in (!n!) loop.

judago, i try your script above and it only format the first column. and btw, what the purpose of using in te rn arguments on your call statement?

Report •

January 20, 2009 at 00:26:54
Did you use a space as a delimiter? Because the file is read after each column and padding is done with spaces it will read the padding as the delimiter. If you are struggling to find a character something within extended ansi may work, an accented letter like รน should work.

The in te rn is simply so I can embed a label call subroutine within the for loop. I need the call so I can use goto. As I called the label with the arguments "in te rn" the "if "%~1%~2%~3"=="intern"" is simply checking if the label was called or not. Any text should work the same.

Report •

January 20, 2009 at 00:28:18

I had multiple tabs and accidentally double post, don't ask me how I did it.....

Anyway please ignore this post.

Report •

January 20, 2009 at 05:47:29
ok, done, it format the column nicely now. thanks.

Report •

Ask Question