Search code examples
cmdsplitdouble-quotes

Split string by Quotation Mark And Followed By Space CMD Batch File


I have a string as:

"C:\Users\Desktop\Download 4.txt" "C:\Users\Desktop\Download 2.txt" "C:\Users\Desktop\Download 3.txt"

Output I need as follow:

"C:\Users\Desktop\Download 4.txt"
"C:\Users\Desktop\Download 2.txt"
"C:\Users\Desktop\Download 3.txt"

How should I get that using CMD?

Please help.


Solution

  • It's very easy as the FOR command splits a string using spaces and treats quoted strings as single tokens.

    @echo off
    set mystring="C:\Users\Desktop\Download 4.txt" "C:\Users\Desktop\Download 2.txt" "C:\Users\Desktop\Download 3.txt"
    echo Original:
    echo %mystring%
    echo Split:
    for %%i in (%mystring%) do echo %%i