Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
jwoegerbauer
- Posts: 33
- Joined: 01 Jan 2013 12:09
#1
Post
by jwoegerbauer » 26 Feb 2015 13:04
Hi there,
again I've a huge problem.
In this code (excerpt!)
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
....
IF %archive_volume%==%backup_volume% (
:GET_ARCHIVE_VOLUME
rem We only offer the drives-menu, if at least among two drives can be selected
SET "string_length="
FOR %%? IN (%drive_letters_file%) DO ( SET /a string_length=%%~z?-2 )
IF !string_length! GEQ 2 (
SET "idx="
SET "available_drive_letters="
FOR /f %%? IN (%drive_letters_file%) DO ( SET available_drive_letters=%%? )
CLS
ECHO.
ECHO Drive %archive_volume% by nature can't be used as backup volume.
CHOICE /C:!available_drive_letters! /M:"Pick out drive where archive should be stored"
SET /a "idx=!errorlevel!"
SET archive_drive_letter=!available_drives_letter:~!idx!,1!
the last line doesn't work as expected. It should result in a drive letter (eg. F) got as substring of 'available_drives_letter' (eg. DEFGH) depending on 'errorlevel' which 'CHOICE' returned. The substring returned is "~3,1" if I enter the the 3rd character provided by 'CHOICE', for example.
It drives me nuts. Wasted hours to find a solution myself. Now I hope you can help me, i.e. can provide the correct code.
TIA
-
Squashman
- Expert
- Posts: 4471
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 26 Feb 2015 13:52
You can't do that. Not sure if a CALL SET will work either. Will need to test it.
-
ShadowThief
- Expert
- Posts: 1163
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#3
Post
by ShadowThief » 26 Feb 2015 13:52
Your problem lies in the last line:
!available_drives_letter:~
!idx
!,1!
Instead of the inner !s matching and the outer !s matching, batch processes the left two as a pair and the right two as a pair. You need to stick !idx! in a for loop:
Code: Select all
for %%A in (!idx!) do set archive_drive_letter=!available_drives_letter:~%%A,1!
Please note that I haven't tested this.
-
Squashman
- Expert
- Posts: 4471
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 26 Feb 2015 13:57
You also have to remember that substring is an offset. Not the actual starting position.
If your CHOICE command has the letters C D E F G. Then letter C is a 0 offset. Letter D is an offset of 1. So you would need to subtract from the ERRORLEVEL.
-
Squashman
- Expert
- Posts: 4471
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 26 Feb 2015 14:00
ShadowThief wrote:Your problem lies in the last line:
!available_drives_letter:~
!idx
!,1!
Instead of the inner !s matching and the outer !s matching, batch processes the left two as a pair and the right two as a pair. You need to stick !idx! in a for loop:
Code: Select all
for %%A in (!idx!) do set archive_drive_letter=!available_drives_letter:~%%A,1!
Please note that I haven't tested this.
Yes. That should work for the substring but still need to subtract 1 from the idx variable.
-
jwoegerbauer
- Posts: 33
- Joined: 01 Jan 2013 12:09
#6
Post
by jwoegerbauer » 26 Feb 2015 23:43
Yes, this works
Code: Select all
SET /a "idx=!errorlevel!-1"
for %%A in (!idx!) do set archive_drive_letter=!available_drive_letters:~%%A,1!
thank you all so much.