SRL Archive - Other Language Help and Tutorials

[BATCH] file scripting |for /f in do for in echo %%%%a (not working)


wantonman

2014-08-22 22:15:04

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1-20 delims=m" %%a in (mdeeekkkke54m45y54ym54y)do (
for %%A in (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) do (

echo %%%%A

))
i want it to be equal to %%a %%b %%c and so on
however it doesnt recognize that im specifying the first %%a variable
it just sees the second %%A value and trims the % signs…

much help appreciated…

Brandon

2014-08-22 23:03:16

From: wantonman
…



Variables in batch files aren't case sensitive afaik.. This means that %%a and %%A are the exact same variable and so.. the first one's data is overwritten by the second in the inner loop.

Use another variable name..

wantonman

2014-08-22 23:28:46

thanks theres no definate answere i switches %%A to %%z i still get same results and to as my unknowingness this is something tricky…
i read that you can use ascii A-Z and a-z and 0-9 for variables in for loops

http://judago.webs.com/variablecatches.htm

this is the best but it still doest say anything about a for variable used with another for variable…

Brandon

2014-08-23 00:15:15

From: wantonman
thanks theres no definate answere i switches %%A to %%z i still get same results and to as my unknowingness this is something tricky…
i read that you can use ascii A-Z and a-z and 0-9 for variables in for loops

http://judago.webs.com/variablecatches.htm

this is the best but it still doest say anything about a for variable used with another for variable…



It's confusing exactly what you want to do..


Try this (which is now syntactically correct due to the extra quotes).. Without them, it looks for a file called "mdeeekkkke54m45y54ym54y"..


@echo off
setlocal enabledelayedexpansion

for /f "tokens=1-20 delims=m" %%a in ("mdeeekkkke54m45y54ym54y") do (

echo %%a
echo %%b
echo %%c

for %%d in (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) do (

echo %%%%%%d

)

)


I ran it using:

cmd.exe /c foo.bat

and the results are:


deeekkkke54
45y54y
54y

%%a
%%b
%%c
%%d
%%e
%%f
%%g
%%h
%%i
%%j
%%k
%%l
%%m
%%n
%%o
%%p
%%q
%%r
%%s
%%t


If that's not what you want, I'm lost..

Do you want the equivalent of the following? Or is the above correct already?


for (char c : "mdeeekkkke54m45y54ym54y".split("m")) { //might not be syntactically correct but meh..

for (char d : new char['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ....]) {
if (d == c)
System.out.println(c);
}

}


// OR??.. which makes not very much sense but sure..??

for (char c : "mdeeekkkke54m45y54ym54y".split("m")) { //might not be syntactically correct but meh..

//C will get re-assigned to whatever value is in the array..
for (c : new char['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ....]) {
System.out.println(c);
}

}

Describe what you need or even write it in any other language and we can probably help/translate or figure it out.

wantonman

2014-08-23 01:00:19

Heres a more simplifyed version

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1-20 delims=m" %%a in ("mdeeekkkke54m45y54ym54y")do (
echo %%a
echo %%b
echo %%c

for %%z in (a,b,c) do (
echo %%%%%%z
))


HERE IS THE OUTPUT

deeekkkke54
45y54y
54y
%%a
%%b
%%c


on the output im trying to get something like

deeekkkke54
45y54y
54y
%%a <–this should equal "deeekkkke54"
%%b <–this should equal "45y54y"
%%c <–this should equal "54y"

but the variables dont actually show theres any value in it at all as you can see
man…

Brandon

2014-08-23 01:26:29

From: wantonman

but the variables dont actually show theres any value in it at all as you can see
man…



That's because you're not de-referencing the variables at all.. You're interpreting each one in the for-loop as a brand new character.

See below (I know it's not "bash/shell" code.. I just used that tag for the highlighting):


@echo off
setlocal enabledelayedexpansion

for /f "tokens=1-20 delims=m" %%a in ("mdeeekkkke54m45y54ym54y")do (
echo %%a
echo %%b
echo %%c

for %%z in (%%a, %%b, %%c) do (
echo %%z
)
)

The result would be exactly what you described.. I'm a bit curious as to why you need it to do this since you already know that %%a holds the first node and so on.. But hopefully the above is what you meant.

wantonman

2014-08-23 04:21:08

your solution works great for what i am trying to acheive

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=x" %%a in ("xthi1xthisxthf3")do (
for %%d in (%%a,%%b,%%c) do (
set x0=%%d
if "!x0:~0,1!"=="t" if "!x0:~1,1!"=="h" if "!x0:~2,1!"=="i" if "!x0:~3,1!"=="s" echo %%d
))


i really do not have a reason for this anymore since your solution did get me what i was trying to do but
for whatever way is it possible to merge for variables in this type of way…

heres a conceptual idea


@echo off
setlocal enabledelayedexpansion
set x0=x
set x1=2
set x2=this is the example
echo !%x0%%x1%!

nottice the merging of the variables
echo !%x0%%x1%!

just in that sense can this work with using a for loop as well?

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=x" %%a in ("xthi1xthisxthf3")do (
for %%d in (a,b,c)do (
echo %%!%%d!
))

the idea is that %%!%%d! would actually mean %%a %%b %%c in the loop…
of course you dont use the !! for a for loop variable but its to show what the idea shoud be to do
… when it all expands it does something but it seems as if this is not possible in this example…

Brandon

2014-08-23 11:09:38

From: wantonman

… when it all expands it does something but it seems as if this is not possible in this example…



