How to split a line in a Windows batch file?

Suppose I have the line "AAA BBB CCC DDD EEE FFF".

How can I split a string and get the nth substring in a batch file?

Equivalent in C # would be

"AAA BBB CCC DDD EEE FFF".Split()[n] 
+43
split windows batch-file
Nov 10 '09 at 10:47
source share
15 answers

see HELP FOR and see examples

or try this

  for /F %%a in ("AAA BBB CCC DDD EEE FFF") do echo %%c 
+26
Nov 10 '09 at 10:53
source share

Three possible solutions to iterate over the words of a string:

Version 1:

 @echo off & setlocal set s=AAA BBB CCC DDD EEE FFF for %%a in (%s%) do echo %%a 

Version 2:

 @echo off & setlocal set s=AAA BBB CCC DDD EEE FFF set t=%s% :loop for /f "tokens=1*" %%a in ("%t%") do ( echo %%a set t=%%b ) if defined t goto :loop 

Version 3:

 @echo off & setlocal set s=AAA BBB CCC DDD EEE FFF call :sub1 %s% exit /b :sub1 if "%1"=="" exit /b echo %1 shift goto :sub1 

Version 1 does not work when a string contains wildcards such as '*' or '?'.

Versions 1 and 3 refer to characters like '=', ';' or ',' as word separators. These characters have the same effect as the space character.

+26
Sep 25 '13 at 15:57
source share

This is the only code that worked for me:

 for /f "tokens=4" %%G IN ("aaa bbb ccc ddd eee fff") DO echo %%G 

exit:

 ddd 
+20
Nov 15 '12 at 16:52
source share

The following code will split the string into an arbitrary number of substrings:

 @echo off setlocal ENABLEDELAYEDEXPANSION REM Set a string with an arbitrary number of substrings separated by semi colons set teststring=The;rain;in;spain REM Do something with each substring :stringLOOP REM Stop when the string is empty if "!teststring!" EQU "" goto END for /f "delims=;" %%a in ("!teststring!") do set substring=%%a REM Do something with the substring - REM we just echo it for the purposes of demo echo !substring! REM Now strip off the leading substring :striploop set stripchar=!teststring:~0,1! set teststring=!teststring:~1! if "!teststring!" EQU "" goto stringloop if "!stripchar!" NEQ ";" goto striploop goto stringloop ) :END endlocal 
+14
Jan 20 '11 at 11:17
source share

Easily

batch file:

 FOR %%A IN (1 2 3) DO ECHO %%A 

:

 FOR %A IN (1 2 3) DO ECHO %A 

exit:

 1 2 3 
+7
Nov 10 2018-10-10
source share

or Powershell for an index at index 0.

 PS C:\> "AAA BBB CCC DDD EEE FFF".Split() AAA BBB CCC DDD EEE FFF PS C:\> ("AAA BBB CCC DDD EEE FFF".Split())[0] AAA 
+2
Jan 09 '13 at 11:22
source share

The following code will split the string by N the number of C # substrings with the separated values. You can use any separator

 @echo off if "%1" == "" goto error1 set _myvar="%1" :FORLOOP For /F "tokens=1* delims=#" %%A IN (%_myvar%) DO ( echo %%A set _myvar="%%B" if NOT "%_myvar%"=="" goto FORLOOP ) goto endofprogram :error1 echo You must provide Argument with # separated goto endofprogram :endofprogram 
+2
Jul 31 '13 at 11:15
source share
 @echo off :: read a file line by line for /F %%i in ('type data.csv') do ( echo %%i :: and we extract four tokens, ; is the delimiter. for /f "tokens=1,2,3,4 delims=;" %%a in ("%%i") do ( set first=%%a&set second=%%b&set third=%%c&set fourth=%%d echo %first% and %second% and %third% and %fourth% ) ) 
+2
Oct 06 '13 at 22:45
source share

I got the following:

 set input=AAA BBB CCC DDD EEE FFF set nth=4 for /F "tokens=%nth% delims= " %%a in ("%input%") do set nthstring=%%a echo %nthstring% 

With this, you can parameterize input and index. Be sure to put this code in the bat file.

+2
Nov 12 '14 at 12:02
source share
 set a=AAA BBB CCC DDD EEE FFF set a=%a:~6,1% 

This code finds the 5th character in the string. If I wanted to find the 9th line, I would replace 6 with 10 (add one).

+1
Jun 30 2018-12-12T00:
source share

Here is a function- based solution that processes each character until it finds a separator character.

