Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
ldoodle
- Posts: 18
- Joined: 26 Sep 2015 04:01
#1
Post
by ldoodle » 06 Oct 2015 04:22
Hey,
With a loop like this, is it possible to know which file or variable it's currently looping through?
Code: Select all
for %%x in (%args% %defargsfile% %userargs%) do (
for /f "tokens=1,* delims=-:" %%g in (%%x) do (
call :setarg "%%g" "%%~h"
echo.Current variable is: xxx
)
)
So know if it's currently working through %args%, %defargsfile% or %userargs%
Thanks
-
TheHunterManX
- Posts: 54
- Joined: 14 Aug 2015 05:59
#2
Post
by TheHunterManX » 06 Oct 2015 04:49
Tell me first, are you trying to echo the current variable it is on (%args%,%defargsfile% and %userargs%), or trying to echo the value of the variable?
EDIT:
Nevermind, I understand this and will comment shortly the answer.
Please, tell me what this is for so I can help.
-
ldoodle
- Posts: 18
- Joined: 26 Sep 2015 04:01
#3
Post
by ldoodle » 06 Oct 2015 05:17
Hi,
The current variable it is on.
Basically I need to do something like this:
Code: Select all
for %%x in (%args% %defargsfile% %userargs%) do (
set currentvar=XXX
for /f "tokens=1,* delims=-:" %%g in (%%x) do (
if <currentvar> equ %args% (
call :setarg "define" "%%g" "%%~h"
) else (
call :setarg nul "%%g" "%%~h"
)
)
)
-
TheHunterManX
- Posts: 54
- Joined: 14 Aug 2015 05:59
#4
Post
by TheHunterManX » 06 Oct 2015 06:23
Different type of code, but it gets the job done.
Edit main if you want to add commands.
Also "" count as 1 command so even with 2 words in quotes they count as 1.
Code: Select all
@echo off
REM Setting the variables for each command...
set args=%1
set defargsfile=%2
set userargs=%3
set total=0
set argsn=0
set dfn=0
set uarn=0
echo args...
REM for if the command is set calls the type of command
for %%x in (%args%) do call :ar
echo defargsfile...
REM for if the command is set calls the type of command
for %%x in (%defargsfile%) do call :df
echo userargs...
REM for if the command is set calls the type of command
for %%x in (%userargs%) do call :uar
REM After all is done tells total of all commands and if they have been used...
echo Total commands: %total%
echo for args:%argsn%
echo for defargsfile:%dfn%
echo for userargs:%uarn%
REM Exits...
exit /b 0
:ar
REM If you have an arguement...
set /a argsn=1
echo Found 1...
set currentvar=%args%
set currentcmd=args
Calls main function to process all...
call :main
goto :EOF
:df
REM If you have an arguement...
set /a dfn=1
echo Found 1...
set currentvar=%args%
set currentcmd=df
Calls main function to process all...
call :main
goto :EOF
:uar
REM If you have an arguement...
set /a uarn=%uarn%+1
echo Found 1...
set currentvar=%args%
set currentcmd=uargs
Calls main function to process all...
call :main
goto :EOF
:main
REM main processing of all stuff.
REM REM FOR LATER
set /a total=%total%+1
REM for /f "tokens=1,* delims=-:" %%g in (%%x) do (
REM if %currentcmd% equ args (
REM if %currentvar% EQU -s (
REM call :setarg "%%g" "%%~h"
REM ) else (
REM call :setarg nul "%%g" "%%~h"
REM )
REM )
REM )
goto :EOF
Tell me if it is not what you want...
Last edited by
TheHunterManX on 06 Oct 2015 11:50, edited 2 times in total.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 06 Oct 2015 07:46
You may want to explain what your intended goal is and what the overall batch file is doing. I am betting you are only showing us a code segment of a much larger batch file. Hard to help when we don't know the big picture. The only thing we can assume is this has to do with your previous batch file.
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#6
Post
by Aacini » 06 Oct 2015 08:55
Code: Select all
for /f "tokens=1,* delims=-:" %%g in (%args%) do (
call :setarg "define" "%%g" "%%~h"
)
for %%x in (%defargsfile% %userargs%) do (
for /f "tokens=1,* delims=-:" %%g in (%%x) do (
call :setarg nul "%%g" "%%~h"
)
)
Antonio
-
ldoodle
- Posts: 18
- Joined: 26 Sep 2015 04:01
#7
Post
by ldoodle » 06 Oct 2015 09:28
Thanks Antonio, that's how I originally had it (in fact is was 3 separate for loops for the different variables.
I was convinced you could do it on one, and I think I've worked out a way. Basically set a variable which contains the other 3 variable names, and do an 'outer' loop on the new variable:
Code: Select all
set arglist="args" "argsfile" "userargs"
for %%z in (%arglist%) do (
set listname=%%~z
set list=!%%~z!
for %%x in (!list!) do (
for /f "tokens=1,* delims=-:" %%g in (%%x) do (
set _arg=%%g
set _val=%%h
if [!listname!] neq [args] if not defined !_arg! goto usage
if [!listname!] equ [userargs] (
if [!_arg!] neq [defaultargs] if [!_arg!] neq [resetargs] echo.-!_arg!:!_val!>>%tempargsfile%
)
set "!_arg!=!_val!"
)
)
)
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#8
Post
by Aacini » 06 Oct 2015 10:17
ldoodle wrote:Thanks Antonio, that's how I originally had it (in fact is was 3 separate for loops for the different variables.
I was convinced you could do it on one, and I think I've worked out a way. Basically set a variable which contains the other 3 variable names, and do an 'outer' loop on the new variable:
Although it is
possible to do it that way, the question is
why do it that way? The resulting code is much more complex than repeat a similar code section two or more times...
Anyway, this is a way to do it:
Code: Select all
set arglist="args" "argsfile" "userargs"
set arg=0
for %%z in (%arglist%) do (
set /A arg+=1
set list=!%%~z!
for %%x in (!list!) do (
for /f "tokens=1,* delims=-:" %%g in (%%x) do (
set _arg=%%g
set _val=%%h
if !arg! neq 1 if not defined !_arg! goto usage
if !arg! equ 3 (
if "!_arg!" neq "defaultargs" if "!_arg!" neq "resetargs" echo.-!_arg!:!_val!>>%tempargsfile%
)
set "!_arg!=!_val!"
)
)
)
Antonio
-
ldoodle
- Posts: 18
- Joined: 26 Sep 2015 04:01
#10
Post
by ldoodle » 06 Oct 2015 12:00
Aacini wrote:Although it is possible to do it that way, the question is why do it that way?
The code I posted was just a snippet. In reality it's many more lines, so I'd have to repeat the same code in each for...loop.
I suppose I could put the duplicate code in a function, if that's the right word in Windows Batch, and call it from each for...loop.