How to extract char from string with errorlevel as index

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

How to extract char from string with errorlevel as index

#1 Post by jwoegerbauer » 04 Apr 2014 08:34

Hi all,

I want to get out the selected "volume" after having processed

Code: Select all

choice /C %volumes% /M "Pick a volume"%1

corresponding to ERRORLEVEL returned
This doesn't work:

Code: Select all

IF ERRORLEVEL 1 set idx=1
set volume=%volumes%
set volume=%volume:~%idx%,1%    <-- that's the line which fails

I appreciate any help. TIA

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract char from string with errorlevel as index

#2 Post by foxidrive » 04 Apr 2014 09:00

This should work.

Code: Select all

call set volume=%%volume:~%idx%,1%%

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to extract char from string with errorlevel as index

#3 Post by jwoegerbauer » 04 Apr 2014 14:08

Unfortunately no, although now at least I get a character returned, instead of a nonsense. :)

If for example %volumes% contains character sequence "CDEFG" then always D is returned, regardless what selection was.

Any clues what still is wrong?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to extract char from string with errorlevel as index

#4 Post by aGerman » 04 Apr 2014 14:20

This code works for me

Code: Select all

@echo off &setlocal
set "volumes=CDEFG"
choice /C %volumes% /M "Pick a volume"
set /a "idx = %errorlevel% - 1"
call set "volume=%%volumes:~%idx%,1%%"
echo %volume%
pause

Regards
aGerman

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to extract char from string with errorlevel as index

#5 Post by jwoegerbauer » 04 Apr 2014 22:23

Now it works. Thanks.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to extract char from string with errorlevel as index

#6 Post by Squashman » 05 Apr 2014 06:46

jwoegerbauer wrote:Now it works. Thanks.

Yes, but do you understand why it works now?

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to extract char from string with errorlevel as index

#7 Post by jwoegerbauer » 24 Mar 2015 13:05

Sorry for warming up this thread.

A similar problem as initially posted here arose for me, when I wanted to run a string extraction in a FOR loop:
neither

Code: Select all

CALL SET "flash_drive=%%flash_drives:~%offset%,2%%"


nor

Code: Select all

CALL SET "flash_drive=%%flash_drives:~!offset!,2%%"


works, CMD.exe crashes. I can't read the screen content, because it closes too fast.

Here the code excerpt where the code line is embedded:

Code: Select all

@ECHO OFF
SETLOCAL enableDelayedExpansion

rem a lot of lines of code here

ECHO(
ECHO !"!%TAB%>>> Checking flash drives (SD / USB) for enough capacity ...
SET /p flash_drives=<%found_flash_drives_file%
SET flash_drives=%flash_drives:~0,-1%
rem Content looks like "E: G: I: "
CALL :getStringLength "%flash_drives%"
rem String length must be a real multiple of 3
SET /a "test=%string_length% %% 3"
IF %test% NEQ 0 (
   CALL :displayError "FAILED: Checking flash drives (SD / USB) for enough capacity"
   GOTO :END
)
FOR /l %%i IN (1,3,%string_length%) DO (
   SET /a "offset=%%i-1"
   CALL SET "flash_drive=%%flash_drives:~!offset!,2%%"           <=== THIS IS CRASHING
   CALL :getTotalVolumeSpaceGB "!flash_drive!" 
   IF %script_error_occurred% EQU 1 (
      CALL :displayError "FAILED: getTotalVolumeSpaceGB"
      GOTO :END
   )   
   IF %disk_space_total_GB% LSS %disk_space_needed_GB% (
      SET "unusable_flash_drives=!unusable_flash_drives!!flash_drive! "
   )
)


rem another lot of lines of code here

:END
ENDLOCAL
EXIT /b


Hope you can help me again. TIA

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to extract char from string with errorlevel as index

#8 Post by aGerman » 24 Mar 2015 14:26

I don't believe the script crashes at the point that you marked but to make sure you should output the variable %flash_drives% before.

My guesses:

Code: Select all

   IF %script_error_occurred% EQU 1 (

Was script_error_occurred already defined before the FOR loop began? Or would you rather need to enclose the varable into exclamation marks instead?

Code: Select all

   IF %disk_space_total_GB% LSS %disk_space_needed_GB% (

Same question here.

Regards
aGerman

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to extract char from string with errorlevel as index

#9 Post by jwoegerbauer » 24 Mar 2015 14:53

You are right: this works

Code: Select all

FOR /l %%i IN (1,3,%string_length%) DO (
   SET /a "offset=%%i-1"
   CALL SET "flash_drive=%%flash_drives:~!offset!,2%%"
)


but now I don't know how to add "correctly coded" lines currently removed

EDIT:

Variable script_error_occured is declared and initialized to 0 before entering the FOR loop, same with variable disk_space_needed_GB. Variable disk_space_total_GB gets (first time) defined and initialized inside the FOR loop.
Last edited by jwoegerbauer on 24 Mar 2015 15:10, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to extract char from string with errorlevel as index

#10 Post by aGerman » 24 Mar 2015 15:08

As I already wrote - replace the percent signs with exclamation marks in the lines that I quoted.
Since I don't know the code in the called subroutines you may also need to initialize these variables (assign 0 for those that are not defined before the beginning of the loop).

Regards
aGerman

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: How to extract char from string with errorlevel as index

#11 Post by jwoegerbauer » 25 Mar 2015 10:54

Did the replacements: All works perfectly.

@aGerman, thank you for helping.

Post Reply