How can I iterate in a list in batch?












1















I have two strings:



jobID: w,x,y,z
Test: A,B,C,D


I'm iterating the second string (after separating it by comma) and print both the value of that generated list and the one from the first string.



My code until now is this:



@echo off
setlocal EnableDelayedExpansion

set num=-1
for %%a in ("%jobID:,=" "%") do (
set /A num+=1
set elem[!num!]=%%a
)

set i=-1
for %%a in ("%Test:,=" "%") do (
set /A i=i+1
echo file name is %%a and first list element is !%elem[!%i%!]%!
)


What I'm expecting is this:



file name is A and first list element is w
file name is B and first list element is x
file name is C and first list element is y
file name is D and first list element is z


What I get:



file name is "A" and first list element is "0"
file name is "B" and first list element is "1"
file name is "C" and first list element is "2"
file name is "D" and first list element is "3"


EDIT: I need to get the values without the double quotes.










share|improve this question

























  • And what is what you are getting?

    – double-beep
    Nov 27 '18 at 19:02











  • You're right. I edited the question.

    – zephirus
    Nov 27 '18 at 19:04











  • By the way, my struggle is how to get the value of a list element.

    – zephirus
    Nov 27 '18 at 19:11
















1















I have two strings:



jobID: w,x,y,z
Test: A,B,C,D


I'm iterating the second string (after separating it by comma) and print both the value of that generated list and the one from the first string.



My code until now is this:



@echo off
setlocal EnableDelayedExpansion

set num=-1
for %%a in ("%jobID:,=" "%") do (
set /A num+=1
set elem[!num!]=%%a
)

set i=-1
for %%a in ("%Test:,=" "%") do (
set /A i=i+1
echo file name is %%a and first list element is !%elem[!%i%!]%!
)


What I'm expecting is this:



file name is A and first list element is w
file name is B and first list element is x
file name is C and first list element is y
file name is D and first list element is z


What I get:



file name is "A" and first list element is "0"
file name is "B" and first list element is "1"
file name is "C" and first list element is "2"
file name is "D" and first list element is "3"


EDIT: I need to get the values without the double quotes.










share|improve this question

























  • And what is what you are getting?

    – double-beep
    Nov 27 '18 at 19:02











  • You're right. I edited the question.

    – zephirus
    Nov 27 '18 at 19:04











  • By the way, my struggle is how to get the value of a list element.

    – zephirus
    Nov 27 '18 at 19:11














1












1








1








I have two strings:



jobID: w,x,y,z
Test: A,B,C,D


I'm iterating the second string (after separating it by comma) and print both the value of that generated list and the one from the first string.



My code until now is this:



@echo off
setlocal EnableDelayedExpansion

set num=-1
for %%a in ("%jobID:,=" "%") do (
set /A num+=1
set elem[!num!]=%%a
)

set i=-1
for %%a in ("%Test:,=" "%") do (
set /A i=i+1
echo file name is %%a and first list element is !%elem[!%i%!]%!
)


What I'm expecting is this:



file name is A and first list element is w
file name is B and first list element is x
file name is C and first list element is y
file name is D and first list element is z


What I get:



file name is "A" and first list element is "0"
file name is "B" and first list element is "1"
file name is "C" and first list element is "2"
file name is "D" and first list element is "3"


EDIT: I need to get the values without the double quotes.










share|improve this question
















I have two strings:



jobID: w,x,y,z
Test: A,B,C,D


I'm iterating the second string (after separating it by comma) and print both the value of that generated list and the one from the first string.



My code until now is this:



@echo off
setlocal EnableDelayedExpansion

set num=-1
for %%a in ("%jobID:,=" "%") do (
set /A num+=1
set elem[!num!]=%%a
)

set i=-1
for %%a in ("%Test:,=" "%") do (
set /A i=i+1
echo file name is %%a and first list element is !%elem[!%i%!]%!
)


What I'm expecting is this:



file name is A and first list element is w
file name is B and first list element is x
file name is C and first list element is y
file name is D and first list element is z


What I get:



file name is "A" and first list element is "0"
file name is "B" and first list element is "1"
file name is "C" and first list element is "2"
file name is "D" and first list element is "3"


EDIT: I need to get the values without the double quotes.







batch-file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 19:47







zephirus

















asked Nov 27 '18 at 18:46









zephiruszephirus

144215




144215













  • And what is what you are getting?

    – double-beep
    Nov 27 '18 at 19:02











  • You're right. I edited the question.

    – zephirus
    Nov 27 '18 at 19:04











  • By the way, my struggle is how to get the value of a list element.

    – zephirus
    Nov 27 '18 at 19:11



















  • And what is what you are getting?

    – double-beep
    Nov 27 '18 at 19:02











  • You're right. I edited the question.

    – zephirus
    Nov 27 '18 at 19:04











  • By the way, my struggle is how to get the value of a list element.

    – zephirus
    Nov 27 '18 at 19:11

















And what is what you are getting?

