You are not logged in.
Is there a way to sort digits in an array from lowest to highest eg 1 - 20, WITHOUT resorting to prefixing a leading 0 so they are both same number of digits? This is because my range may vary somewhat, and I don't want to have to write extra logic to deal with prefixing 0's etc. I understand that variables are text, however I'm sure someone here has a simple work around.
eg
set _x_1=value
set _x_10=value
set _x_2=value
set _x_[10]=value
set _x_[11]=value
set _x_[1]=value
set _x_[2]=value
set _x_[3]=value
values are displayed in that order, if using the set command
for /f "usebackq tokens=2-3* delims=_=" %g in (`set _x_ 2^>nul`) do (echo %g&echo %h)
If I want to call the values on the array, I would use a command as above, however it is not sorted by numerical order.
?
Last edited by NDog (06 Jan 2018 01:35)
cmd, vbs, ps, bash
autoit, python, swift
Offline
As you said, such values are sorted in that way if using the set command, so the solution is not use set to process the array elements, but a for /L command:
for /L %%i in (1,1,20) do echo !_x_[%%i]!
Of course, this method requires setlocal EnableDelayedExpansion command at beginning of the program.
Antonio
Offline
Hi Aacini
Thanks, its a simple tip but works great.
EnableDelayedExpansion not required if you call a sub
@echo off&cls
for %%a in ("%~dp0\.") do set _parentdir=%%~nxa
title %_parentdir%
:: Install Software - by ndog, credits to Aacini
set _fPath_[1]=%~dp0Drivers\Broadcom Bluetooth Software 6.5\sp61617\Win64\BTW.msi
set _swtch_[1]=/passive
set _regKy_[1]={A1439D4F-FD46-47F2-A1D3-FEE097C29A09}
set _regDN_[1]=Broadcom Bluetooth Software
set _regDV_[1]=6.5.1.3700
set _fPath_[2]=%~dp0Drivers\Validity Fingerprint Sensor Driver 4.3.301.0\sp56403.exe
set _swtch_[2]=/s
set _regKy_[2]={DA83578A-7DB2-4CF6-9453-CF24C7917AB8}
set _regDN_[2]=Validity WBF DDK
set _regDV_[2]=4.3.301.0
set _fPath_[3]=%~dp0Utilities\HP CoolSense Technology 2.10.51\sp60051.exe
set _swtch_[3]=/s /v"/passive"
set _regKy_[3]={11AF9A96-6D83-4C3B-8DCB-16EA2A358E3F}
set _regDN_[3]=HP CoolSense
set _regDV_[3]=2.10.51
set _fPath_[4]=%~dp0Utilities\HP Power Manager Utility Software 1.40\sp57398.exe
set _swtch_[4]=/s /v"/passive"
set _regKy_[4]={7E799992-5DA0-4A1A-9443-B1836B063FEC}
set _regDN_[4]=HP Power Manager
set _regDV_[4]=1.4.8
:: loop through installation packages
set _count=4
for /l %%g in (1,1,%_count%) do (call :sub_install "%%g")
:: script finished
goto :eof
:sub_install <index>
:: set variables
set _index=%~1
call set _fPath=%%_fPath_[%_index%]%%%
if not defined _fPath goto :eof
call set _swtch=%%_swtch_[%_index%]%%%
call set _regKy=%%_regKy_[%_index%]%%%
call set _regDN=%%_regDN_[%_index%]%%%
call set _regDV=%%_regDV_[%_index%]%%%
echo Installing %_regDN% %_regDV%
:: query registry for DisplayVersion 64bit and 32bit uninstall locations
for /f "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%_regKy%" /v "DisplayVersion" 2^>nul') do set _reg_version=%%a
if "%_reg_version%" geq "%_regDV%" goto :eof
for /f "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\%_regKy%" /v "DisplayVersion" 2^>nul') do set _reg-wow64_version=%%a
if "%_reg-wow64_version%" geq "%_regDV%" goto :eof
:: install software with switch
start /wait "" "%_fPath%" %_swtch%
goto :eof
Last edited by NDog (09 Jan 2018 14:25)
cmd, vbs, ps, bash
autoit, python, swift
Offline
Hi Aacini
Thanks, its a simple tip but works great.
EnableDelayedExpansion not required if you call a sub. . . .
Well, yes, but call command is less efficient than using Delayed Expansion, so I used to not use call to achieve a double expansion, excepting in certain cases...
Anyway, your call example is wrong. You inserted three percent-signs at end, but the right number is two:
:sub_install <index>
:: set variables
set _index=%~1
call set _fPath=%%_fPath_[%_index%]%%
if not defined _fPath goto :eof
call set _swtch=%%_swtch_[%_index%]%%
call set _regKy=%%_regKy_[%_index%]%%
call set _regDN=%%_regDN_[%_index%]%%
call set _regDV=%%_regDV_[%_index%]%%
. . . .
Also, it has no much sense to duplicate the value of %~1 parameter into _index variable just to use its value. You may directly use the parameter in each case:
:sub_install <index>
:: set variables
call set _fPath=%%_fPath_[%~1]%%
if not defined _fPath goto :eof
call set _swtch=%%_swtch_[%~1]%%
call set _regKy=%%_regKy_[%~1]%%
call set _regDN=%%_regDN_[%~1]%%
call set _regDV=%%_regDV_[%~1]%%
. . . .
Antonio
Offline