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.

zephirus
  • 391
  • 1
  • 5
  • 23

1 Answers1

6

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
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 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?](https://stackoverflow.com/help/how-to-ask). [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). 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
  • You're completely right. When I posted the question I used my simplified code that I was using to find a solution. I edited the question. Again, thanks for your answer, it helped a lot! – zephirus Nov 27 '18 at 19:49
  • Thanks for your comprehensive answer. I've solved my problem. – zephirus Nov 28 '18 at 23:56