caopeng
caopeng

Reputation: 944

How can we split string using windows bat

How can we split string using windows bat script?

for below .bat code snippet

@echo off & setlocal EnableDelayedExpansion
set j=0

for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
    rem mkdir !a!
) || (
    echo +) 
    rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause

below config file test config file

Q

  1. What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
  2. How can I get the /path/to/case and value as a pair? for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)

Upvotes: 1

Views: 150

Answers (2)

Compo
Compo

Reputation: 38589

I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:

@Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
    If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
        Echo=MD %%A
        Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
        ) Do (Set/A "i+=1"
            Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF

remove Echo= on line 6 if you are happy with the output and really want to create those directories

Upvotes: 0

Magoo
Magoo

Reputation: 79983

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"

set j=0

for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
    echo mkdir !a!
) || (
    echo +)
    for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
    for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q43407067.txt containing your data for my testing.

(These are setting that suit my system)

SO - to address your problems:

  1. because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----

You can't delete that ) because it's participating in a parenthesis-pair.

  1. You need a space between the in and the (.

  2. I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)

In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.

Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.

Upvotes: 1

Related Questions