Tristan Otterpohl
Tristan Otterpohl

Reputation: 15

Remove last 2 characters of string if set variable is 2 digits long

Is there a way to remove the last two digits if the set variable added is 2 digits long

eg. 000%i% may equal 0001 or 00015 where as I need for it to be 0015 not 00015

I then had to make almost redundant and unnecessary code to bypass this.

Please ignore the bad format, this is basically my first try at scripting.

See Below Code:

@echo off

set brokendriver=Qualcomm Atheros AR9285 Wireless Network Adapter
set brokendriver1= Random Name

set /a i=0
set /a max=9
set /a Limit=30

:start

IF %i%==%Limit% (Echo Driver Not Found
pause
exit)

IF %i% LEQ %max% (
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000%i% /v DriverDesc`) DO (
    set drivername=%%A %%B
    )
) ELSE (
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\00%i% /v DriverDesc`) DO (
    set drivername=%%A %%B
    )
  )

IF "%brokendriver%"=="%drivername%" (GOTO:next)
IF "%brokendriver1%"=="%drivername%" (GOTO:next)

set /a i+=1
GOTO:start

:next
IF %i% LEQ %max% (
    FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000%i% /v PnPCapabilities`) DO (
    set driveroldvalue=%%A %%B )
    REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\000%i% /v PnPCapabilities /t REG_DWORD /d 24 /f
    FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000%i% /v PnPCapabilities`) DO (
    set drivernewvalue=%%A %%B
    )
) ELSE (
    FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\00%i% /v PnPCapabilities`) DO (
    set driveroldvalue=%%A %%B )
    REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\00%i% /v PnPCapabilities /t REG_DWORD /d 24 /f
    FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\00%i% /v PnPCapabilities`) DO (
    set drivernewvalue=%%A %%B
    )
)

Echo Driver Name: "%drivername%"
Echo Driver Old Value: %driveroldvalue%
Echo Driver New Value: %drivernewvalue%

pause

Upvotes: 1

Views: 73

Answers (2)

Aacini
Aacini

Reputation: 67216

This is the way I would solve this:

@echo off

set brokendriver=Qualcomm Atheros AR9285 Wireless Network Adapter
set brokendriver1= Random Name

set /a i=10000
set /a Limit=10030

:start

IF %i%==%Limit% (Echo Driver Not Found
pause
exit)

FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\%i:~1% /v DriverDesc`) DO (
    set drivername=%%A %%B
)

The i variable have an initial value of 10000, so you just need to get the value of i after the first digit, that is %i:~1% to get a number with four digits always. The increment of i works in the same way and it is only necessary to change the Limit from 30 to 10030.

Upvotes: 2

Magoo
Magoo

Reputation: 80033

Difficult to tell from all that code where you are trying to put what.

Tip: After @echo off, put a line

setlocal

which will ensure that any changes you make to the environment are discarded when the batch ends. That way, you don't have stale data ffrom a previous run while you're debugging.

As for getting just the last n digits, you can use %var:~-n% where var contains your string.

If you are using numerics, then

set /a var+=1000000

will make sure your variable is 7-digits long (provided var is originally 0..999999) and then %var:~-5% will provide the last 5 digits. Mix and match to suit your situation.


For example

set /a paddedi=1000000+i 

FOR /F "usebackq tokens=3*" %%A IN (
 `REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\%paddedi:~-4% /v DriverDesc`
 ) DO set drivername=%%A %%B

replaces the entire second if statement, padding the value of i (great name for a variable!) and then using the last 4 characters of the resulting padded variable.

Upvotes: 0

Related Questions