Page 1 of 1

How to extract char from string with errorlevel as index

Posted: 04 Apr 2014 08:34
by jwoegerbauer
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

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

Posted: 04 Apr 2014 09:00
by foxidrive
This should work.

Code: Select all

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

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

Posted: 04 Apr 2014 14:08
by jwoegerbauer
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?

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

Posted: 04 Apr 2014 14:20
by aGerman
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

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

Posted: 04 Apr 2014 22:23
by jwoegerbauer
Now it works. Thanks.

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

Posted: 05 Apr 2014 06:46
by Squashman
jwoegerbauer wrote:Now it works. Thanks.

Yes, but do you understand why it works now?

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

Posted: 24 Mar 2015 13:05
by jwoegerbauer
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

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

Posted: 24 Mar 2015 14:26
by aGerman
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

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

Posted: 24 Mar 2015 14:53
by jwoegerbauer
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.

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

Posted: 24 Mar 2015 15:08
by aGerman
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

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

Posted: 25 Mar 2015 10:54
by jwoegerbauer
Did the replacements: All works perfectly.

@aGerman, thank you for helping.