Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Exclude space as delimiter in a list  (Read 6098 times)

0 Members and 1 Guest are viewing this topic.

bazzao

    Topic Starter


    Newbie
    • Experience: Expert
    • OS: Windows 10
    Exclude space as delimiter in a list
    « on: August 09, 2019, 04:27:55 PM »
    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
    Bazzao

    Salmon Trout

    • Guest
    Re: Exclude space as delimiter in a list
    « Reply #1 on: August 10, 2019, 06:17:24 AM »
    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"





    bazzao

      Topic Starter


      Newbie
      • Experience: Expert
      • OS: Windows 10
      Re: Exclude space as delimiter in a list
      « Reply #2 on: August 10, 2019, 02:11:13 PM »
      1. Use quotes around list items.

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

      Well that worked. Many thanks Salmon Trout.
      Bazzao

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Exclude space as delimiter in a list
      « Reply #3 on: August 10, 2019, 02:34:21 PM »
      He Da Man... 8)
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: Exclude space as delimiter in a list
      « Reply #4 on: August 11, 2019, 04:30:39 AM »
      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;8) 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;8) do echo %%~A
      1 2
      3,4
      5
      6
      7
      8





      Salmon Trout

      • Guest
      Re: Exclude space as delimiter in a list
      « Reply #5 on: August 11, 2019, 08:04:45 AM »
      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

      Salmon Trout

      • Guest
      Re: Exclude space as delimiter in a list
      « Reply #6 on: August 11, 2019, 12:30:34 PM »
      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