Use recursion or jumping?


@echo off
setlocal enabledelayedexpansion

set str="mdeeekkkke54m45y54ym54y"
call :start "%str%"
goto :end


:start
setlocal
set list=%1
set list=%list:"=%

for /f "tokens=1* delims=m" %%a in ("%list%") do (

::Do whatver you want with %%a here.. I'll echo it below..

echo %%a


call :start "%%b"

)
endlocal
:end


It will print:


deeekkkke54
45y54y
54y

wantonman

2014-09-10 19:36:09

i havent tried recursion or jumping but nontheless the batch file is up and running but the /F loop doesnt read everyline in the text file like it is documented to do so.
here is my code
ive included an example text file attachment
its supposed to go threw the entire text file but it stops not even half way…

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1-26 DELIMS=/" %%A IN (HTMLCODE.TXT) DO (
FOR %%? IN (%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z) DO (
ECHO %%?
)
)
PAUSE

ive read somewhere that it seems that whenever the /F for loop reads a text file that has maximum length lines theres some things that dont work

Home

2014-09-11 02:54:41

From: wantonman
i havent tried recursion or jumping but nontheless the batch file is up and running but the /F loop doesnt read everyline in the text file like it is documented to do so.
here is my code
ive included an example text file attachment
its supposed to go threw the entire text file but it stops not even half way…

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1-26 DELIMS=/" %%A IN (HTMLCODE.TXT) DO (
FOR %%? IN (%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z) DO (
ECHO %%?
)
)
PAUSE

ive read somewhere that it seems that whenever the /F for loop reads a text file that has maximum length lines theres some things that dont work

What about using Powershell?

-Home

wantonman

2014-09-14 22:53:03

YEAH powershell id still not know
here are some BATCH examples

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1 DELIMS=" %%? IN ('DEBUG.EXE TEXT ^< DEBUG') DO (
SET X0=%%?
SET X0=!X0:~-16!
FOR /L %%! IN (0,1,15) DO (
IF !X1!==5 ECHO GOLD
IF !X1!==5 ( IF !X0!==/ ( SET X1=6 ) ELSE ( SET X1=0 ) )
IF !X1!==4 ( IF !X0!==d ( SET X1=5 ) ELSE ( SET X1=0 ) )
IF !X1!==3 ( IF !X0!==r ( SET X1=4 ) ELSE ( SET X1=0 ) )
IF !X1!==2 ( IF !X0!==a ( SET X1=3 ) ELSE ( SET X1=0 ) )
IF !X1!==1 ( IF !X0!==c ( SET X1=2 ) ELSE ( SET X1=0 ) )
IF !X1!==0 ( IF !X0!==/ ( SET X1=1 ) ELSE ( SET X1=0 ) ) ) )
THIS one just fails ans does not find it


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1 DELIMS=" %%? IN ('DEBUG.EXE TEXT ^< DEBUG') DO (
SET X0=%%?
SET X0=!X0:~-16!
FOR /L %%! IN (0,1,15) DO (
FOR %%@ IN ( / c a r d / ) DO (
IF "!X0:~%%!,1!" == "%%@" ( SET X1=!X1!%%@ )
ECHO !X1! | FIND "/card/" && SET X1= && ECHO GOLD ) ) )
THIS one just fails even more

Home

2014-09-15 16:27:23

Not sure what you are after, but heres how you can split in PowerShell
$Delim = '/'

$File = Get-Content C:\Users\Home\htmlcode.txt

$StringArr = $File -split $Delim

$StringArr

~Home

wantonman

2014-09-18 22:00:59

OKAY I GOT IT

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET X1=0
FOR /F "TOKENS=1 DELIMS=" %%? IN ('DEBUG.EXE TEXT ^< DEBUG') DO (
SET X0=%%?
SET X0=!X0:~-16!
FOR /L %%! IN (0,1,15) DO (
IF !X1!==6 SET X2=!X2!!X0:~%%!,1!&IF "!X0:~%%!,1!"=="/" ECHO !X2!&SET X1=0&SET X2=
IF !X1!==5 IF "!X0:~%%!,1!"=="/" (SET X1=6)ELSE (SET X1=0)
IF !X1!==4 IF "!X0:~%%!,1!"=="d" (SET X1=5)ELSE (SET X1=0)
IF !X1!==3 IF "!X0:~%%!,1!"=="r" (SET X1=4)ELSE (SET X1=0)
IF !X1!==2 IF "!X0:~%%!,1!"=="a" (SET X1=3)ELSE (SET X1=0)
IF !X1!==1 IF "!X0:~%%!,1!"=="c" (SET X1=2)ELSE (SET X1=0)
IF !X1!==0 IF "!X0:~%%!,1!"=="/" (SET X1=1)ELSE (SET X1=0)
) )
and can redirect to textfile with the >> as well
the DEBUG file looks as so

D 0100 L A4A3
Q

-
hex dump as necissary for the BATCH line limit i think 1000 characters
the debug.exe dumps the hex and aside the contents in a 16 byte fashion
so im able to capture each line in the FOR loop and the debug.exe program
is scriptable so that makes it well for redirection and output in BATCH

the TEXT file in the debug.exe command in the FOR loop is just a plain file html file

this is working and will work for any asynchroneous files you may need sorting
just swich around the key search terms and variables as needed as long as you have the
debug.exe program unless your file isnt exeeding the line feed then you can just replace
the hex dump with the actual file and swich the substrings

was stumped in previous example but its working

will print anything after the found phrase and untill the character specifyed