1.

Solve : Exclude space as delimiter in a list?

Answer»

Eventually this BATCH file will expand to allow selection of creation of a range of subdirectories, however I've got to get the code correct first.

In the current directory I want to create the following subdirectories:
2010-11 FY
2011-12 FY
2012-13 FY
2013-14 FY
2014-15 FY
2015-16 FY
2016-17 FY
2017-18 FY
2018-19 FY
2019-20 FY

as they contain spaces, I need to exclude a space as a delimiter.

For /F uses file contents, however the contents are contained within a variable not a file.

If I replace the space in each element with an underscore, a simple for loop will work, but I want a space.

Code: [Select]echo off

:Main0
set DirNames=2010-11 FY,2011-12 FY,2012-13 FY,2013-14 FY,2014-15 FY,2015-16 FY,2016-17 FY,2017-18 FY,2018-19 FY,2019-20 FY
FOR /F "delims=," %%I in (%DirNames%) do echo mkdir %%I

:EndPause
pause

:End

The above code produces the error "The system cannot find the file 2010-11."

Thanks,

Bazzao
1. Use quotes around list items.

2. Use simple FOR loop  (no /F).

echo off

:Main0
set DirNames="2010-11 FY","2011-12 FY","2012-13 FY","2013-14 FY","2014-15 FY","2015-16 FY","2016-17 FY","2017-18 FY","2018-19 FY","2019-20 FY"
FOR %%I in (%DirNames%) do echo mkdir %%I

:EndPause
pause

:End

Output is:

mkdir "2010-11 FY"
mkdir "2011-12 FY"
mkdir "2012-13 FY"
mkdir "2013-14 FY"
mkdir "2014-15 FY"
mkdir "2015-16 FY"
mkdir "2016-17 FY"
mkdir "2017-18 FY"
mkdir "2018-19 FY"
mkdir "2019-20 FY"



Quote from: Salmon Trout on August 10, 2019, 06:17:24 AM

1. Use quotes around list items.

2. Use simple FOR loop  (no /F).

Well that worked. Many thanks Salmon Trout.
He Da MAN... A technical explanation, in case anyone is interested (might HELP some readers)?

In a simple FOR loop, for %%V in (dataset) do... if dataset is a list, separated by standard delimiters (space, comma, semicolon) then each time around the loop %%V will hold, one after the other, each item in the list. Thus:

for %%A in (1,2,3) do echo %%A
for %%A in (1 2 3) do echo %%A
for %%A in (1;2;3) do echo %%A

will all output
1
2
3

Quotes around list items allow the item to contain standard delimiters, e.g. spaces. The quotes become part of the variable string.

for %%A in ("1 2" "3,4" 5 6 7; do echo %%A
"1 2"
"3,4"
5
6
7
8


If you want to strip the quotes, then, in the loop, use a tilde (~) before the loop variable like so

for %%A in ("1 2" "3,4" 5 6 7; do echo %%~A
1 2
3,4
5
6
7
8




and, of course, dataset can be a variable:

set MyList="1 2" "3,4" 5 6 7;8
for %%A in (%MyList%) do echo %%A
"1 2"
"3,4"
5
6
7
8
You can use a list separated by LINE endings:

for %%A in (
"2010-11 FY"
"2011-12 FY"
"2012-13 FY"
"2013-14 FY"
"2014-15 FY"
"2015-16 FY"
"2016-17 FY"
"2017-18 FY"
"2018-19 FY"
"2019-20 FY"
) do echo mkdir %%A


Discussion

No Comment Found