– double-beep
Nov 27 '18 at 19:02





And what is what you are getting?

– double-beep
Nov 27 '18 at 19:02













You're right. I edited the question.

– zephirus
Nov 27 '18 at 19:04





You're right. I edited the question.

– zephirus
Nov 27 '18 at 19:04













By the way, my struggle is how to get the value of a list element.

– zephirus
Nov 27 '18 at 19:11





By the way, my struggle is how to get the value of a list element.

– zephirus
Nov 27 '18 at 19:11












1 Answer
1






active

oldest

votes


















5














You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.



Change your echo command to this.



call echo file name is %%a and first list element is %%elem[!i!]%%


This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script



Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.



You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line





share|improve this answer


























  • Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

    – zephirus
    Nov 27 '18 at 19:33











  • @zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

    – Squashman
    Nov 27 '18 at 19:35













  • Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

    – zephirus
    Nov 27 '18 at 19:39











  • @zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

    – Squashman
    Nov 27 '18 at 19:41








  • 1





    @zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

    – Squashman
    Nov 27 '18 at 19:46













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506241%2fhow-can-i-iterate-in-a-list-in-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.



Change your echo command to this.



call echo file name is %%a and first list element is %%elem[!i!]%%


This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script



Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.



You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line





share|improve this answer


























  • Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

    – zephirus
    Nov 27 '18 at 19:33











  • @zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

    – Squashman
    Nov 27 '18 at 19:35













  • Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

    – zephirus
    Nov 27 '18 at 19:39











  • @zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

    – Squashman
    Nov 27 '18 at 19:41








  • 1





    @zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

    – Squashman
    Nov 27 '18 at 19:46


















5














You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.



Change your echo command to this.



call echo file name is %%a and first list element is %%elem[!i!]%%


This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script



Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.



You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line





share|improve this answer


























  • Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

    – zephirus
    Nov 27 '18 at 19:33











  • @zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

    – Squashman
    Nov 27 '18 at 19:35













  • Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

    – zephirus
    Nov 27 '18 at 19:39











  • @zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

    – Squashman
    Nov 27 '18 at 19:41








  • 1





    @zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

    – Squashman
    Nov 27 '18 at 19:46
















5












5








5







You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.



Change your echo command to this.



call echo file name is %%a and first list element is %%elem[!i!]%%


This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script



Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.



You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line





share|improve this answer















You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.



Change your echo command to this.



call echo file name is %%a and first list element is %%elem[!i!]%%


This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script



Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.



You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 19:58

























answered Nov 27 '18 at 19:19









SquashmanSquashman

8,92431933




8,92431933













  • Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

    – zephirus
    Nov 27 '18 at 19:33











  • @zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

    – Squashman
    Nov 27 '18 at 19:35













  • Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

    – zephirus
    Nov 27 '18 at 19:39











  • @zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

    – Squashman
    Nov 27 '18 at 19:41








  • 1





    @zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

    – Squashman
    Nov 27 '18 at 19:46





















  • Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

    – zephirus
    Nov 27 '18 at 19:33











  • @zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

    – Squashman
    Nov 27 '18 at 19:35













  • Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

    – zephirus
    Nov 27 '18 at 19:39











  • @zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

    – Squashman
    Nov 27 '18 at 19:41








  • 1





    @zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

    – Squashman
    Nov 27 '18 at 19:46



















Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

– zephirus
Nov 27 '18 at 19:33





Thanks Squashman, that solved it. The only issue I still have is that the variables are inside double quotes. I need to concatenate them to create a file path so I can copy some files. How can I get rid of the double quotes?

– zephirus
Nov 27 '18 at 19:33













@zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

– Squashman
Nov 27 '18 at 19:35







@zephirus, I do not see any double quotes in your code. You will need to edit your question with an example of that. As a best practice I would advise you to not assign double quotes as a value to your variables. They can always be used when needed, especially with file paths.

– Squashman
Nov 27 '18 at 19:35















Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

– zephirus
Nov 27 '18 at 19:39





Sorry. I forgot to say that the for in my question was a simplification and it was enough to explain the issue I had. In fact, in my code I have a comma separated string so the code is like this: for %%a in ("%var:,=" "%") do ( where the %var is A,B,C,D. So the double quotes are a result of the comma split.

– zephirus
Nov 27 '18 at 19:39













@zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

– Squashman
Nov 27 '18 at 19:41







@zephirus, going forward, please read these two links before you post anymore questions. How to ask a good question?. How to create a Minimal, Complete, and Verifiable example. As I said in my previous comment, EDIT your question with updated examples.

– Squashman
Nov 27 '18 at 19:41






1




1





@zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

– Squashman
Nov 27 '18 at 19:46







@zephirus I would also advise you to read the the help file for the FOR command. Open up a command prompt and type: for /?. I bet you will see something about removing quotes from the for variable. It is the very last section of the help file.

– Squashman
Nov 27 '18 at 19:46






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506241%2fhow-can-i-iterate-in-a-list-in-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks