
|
process the list in reverse order in batch file
Hello I have a variable called "str" which contains list of values . All are comma separated . I need to process them in reverse order ..
How can I do it using for loop ???
I have code nippet
key=dependencies
for /f "tokens=1-2 delims==" %%a in (private\resolved.properties) do (
if %%a equ !key! (
set str = %%b
// here str will conatain list of values all are comma separated . How do I access those values in reverse order ...
)
)
)
|

|
Re: process the list in reverse order in batch file
You can always reverse the original string, and then process the reversed string front to back for what you need to accomplish. Sample code which will reverse a comma separated string is:
setlocal ENABLEDELAYEDEXPANSION
set foo=one,two,three,four
set revFoo=
set commaAdder=
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!foo!") do (set revFoo=%%p!commaAdder!!revFoo!&set foo=%%q&set commaAdder=,)
if not "!foo!" == "" goto :reverseLoop
echo foo is !foo!
echo revFoo is !revFoo!
In order to better debug your batch code, I recommend using Running Steps. The batch debugger from Stepping Software ( http://www.steppingsoftware.com). It has a code analyzer which provides you with errors and warnings even before you start executing, plus you can step through your code, watch variables, etc.
Good Luck
GISkier
|

|
Re: process the list in reverse order in batch file
Be careful with the URLs below - I don't think nabble.com it affiliated
with batchworld at all. They could be links with malware exploits.
On Sat, 8 Aug 2009 08:34:56 -0700 (PDT), giskier
< gonzaloi@...> wrote:
|

|
Re: process the list in reverse order in batch file
Cheers mate ;o)
2009/8/8 foxidrive < foxidrive@...>
>
>
>
> Be careful with the URLs below - I don't think nabble.com it affiliated
> with batchworld at all. They could be links with malware exploits.
>
>
> On Sat, 8 Aug 2009 08:34:56 -0700 (PDT), giskier
> < gonzaloi@... <gonzaloi%40steppingsoftware.com>> wrote:
>
> >
> >In order to better debug your batch code, I recommend using Running Steps.
>
> >The batch debugger from Stepping Software (
> http://www.steppingsoftware0.com).
> >It has a code analyzer which provides you with errors and warnings even
> >before you start executing, plus you can step through your code, watch
> >variables, etc.
> >
> >Good Luck
> >
> >GISkier
> >
> >--
> >View this message in context:
> http://www.nabble.com/process-the-list-in-reverse-order-in-batch-file-tp24846264p248779810.html> >Sent from the Batch World mailing list archive at Nabble.com.
>
>
[Non-text portions of this message have been removed]
|

|
Re: process the list in reverse order in batch file
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
In your code to process each line you add the following (which will take the first five letters of the variable and compare to zero. (including period)). As you can see, if the comparison matches it jumps over the processing and returns without doing anything.
:ProcessLine
if /I "%reversefoo:~0,5" == "zero." goto :EndProcessLine
...
do processing stuff here
...
:EndProcessLine
goto :EOF
The last line (goto :EOF) is the return statement assuming you got to ProcessLine using "call :ProcessLine ....etc...". You can also return using "exit /B X" where X is a number. If you use this approach, upon returning %errorlevel% will be set to X.
I hope this helps. Check the website for Stepping Software. You will find multiple interesting articles on interesting techniques to be applied in batch files. Plus the best batch file debugger: Running Steps 1.1.
Good Luck.
GISkier
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
giskier wrote:
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
The link is not malicious. I am simply informing you of a great tool for batch files in a forum for batch files. If you don't want to click, just do a search on "Running Steps Debugger". You will find the same information I am giving you here.
GISkier
Theodorik OBroin wrote:
Cheers mate ;o)
2009/8/8 foxidrive <foxidrive@yahoo.com.au>
>
>
>
> Be careful with the URLs below - I don't think nabble.com it affiliated
> with batchworld at all. They could be links with malware exploits.
>
>
> On Sat, 8 Aug 2009 08:34:56 -0700 (PDT), giskier
> <gonzaloi@steppingsoftware.com <gonzaloi%40steppingsoftware.com>> wrote:
>
> >
> >In order to better debug your batch code, I recommend using Running Steps.
>
> >The batch debugger from Stepping Software (
> http://www.steppingsoftware0.com).
> >It has a code analyzer which provides you with errors and warnings even
> >before you start executing, plus you can step through your code, watch
> >variables, etc.
> >
> >Good Luck
> >
> >GISkier
> >
> >--
> >View this message in context:
> http://www.nabble.com/process-the-list-in-reverse-order-in-batch-file-tp24846264p248779810.html> >Sent from the Batch World mailing list archive at Nabble.com.
>
>
[Non-text portions of this message have been removed]
|

|
Running Steps Debugger
On Sun, 9 Aug 2009 08:14:26 -0700 (PDT), giskier
< gonzaloi@...> wrote:
>
>The link is not malicious. I am simply informing you of a great tool for
>batch files in a forum for batch files. If you don't want to click, just do
>a search on "Running Steps Debugger". You will find the same information I
>am giving you here.
>
>GISkier
How are these posts getting to batchworld? Are you gating them yourself?
Do you have permission from Yahoo or the moderator?
I would welcome such an arrangement where the original posts arrive here as
well, but seeing *only* your followups and the text below made me very
suspcious.
>>> >View this message in context:
>>> http://www.nabble.com/process-the-list-in-reverse-order-in-batch-file-tp24846264p248779810.html>>> >Sent from the Batch World mailing list archive at Nabble.com.
This isn't Nabble.com and only your user name is on these posts...
|

|
Re: process the list in reverse order in batch file
Hi I have tried this , But it didnt work
I have tried simple example..
set str = zero.core
if /I "%str:~0,5" == "zero." echo HI
It didnt print anything..
What is the problem here ??
In your code to process each line you add the following (which will take the first five letters of the variable and compare to zero. (including period)). As you can see, if the comparison matches it jumps over the processing and returns without doing anything.
:ProcessLine
if /I "%reversefoo:~0,5" == "zero." goto :EndProcessLine
...
do processing stuff here
...
:EndProcessLine
goto :EOF
The last line (goto :EOF) is the return statement assuming you got to ProcessLine using "call :ProcessLine ....etc...". You can also return using "exit /B X" where X is a number. If you use this approach, upon returning %errorlevel% will be set to X.
I hope this helps. Check the website for Stepping Software. You will find multiple interesting articles on interesting techniques to be applied in batch files. Plus the best batch file debugger: Running Steps 1.1.
Good Luck.
GISkier
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
giskier wrote:
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
I missed the closing quote:
%str:~0,5%
Good Luck
GISkier
Hi I have tried this , But it didnt work
I have tried simple example..
set str = zero.core
if /I "%str:~0,5" == "zero." echo HI
It didnt print anything..
What is the problem here ??
In your code to process each line you add the following (which will take the first five letters of the variable and compare to zero. (including period)). As you can see, if the comparison matches it jumps over the processing and returns without doing anything.
:ProcessLine
if /I "%reversefoo:~0,5" == "zero." goto :EndProcessLine
...
do processing stuff here
...
:EndProcessLine
goto :EOF
The last line (goto :EOF) is the return statement assuming you got to ProcessLine using "call :ProcessLine ....etc...". You can also return using "exit /B X" where X is a number. If you use this approach, upon returning %errorlevel% will be set to X.
I hope this helps. Check the website for Stepping Software. You will find multiple interesting articles on interesting techniques to be applied in batch files. Plus the best batch file debugger: Running Steps 1.1.
Good Luck.
GISkier
Raagu wrote:
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
giskier wrote:
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
ya it worked fine :)
But in my code o am gettring str as a pameter from other piece of code..
Here is my snippet
:processEach
for /F "tokens=1,* delims=," %%r in ("!revDepends!") do (set revDepends=%%s& call :compile %%r)
if not "!revDepends!" == "%1" goto :processEach
:compile
echo %1
if /I "%(%1):~0,5%" == "zero." echo HI
How Do I put %1 (that conatns a single value , may be 'zero.core' ) in if condition...
I missed the closing quote:
%str:~0,5%
Good Luck
GISkier
Hi I have tried this , But it didnt work
I have tried simple example..
set str = zero.core
if /I "%str:~0,5" == "zero." echo HI
It didnt print anything..
What is the problem here ??
In your code to process each line you add the following (which will take the first five letters of the variable and compare to zero. (including period)). As you can see, if the comparison matches it jumps over the processing and returns without doing anything.
:ProcessLine
if /I "%reversefoo:~0,5" == "zero." goto :EndProcessLine
...
do processing stuff here
...
:EndProcessLine
goto :EOF
The last line (goto :EOF) is the return statement assuming you got to ProcessLine using "call :ProcessLine ....etc...". You can also return using "exit /B X" where X is a number. If you use this approach, upon returning %errorlevel% will be set to X.
I hope this helps. Check the website for Stepping Software. You will find multiple interesting articles on interesting techniques to be applied in batch files. Plus the best batch file debugger: Running Steps 1.1.
Good Luck.
GISkier
Raagu wrote:
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
giskier wrote:
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
You can assign the parameter to a temporary variable and use the variable:
:compile
set HelpVar=%1
if /I "%HelpVar:~0,5%" == "zero." ...etc...
GISkier
ya it worked fine :)
But in my code o am gettring str as a pameter from other piece of code..
Here is my snippet
:processEach
for /F "tokens=1,* delims=," %%r in ("!revDepends!") do (set revDepends=%%s& call :compile %%r)
if not "!revDepends!" == "%1" goto :processEach
:compile
echo %1
if /I "%(%1):~0,5%" == "zero." echo HI
How Do I put %1 (that conatns a single value , may be 'zero.core' ) in if condition...
I missed the closing quote:
%str:~0,5%
Good Luck
GISkier
Hi I have tried this , But it didnt work
I have tried simple example..
set str = zero.core
if /I "%str:~0,5" == "zero." echo HI
It didnt print anything..
What is the problem here ??
giskier wrote:
In your code to process each line you add the following (which will take the first five letters of the variable and compare to zero. (including period)). As you can see, if the comparison matches it jumps over the processing and returns without doing anything.
:ProcessLine
if /I "%reversefoo:~0,5" == "zero." goto :EndProcessLine
...
do processing stuff here
...
:EndProcessLine
goto :EOF
The last line (goto :EOF) is the return statement assuming you got to ProcessLine using "call :ProcessLine ....etc...". You can also return using "exit /B X" where X is a number. If you use this approach, upon returning %errorlevel% will be set to X.
I hope this helps. Check the website for Stepping Software. You will find multiple interesting articles on interesting techniques to be applied in batch files. Plus the best batch file debugger: Running Steps 1.1.
Good Luck.
GISkier
Raagu wrote:
Thanks giskier... Your code worked fine.. [:)]
NOw I have one more thing to resolve.
I have a string "reverseFoo" which contains list of values , all are comma separarted..
I have to process each one of them..
I have to check each value .. If it starts with "zero." then I should discard them , otherwise process them (simply echo them)..
How Do I put it in for loop ???
Please do reply
giskier wrote:
You can use a command in the form
for /f %p in (myfile.txt) do call :ProcessLine %p
goto :EOF
:ProcessLine
rem Add code here to reverse the line (which is %* since its used as parameter in the call), and do all the processing you need here.
Good Luck
GISkier
PS. If you don't trust the llink I sent, which is not malicious, just do a web search for "Running Steps debugger" and you will find this tool. It will be very helpful.
Raagu wrote:
Hello Thanks for wonderful reply. It worked fine..
But I have to execute this for loop multiple times and d number of times determined by number files in a text file..
Here is the my snippet
key=dependencies
for /f %%f in (myBuild\modules.txt) do (
set revDepends=
set commaAdder=
for /f "tokens=1-2 delims==" %%a in (%%f\.zero\private\resolved.properties) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!
:reverseLoop
for /F "tokens=1,* delims=," %%p in ("!str!") do (set revDepends=%%p!commaAdder!!revDepends!&set str=%%q&set commaAdder=,)
if not "!str!" == "" goto :reverseLoop
echo !revDepends!
)
I wrote like this....
Here 'str' contains list of strings separated by comma after first inner for loop
myBuild/modules.properties contains two lines as below
demo
simple
I want to execute your for loop twice for these two lines..
But In my code, the outer for loop executing only once..
can u tel what is happening.
|

|
Re: process the list in reverse order in batch file
oh ok Thanks...
Now
I have a bat script which conatins a for loop
for ******** do call :LABEL1
:LABEL1
// here my code
:LABEL2
//mycode
goto :LABEL3
:LABEL3
//bla bla
:LABEL4
Here After :LABEL3 completes it should go to for loop again for next iteration..
That is I want to execute first three LABELS for each iteration of for loop.. after finished that LABEL4 should be executed..
when one condition satisfies within for loop, then it should go to LABEL4 and execute from there..
Now In my code after :LABEL3 it is coming to LABEL4 .. I dont want that..
ANd after :LABEL3 i cant put goto for loop because , In for loop i am accessing a file,, So it will start from the beginning..
What do I Do for this ??
please reply..
|

|
Re: process the list in reverse order in batch file
There are 2 ways to return from a call
1) goto :EOF
2) exit /B X
In way 2, X is a number, and the errorlevel variable will be set to X.
Example
for %p in (one two three) do call :showParam
:ShowParam
echo %1
goto :EOF
Good Luck
GISkier
Raagu wrote:
oh ok Thanks...
Now
I have a bat script which conatins a for loop
for ******** do call :LABEL1
:LABEL1
// here my code
:LABEL2
//mycode
goto :LABEL3
:LABEL3
//bla bla
:LABEL4
Here After :LABEL3 completes it should go to for loop again for next iteration..
That is I want to execute first three LABELS for each iteration of for loop.. after finished that LABEL4 should be executed..
when one condition satisfies within for loop, then it should go to LABEL4 and execute from there..
Now In my code after :LABEL3 it is coming to LABEL4 .. I dont want that..
ANd after :LABEL3 i cant put goto for loop because , In for loop i am accessing a file,, So it will start from the beginning..
What do I Do for this ??
please reply..
|