0

I've got a batch file that runs through some commands, but sometimes these commands output an error code. I would like to make my script automatically do some things when getting specific error codes, which can easily be done by if statements etc - my problem is that I'm not sure how to capture just the error code from the text that is generated when the commands are ran.

Currently the script saves the entire output of the commands into a variable called ato. Inside of this variable, is has the error code in the middle of the text surrounded by ''s. I figure I could use these apostrophe's as delimiters in the variable I want to create, but I'm not sure how to go about this. I have found a few threads which seem to point me in a close direction but I cannot get them to work, I'm rather a newbie to batch scripts.

The strings I am working with looks like this: An error has occurred, error code '0x00000000'. Please try again.

The string can be different lengths, as well as the error, I just need to extract all the information inside the apostrophe's into a second variable.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71

1 Answers1

2

I found a solution to my problem at windows-batch-file-split-string-with-string-as-delimiter by Aacini, but changed a bit. My solution:

for /F "tokens=2 delims='" %%a in ("%ato%") do (
   echo %%a
)

Tokens = 2 to get only the 2nd string that is in the split string.

Community
  • 1
  • 1
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71