It's relatively slow, but at least it's not a brain teaser (other than the functional part).

 :: Example #1: set data=aa bb cc echo Splitting off from "%data%": call :split_once "%data%" " " "left" "right" echo Split off: %left% echo Remaining: %right% echo. :: Example #2: echo List of paths in PATH env var: set paths=%PATH% :loop call :split_once "%paths%" ";" "left" "paths" if "%left%" equ "" goto loop_end echo %left% goto loop :loop_end :: HERE BE FUNCTIONS goto :eof :: USAGE: :: call :split_once "string to split once" "delimiter_char" "left_var" "right_var" :split_once setlocal set right=%~1 set delimiter_char=%~2 set left= if "%right%" equ "" goto split_once_done :split_once_loop if "%right:~0,1%" equ "%delimiter_char%" set right=%right:~1%&& goto split_once_done if "%right:~0,1%" neq "%delimiter_char%" set left=%left%%right:~0,1% if "%right:~0,1%" neq "%delimiter_char%" set right=%right:~1% if "%right%" equ "" goto split_once_done goto split_once_loop :split_once_done endlocal & set %~3=%left%& set %~4=%right% goto:eof 
+1
Feb 06 '14 at 10:44
source share

you can use vbscript instead of package (cmd.exe)

 Set objFS = CreateObject("Scripting.FileSystemObject") Set objArgs = WScript.Arguments str1 = objArgs(0) s=Split(str1," ") For i=LBound(s) To UBound(s) WScript.Echo s(i) WScript.Echo s(9) ' get the 10th element Next 

using:

 c:\test> cscript /nologo test.vbs "AAA BBB CCC" 
0
Nov 10 '09 at 11:13
source share

UPDATE: Well, initially I posted a solution to a more complex problem to get a complete separation of any line with any separator (just changing the separators). I read more decisions than the OP wanted, sorry. I think this time I obey the original requirements:

  @echo off IF [%1] EQU [] echo get n ["user_string"] & goto :eof set token=%1 set /a "token+=1" set string= IF [%2] NEQ [] set string=%2 IF [%2] EQU [] set string="AAA BBB CCC DDD EEE FFF" FOR /F "tokens=%TOKEN%" %%G IN (%string%) DO echo %%~G 

Another version with a better user interface:

  @echo off IF [%1] EQU [] echo USAGE: get ["user_string"] n & goto :eof IF [%2] NEQ [] set string=%1 & set token=%2 & goto update_token set string="AAA BBB CCC DDD EEE FFF" set token=%1 :update_token set /a "token+=1" FOR /F "tokens=%TOKEN%" %%G IN (%string%) DO echo %%~G 

Output Examples:

 E:\utils\bat>get USAGE: get ["user_string"] n E:\utils\bat>get 5 FFF E:\utils\bat>get 6 E:\utils\bat>get "Hello World" 1 World 

This is a batch file to separate path directories:

 @echo off set string="%PATH%" :loop FOR /F "tokens=1* delims=;" %%G IN (%string%) DO ( for /f "tokens=*" %%g in ("%%G") do echo %%g set string="%%H" ) if %string% NEQ "" goto :loop 

Second version:

 @echo off set string="%PATH%" :loop FOR /F "tokens=1* delims=;" %%G IN (%string%) DO set line="%%G" & echo %line:"=% & set string="%%H" if %string% NEQ "" goto :loop 

Third version:

 @echo off set string="%PATH%" :loop FOR /F "tokens=1* delims=;" %%G IN (%string%) DO CALL :sub "%%G" "%%H" if %string% NEQ "" goto :loop goto :eof :sub set line=%1 echo %line:"=% set string=%2 
0
Sep 18 '14 at 19:30
source share

This works for me (just an excerpt from my entire script)

 choice /C 1234567H /M "Select an option or ctrl+C to cancel" set _dpi=%ERRORLEVEL% if "%_dpi%" == "8" call :helpme && goto menu for /F "tokens=%_dpi%,*" %%1 in ("032 060 064 096 0C8 0FA 12C") do set _dpi=%%1 echo _dpi:%_dpi%: 
0
Oct 30 '14 at 19:09
source share

Another option is that it searches for cmd.exe in the current path and reports the first match:

 @echo off setlocal setlocal enableextensions setlocal enabledelayedexpansion set P=%PATH% :pathloop for /F "delims=; tokens=1*" %%f in ("!P!") do ( set F=%%f if exist %%f\cmd.exe goto found set P=%%g ) if defined P goto pathloop echo path of cmd.exe was not found! goto end :found echo found cmd.exe at %F% goto end :end 
-one
Dec 03 '14 at 13:18
source share



All Articles