Umang Bhatia
Umang Bhatia

Reputation: 67

Splitting a String Variable in Windows Bat File

I am executing a command that returns a string output in format - 'SessionID:xxx-xxx-xxx-xxx'. I want to split this SessionID string based on ':' and store the later part in a variable called Session_ID.

I am trying the below code:

 @echo off
    
    echo Changing the Directory to Blue Prism Automate
    cd C:\Program Files\Blue Prism Limited\Blue Prism Automate

    echo Triggering CSM Validation Process and Storing the Session ID
    for /f %%i in ('AutomateC.exe /resource BWD14195034 /port 8215 /sso /run "CSMValidationInput"') do set RESULT=%%i
    
    For /f "delims=:" %%A in ("%RESULT%") do Set Session_ID = %%A
    
    echo %Session_ID%
    
    Pause

Upon executing I am getting 'Echo is OFF'. Why am i not able to split my string and get the Session ID?

Upvotes: 0

Views: 70

Answers (2)

Compo
Compo

Reputation: 38589

I'm not sure why you're using two s:

@Echo Off

Echo Changing the current directory to Blue Prism Automate.
CD /D "%ProgramFiles%\Blue Prism Limited\Blue Prism Automate" 2>NUL||GoTo :EOF

Echo Triggering CSM validation and storing the session ID as a variable.
For /F "Tokens=1,* Delims=:" %%G In ('AutomateC.exe /resource BWD14195034 /port 8215 /sso /run "CSMValidationInput"')Do Set "%%G_ID=%%H"

Echo(%Session_ID%

Pause

Upvotes: 1

user7818749
user7818749

Reputation:

Fixed code. Note double quotes around path, /d switch with cd, removal of spaces when setting variables and then using tokens=2* to get the second part of the split string. where token=1 would be everything before the :

@echo off
  
echo Pushing to 'Blue Prism Automate' Directory
cd /d "C:\Program Files\Blue Prism Limited\Blue Prism Automate"

echo Triggering CSM Validation Process and Storing the Session ID
for /f %%i in ('AutomateC.exe /resource BWD14195034 /port 8215 /sso /run "CSMValidationInput"') do set RESULT=%%i
    
For /f "tokens=2* delims=:" %%A in ("%RESULT%") do Set "Session_ID=%%A" 
echo %Session_ID% 
Pause

I would have however used pushd instead:

@echo off
  
echo Changing the Directory to Blue Prism Automate
pushd "C:\Program Files\Blue Prism Limited\Blue Prism Automate"

echo Triggering CSM Validation Process and Storing the Session ID
for /f %%i in ('AutomateC.exe /resource BWD14195034 /port 8215 /sso /run "CSMValidationInput"') do set RESULT=%%i
popd
For /f "tokens=2* delims=:" %%A in ("%RESULT%") do Set "Session_ID=%%A" 
echo %Session_ID% 
Pause

Upvotes: 0

Related Questions