::Created by victor888, QQ:2511755859
::Special thanks to CLONECD
::Special thanks to all the testers who provided with valid test results.
::Ordering and showing messed drive letters by drive type

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

set ver=2022-07-15

::if "AUTO" was set to "y", all messed drive letters will be re-ordered automaticly without notification. 
::if "AUTO" was set to "n", a notification will come out to ask for confirmation wether to re-order messed driver letters. 

set AUTO=y

set USBCDROM=T
set USBDRV=U

:: 1. Fix the only USB CDROM drive letter to the letter assigned by USBCDROM, such as T.
:: 2. Fix the only Flash drive letter to the letter assigned by USBDRV, such as U.
:: 3. The assigned letters should not be exist letters, otherwise error will occured.
:: 4. Both T and U could be deleted, which means it doesn't need to fix them.

pushd %~dp0

if %~d0 neq %systemdrive% (
  if not exist %temp%\%~nx0 (
    copy %0 %temp%\ /y >nul
    copy fsutil.exe %temp%\ /y >nul 2>&1
    copy mountvol.exe %temp%\ /y >nul 2>&1
    copy drvtype.exe %temp%\ /y >nul 2>&1
    copy smartctl.exe %temp%\ /y >nul 2>&1
    pushd %temp%\
    call %~nx0
    exit
  )
)

echo                 Batch file for ordering messed drive letters.
echo                    V.!ver!, By Victor888 from WUYOU.NET
echo       ------------------------------------------------------------------
echo.
echo Here is detailed information:
echo.

::get all drive letters by fsutil.exe
call :fsutil
if defined ALLDRV (
  set ALLDRV=!ALLDRV:\=!
  if defined ALLDRV (
    set ALLDRV=!ALLDRV: =!
    if defined ALLDRV (
      set ALLDRV=!ALLDRV::=: !
    )
  )
)

echo All drive letters: %ALLDRV%

::get all volume mount points and their full paths and merge them
FOR /F %%a in ('mountvol ^|find "\"') do set mtvdrv=!mtvdrv! %%a

::get unique drive letters which contain ":"
set mtvdrv=%mtvdrv: =%
set mtvdrv=%mtvdrv:\\?\Volume= %
set mtvdrv=%mtvdrv:}\=}%

for %%a in (%mtvdrv%) do (
  for /f "delims=} tokens=1,2" %%b in ("%%a") do (
    set drv=%%c
    set ttmp=%%c
    set ttmp=!ttmp:~-2,1!
    if !ttmp! equ : set mtdrv=!mtdrv! !drv:~0,2!
  )
)

if defined mtdrv (
  set mtdrv=!mtdrv: =!
  if defined mtdrv (
    set mtdrv=!mtdrv::=: !
    echo Mounted drive letters: !mtdrv!
  )
)

::get virtual drive letters which has no mount point
if defined ALLDRV set virdrv=%ALLDRV%
if defined mtdrv (
  for %%a in (%mtdrv%) do (
    set virdrv=!virdrv:%%a=!
  )
)

if defined virdrv (
  set virdrv=!virdrv: =!
  if defined virdrv set virdrv=!virdrv::=: !
  if defined virdrv echo Virtual drive letters: !virdrv!
  for /f "tokens=3 delims=	 " %%a in ('reg query HKLM\System\CurrentControlSet\Services\ISODrive\Parameters /s 2^>nul ^|find /i "drive"') do (
    if %errorlevel% equ 0 (
      set indvir=%%a
      if defined indvir (
        for %%b in (!virdrv!) do (
          if /i !indvir!: equ %%b (
            echo The ULTRAISO produced drive letter %%b 
          )
        )
      )
    )
  )
)

:: Determine system type

reg query "hklm\system\currentcontrolset\control\MiniNT" 2>nul && set ispe=ok

::get hard disk drive letters, SSD drive letters, Dataram drive letters and fixed USB drive letters by clonecd's DRVTYPE. 
for /f "tokens=1-5 delims=|" %%a in ('drvtype -a ^|find ":"') do (
  set diskorder=%%a
  set hdtype=%%b
  set gptmbr=%%c
  set isssd=%%d
  set strdrv=%%e
  set oshd=
  set checktp=
  set hddrv=
 
  if not "!isssd!"=="SSD" (
    for /f "delims=" %%g in ('reg query HKLM\SYSTEM\CurrentControlSet\services\disk\enum /v %%a  /t reg_sz 2^>nul ^|find "REG_SZ"') do (
      set checktp=%%g
      if defined checktp (
        for /f "tokens=2 delims=\" %%g in ("!checktp!") do set prdstr=%%g
        if /i "!prdstr!" neq "!prdstr:ssd=!" set isssd=SSD
      )
    )
  )

  if not "!isssd!"=="SSD" (
    smartctl -i /dev/pd%%a |find /i "solid" >nul && set isssd=SSD
    smartctl -i /dev/pd%%a |find /i "nvme" >nul && set isssd=SSD
  )
  
  if not defined strdrv set strdrv=%%d

  for %%f in (!strdrv!) do (
    set eachdrv=%%f
    set eachdrv=!eachdrv:~1,2!!
    if "!eachdrv:$=!"=="!eachdrv!" (
      if exist !eachdrv!\windows\system32\kernel32.dll set oshd=ok
      set hddrv=!hddrv! !eachdrv!
      set order=!order! !diskorder!:%%f
    )
  )
  if "!hddrv!" neq "!hddrv:%systemdrive%=!" set syshddrv=!hddrv!

  if defined oshd (
    set hdtype=!hdtype:Vmware=!
    if !hdtype! neq %%b (
      set /a ii+=1
      echo Vmware Internel !gptmbr! hard disk !ii! mount path: !hddrv!
      set osthddrv=!osthddrv! !hddrv!
    ) else (
      set hdtype=!hdtype:diskvirtual=!
      if !hdtype! neq %%b (
        set /a rr+=1
        echo Virtual PC Internel !gptmbr! hard disk !rr! mount path: !hddrv!
        set osthddrv=!osthddrv! !hddrv!
      ) else (
        set "hdtype=!hdtype:Virtual=!"
        if !hdtype! neq %%b (
          set /a jj+=1
          echo Virtual !gptmbr! hard disk !jj! mount path: !hddrv!
          set virdrv=!virdrv! !hddrv!
        ) else (
          set hdtype=!hdtype:RAMDISK=!
          if !hdtype! neq %%b (
            set /a oo+=1
            echo RAMDISK !gptmbr! hard disk !oo! mount path: !hddrv!
            set virdrv=!virdrv! !hddrv!
          ) else (
            set hdtype=!hdtype:FIRADISK=!
            if !hdtype! neq %%b (
              set /a pp+=1
              echo FIRADISK !gptmbr! hard disk !pp! mount path: !hddrv!
              set firadrv=!firadrv! !hddrv!
            ) else (
              set hdtype=!hdtype:winvblock=!
              if /i !hdtype! neq %%b (
                set /a bb+=1
                echo WINVBLOCK !gptmbr! hard disk !bb! mount path: !hddrv!
                set winvdrv=!winvdrv! !hddrv!
              ) else (
                set hdtype=!hdtype:USB=!
                if /i !hdtype! neq %%b (
                  set /a nn+=1
                  echo Fixed USB !gptmbr! disk !nn! mount path: !hddrv!
                  set osusbhddrv=!osusbhddrv! !hddrv!
                ) else (
                  if "!isssd!" equ "SSD" (
                    set /a qq+=1
                    echo Solid State !gptmbr! Hard disk !qq! mount path: !hddrv!
                    set osssddrv=!osssddrv! !hddrv!
                  ) else (
                    set /a ll+=1
                    set osthddrv=!osthddrv! !hddrv!
                    echo Internal !gptmbr! hard disk !ll! mount path: !hddrv!
                  )
                )
              )
            )
          )
        )
      )
    )
  ) else (
    set hdtype=!hdtype:Vmware=!
    if !hdtype! neq %%b (
      set /a ii+=1
      echo Vmware Internel !gptmbr! hard disk !ii! mount path: !hddrv!
      set thddrv=!thddrv! !hddrv!
    ) else (
      set hdtype=!hdtype:diskvirtual=!
      if !hdtype! neq %%b (
        set /a rr+=1
        echo Virtual PC Internel !gptmbr! hard disk !rr! mount path: !hddrv!
        set thddrv=!thddrv! !hddrv!
      ) else (
        set "hdtype=!hdtype:Virtual=!"
        if !hdtype! neq %%b (
          set /a jj+=1
          echo Virtual !gptmbr! hard disk !jj! mount path: !hddrv!
          set virdrv=!virdrv! !hddrv!
        ) else (
          set hdtype=!hdtype:RAMDISK=!
          if !hdtype! neq %%b (
            set /a oo+=1
            echo RAMDISK !gptmbr! hard disk !oo! mount path: !hddrv!
            set virdrv=!virdrv! !hddrv!
          ) else (
            set hdtype=!hdtype:FIRADISK=!
            if !hdtype! neq %%b (
              set /a pp+=1
              echo FIRADISK !gptmbr! hard disk !pp! mount path: !hddrv!
              set firadrv=!firadrv! !hddrv!
            ) else (
              set hdtype=!hdtype:winvblock=!
              if /i !hdtype! neq %%b (
                set /a bb+=1
                echo WINVBLOCK !gptmbr! hard disk !bb! mount path: !hddrv!
                set winvdrv=!winvdrv! !hddrv!
              ) else (
                set hdtype=!hdtype:USB=!
                if /i !hdtype! neq %%b (
                  set /a nn+=1
                  echo Fixed USB !gptmbr! disk !nn! mount path: !hddrv!
                  set usbhddrv=!usbhddrv! !hddrv!
                ) else (
                  if "!isssd!" equ "SSD" (
                    set /a qq+=1
                    echo Solid State !gptmbr! Hard disk !qq! mount path: !hddrv!
                    set ssddrv=!ssddrv! !hddrv!
                  ) else (
                    set /a ll+=1
                    set thddrv=!thddrv! !hddrv!
                    echo Internal !gptmbr! hard disk !ll! mount path: !hddrv!
                  )
                )
              )
            )
          )
        )
      )
    )
  )
) 

set thddrv=!osssddrv! !osthddrv! !osusbhddrv! !ssddrv! !thddrv! !usbhddrv!

if defined syshddrv (
  set thddrv=!thddrv:%syshddrv%=!
  set thddrv=!syshddrv! !thddrv!
)

set allhddrv=!thddrv! !firadrv! !winvdrv!
if defined allhddrv set allhddrv=!allhddrv: =!
if defined allhddrv set allhddrv=!allhddrv::=: !

if defined virdrv (
  if defined allhddrv (
    for %%a in (!virdrv!) do (
      for %%b in (!allhddrv!) do (
        if %%a equ %%b (
          set hdvir=!hdvir! %%a
          set virdrv=!virdrv:%%a=!
        )
      )
    )
  )
)

if defined hdvir echo No mount point hard disk drive letters: !hdvir!

if defined allhddrv (
  set mthddrv=!allhddrv!
  if defined mtdrv (
    set otherdrv=!mtdrv!
    for %%a in (!mthddrv! !virdrv!) do set otherdrv=!otherdrv:%%a=!
    if defined otherdrv (
      set otherdrv=!otherdrv: =!
      if defined otherdrv set otherdrv=!otherdrv:%systemdrive%=!
      if defined otherdrv (
        set otherdrv=!otherdrv::=: !
        echo Other mounted drive letters: !otherdrv!
      )
    )
  )
)

:: Get all removable drive letters and USBCDROM letters of a flash disk. added on 2022-02-15

for /f "delims=" %%x in ('reg query HKLM\SYSTEM\CurrentControlSet\services\disk\enum /s /t reg_sz 2^>nul ^|find "USBSTOR"') do (
  set longspec=%%x
  rem echo !longspec!
  set firstdrv=
  set rmdrv=
  set allrmdrv=
  set dipath=
  set diskid=
  set /a usbno+=1
  
  if defined longspec (
    rem get specific string
    for /f "tokens=3 delims=\" %%a in ("!longspec!") do set spechar=%%a
    rem echo spechar=!spechar!
    set spechar=!spechar:~0,63!
    call :str2hex "!spechar!"
    set hexspechar=!result!
    set result=
    rem echo hexspechar=!hexspechar!

    Rem find first partition drive letter if exist   
    for /f "delims= " %%a in ('reg query HKLM\SYSTEM\MountedDevices /f "!hexspechar!" 2^>nul ^|find "DosDevices"') do (
      set firstpdrv=%%a
      set firstpdrv=!firstpdrv:~-2!
      rem echo firstpdrv=!firstpdrv! 000
      if defined firstpdrv (
        rem to classify firstdrv type, USBCDROM or storage drive.
        for /f "skip=2 tokens=3 delims=	 " %%a in ('reg query HKLM\SYSTEM\MountedDevices /v \DosDevices\!firstpdrv! 2^>nul') do (
          set "mnreg=%%a"
          rem echo mnreg=!mnreg!
          if defined mnreg (
            set ucdufp=!mnreg:~48,16!
            if !ucdufp! equ 4400690073006B00 (
             mountvol !firstpdrv! /l >nul && set firstdrv=!firstpdrv!
            )
          )
        )
      )
    )

    Rem get USB storage drive letter by diskid
    rem echo spechar=!spechar!
    for /f %%a in ('reg query HKLM\SYSTEM\CurrentControlSet\enum\USBSTOR\ /f "!spechar!" /s /k 2^>nul ^|find "\"') do set dipath=%%a
    rem echo dipath1=!dipath!
    if defined dipath (
      set dipath=!dipath:HKEY_LOCAL_MACHINE^\=!
      rem echo dipath2=!dipath!
      for /f "delims=" %%q in ('reg query "HKLM\!dipath!\Device Parameters\Partmgr" /v diskid 2^>nul ^|find "diskid"') do set diskid=%%q
      if defined diskid (
        set diskid=!diskid:~-38!
        rem echo diskid=!diskid!
        call :str2hex "!diskid!"
        set hexdiskid=!result!
        set result=
        rem echo hexdiskid=!hexdiskid!
        for /f "delims= " %%a in ('reg query HKLM\SYSTEM\MountedDevices /f "!hexdiskid!" 2^>nul ^|find "DosDevices"') do (
          rem echo %%a
          set tmprmdrv=%%a
          set tmprmdrv=!tmprmdrv:~-2!
          if defined tmprmdrv (
            mountvol !tmprmdrv! /l >nul && set rmdrv=!rmdrv! !tmprmdrv!
          )
        )
      )
      Rem combine first drv and other drv of flash disk
      set allrmdrv=!firstdrv! !rmdrv!
      if defined allrmdrv set allrmdrv=!allrmdrv: =!
      if defined allrmdrv set allrmdrv=!allrmdrv::=: !
      if defined allrmdrv (
        echo Flash disk !usbno! drive letter: !allrmdrv!
        set tallrmdrv=!tallrmdrv! !allrmdrv!
      ) else (
        echo USB device !usbno! is not a flash disk or has no drive letter or is a USB CDROM .
      )
    )
  )
)

:: Get other drv type.
if defined otherdrv (
  for %%a in (%otherdrv%) do (
    set mnreg=
    set tpcode=
    set drvcode=
    set prodcode=
    for /f "skip=2 tokens=3 delims=	 " %%b in ('reg query HKLM\SYSTEM\MountedDevices /v \DosDevices\%%a 2^>nul') do (
      set "mnreg=%%b"
      if defined mnreg (
        set first8=!mnreg:~0,8!

       if !first8! equ 444D494F (         
         set gprm=!gprm! %%a
         echo Removable GPT falsh disk drive letters: %%a
       )
        set docheck=
        if !first8! equ 5F003F00 set docheck=ok
        if !first8! equ 5C003F00 set docheck=ok
        if /i !docheck! equ ok (
          set mnreg=!mnreg:5C=#!
          set mnreg=!mnreg:5f=#!
          set mnreg=!mnreg:23=#!
          set mnreg=!mnreg:26=#!
          set mnreg=!mnreg:00=!
          for /f "tokens=2,3,7 delims=#" %%c in ("!mnreg!") do (
            set tpcode=%%c
            set drvcode=%%d
            set prodcode=%%e
            rem FDC
            if /i !tpcode! equ 464443 (
              set intflp=!intflp! %%a
              echo Internal Floppy Drive Letter: %%a
            )
            rem DAEMON TOOLS
            if /i !tpcode! equ 4454534F4654425553 (
              set dtdvd=!dtdvd! %%a
              echo Daemon Tools CD/DVD Drive Letter: %%a
            )
            rem IDE
            if /i !tpcode! equ 494445 (
              if /i !tpcode! neq !tpcode:4364526F6D=! (
                set idedvd=!idedvd! %%a
                echo IDE CD/DVD ROM: %%a
              )
            )
            rem SCSI
            if /i !tpcode! equ 53435349 (
              if /i !drvcode! neq !drvcode:4364526F6D=! (
                if /i !prodcode! equ 5669727475616C (
                  echo SCSI Virtual DVD ROM: %%a
                  set msvirdvd=!msvirdvd! %%a
                ) else (
                  echo SCSI CD/DVD ROM: %%a
                  set scsidvd=!scsidvd! %%a
                )
              )
            )
            rem USBSTOR        
            if /i !tpcode! equ 55534253544F52 (
              if /i !drvcode! equ 4364526F6D (
                set usbdvd=!usbdvd! %%a
                echo USB CD/DVD ROM: %%a
              )
            )
            rem STORAGE, also removable USB
            if /i !tpcode! equ 53544F52414745 (
              if /i !drvcode! equ 52656D6F7661626C654D65646961 (
                set usbrem=!usbrem! %%a
                echo Removable USB "Storage" Drive letter: %%a
              )
            )  
            rem FIRA_DISK  
            if /i !tpcode! equ 464952414449534B (
              if /i !drvcode! equ 53466C6F707079 (
                set firaflp=!firaflp! %%a
                echo FIRA Floppy Disk Drive letter: %%a
              )
              if /i !drvcode! equ 4344524F4D (
                set firadvd=!firadvd! %%a
                echo FIRA CD/DVD ROM: %%a
              )
            )
            rem WINV_BLOCK
            if /i !tpcode! equ 57696E56426C6F636B (
              if /i !drvcode! equ 52414D466C6F7070794469736B (
                set winvflp=!winvflp! %%a
                echo Winv Block Ram Floppy Disk Drive letter: %%a
              )
              if /i !drvcode! equ 52414D4F70746963616C44697363 (
                set wvbkdvd=!wvbkdvd! %%a
                echo Winv Block Ram CD/DVD ROM: %%a
              )
              if /i !drvcode! equ 46696C654F70746963616C44697363 (
                set wvbkdvd=!wvbkdvd! %%a
                echo Winv Block File CD/DVD ROM: %%a
              )
            )
            if /i !drvcode! equ 53466C6F707079 (
              set ufddrv=!ufddrv! %%a
              echo USB Floppy Drive Letter: %%a
            )
          )
        )
      )
    )
  )
)


rem Fix the only USB CDROM drive letter.
if defined usbdvd set usbdvd=!usbdvd: =!
if defined usbdvd set usbdvd=!usbdvd::=: !

set aucd=
if defined usbdvd (
  for %%a in (!usbdvd!) do set /a aucd+=1
)

if !aucd! equ 1 (
  if "!usbdvd!" neq "%systemdrive% " (
    if defined USBCDROM (
      if /i "!usbdvd!" neq "!USBCDROM!: " (
        for /f %%b in ('mountvol !usbdvd! /l') do (
          mountvol !usbdvd! /d
          mountvol !USBCDROM!: %%b
          echo The only USB CDROM drive letter was set to !USBCDROM!:
        )
      )
      set otherdrv=!otherdrv:%usbdvd%=!
      set usbdvd=
    )
  )
)

set flpdrv=!intflp!!ufddrv!!firaflp!!winvflp!
set dvddrv=!scsidvd!!idedvd!!usbdvd!!msvirdvd!!dtdvd!!firadvd!!wvbkdvd!
set tmbgprm=!tallrmdrv!!gprm!!usbrem!

if defined dvddrv (
  set dvddrv=!dvddrv: =!
  if defined dvddrv (
    set dvddrv=!dvddrv::=: !
    if defined dvddrv (
      for %%a in (!dvddrv!) do (
        set otherdrv=!otherdrv:%%a=!
        rem echo otherdrv=!otherdrv!
      )
    )
  )
)

rem Fix the first flash disk drive letter.

if defined tmbgprm set tmbgprm=!tmbgprm: =!
if defined tmbgprm set tmbgprm=!tmbgprm::=: !

set aurm=
if defined tmbgprm (
  for %%a in (!tmbgprm!) do set /a aurm+=1
)

if !aurm! equ 1 (
  if /i "!tmbgprm!" neq "%systemdrive% " (
    if defined USBDRV (
      if /i "!tmbgprm!" neq "!USBDRV!: " (
        for /f %%b in ('mountvol !tmbgprm! /l') do (
          mountvol !tmbgprm! /d
          mountvol !USBDRV!: %%b
          echo The only Flash drive letter was set to !USBDRV!:
        )
      )
      set otherdrv=!otherdrv:%tmbgprm%=!
      set tmbgprm=
    )
  )
)

if defined tmbgprm (
  for %%a in (!tmbgprm!) do (
    set otherdrv=!otherdrv:%%a=!
    rem echo otherdrv=!otherdrv!
  )
)

if defined otherdrv set otherdrv=!otherdrv: =!
if defined otherdrv set otherdrv=!otherdrv::=: !
if defined otherdrv (
  echo Unrecognized drive letter: !otherdrv! and these letters will not be ordered.
  echo Please report this to me. QQ: 2511755859. 
  set virdrv=!virdrv! !otherdrv!
)

set virdrv=!virdrv! !flpdrv!
if defined virdrv (
  set virdrv=!virdrv: =!
  if defined virdrv (
    set virdrv=!virdrv::=: !
  )
)

echo SYSTEM DRIVE LETTER: %systemdrive%

set totaldrv=%allhddrv% %dvddrv% %tmbgprm%
set totaldrv=!totaldrv:%systemdrive%=!

if defined virdrv (
  for %%a in (!virdrv!) do set totaldrv=!totaldrv:%%a=!
  set virdrv=!virdrv::=!
)

if defined totaldrv (
  if defined totaldrv set totaldrv=!totaldrv: =!
  if defined totaldrv set totaldrv=!totaldrv::=: !
  if defined totaldrv set totaldrv=!totaldrv::=!
)

set fulldrv=CDEFGHIJKLMNOPQRSTUVWXYZ

for %%a in (%virdrv%) do set fulldrv=!fulldrv:%%a=!
set fulldrv=!fulldrv:%systemdrive:~0,1%=!

::ordering drive letters start
set /a kk=-1
for %%a in (%totaldrv%) do (
  set /a kk=kk+1
  call :olddrv %%a
)

if defined ordereddrv (
  if /i !AUTO! equ n (
    echo DRIVE LETTERS to be ordered: %ordereddrv:~0,-1%
    set /p doorder="Are you sure to make above drive letter changes (Y/N)"
    if /i !doorder! equ y (
      call :mountbegin
      if %errorlevel% equ 0 (
        echo "Drive letter changing succeeded!"
      ) else (
        echo "Error happened, please check no drive letter used when changing begin!"
      )
    ) else (
      echo Drive letter changing cancelled by user!
    )
  ) else (
    call :mountbegin
  )
) else (
  echo DRIVE LETTER ORDER IS CORRECT.
)

if %~d0 neq %systemdrive% (
  if exist %temp%\drvtype.exe del %temp%\drvtype.exe
  if exist %temp%\smartctl.exe del %temp%\smartctl.exe
  if exist %temp%\fsutil.exe del %temp%\fsutil.exe
  if exist %temp%\mountvol.exe del %temp%\mountvol.exe
  if exist %temp%\%~nx0 del %temp%\%~nx0
)

pushd %~dp0

if !AUTO! equ n (
  goto :end

) else (
  goto :end
)
goto :end

:olddrv
set nk=!fulldrv:~%kk%,1!
if /i %1 neq !nk! (
  set ordereddrv=!ordereddrv! %1: to !nk!:,
)
goto :eof

:mountbegin
set ordereddrv=!ordereddrv:to=!
set ordereddrv=!ordereddrv: =!
set ordereddrv=!ordereddrv:,= !

for %%a in (!ordereddrv!) do (
  set oldnew=%%a
  call :vid_drv !oldnew:~0,1! !oldnew:~-2,1!
)

for %%a in (!ordereddrv!) do (
  set oldnew=%%a
  mountvol !oldnew:~0,2! /d
)

for %%a in (!newvid!) do (
  set eachnewvid=%%a
  set tmpeachnewvid=!eachnewvid:~0,1!
  if !tmpeachnewvid! neq { (
    pecmd show !eachnewvid:~0,-1!,!eachnewvid:~-1!
  ) else (
    mountvol !eachnewvid:~-1!: \\?\Volume!eachnewvid:~0,-1!\
  )
)

goto :eof

:dsptdrv
set dpd=
for %%a in (!order!) do (
  set eachdpd=%%a
  set tmpeachdpd=!eachdpd:%1=!
  if !eachdpd! neq !tmpeachdpd! (
    if !eachdpd:~-1! neq $ (
      set dpd=!eachdpd:~0,-2!
    ) else (
      set dpd=!eachdpd:~0,-3!
    )
  )
)
goto :eof

:vid_drv
set aa=%1
if defined hdvir (
  set tmphdvir=!hdvir:%1=!
  if !hdvir! neq !tmphdvir! (
    call :dsptdrv !aa!
    set newvid=!newvid! !dpd!%2
  ) else (
    for /f %%b in ('mountvol %1: /l') do (
      set tmp=%%b
      set tmp=!tmp:\\?\Volume=!
      set tmp=!tmp:\=!
      set newvid=!newvid! !tmp!%2
    )
  ) 
) else (
  for /f %%c in ('mountvol %1: /l') do (
    set tmp=%%c
    set tmp=!tmp:\\?\Volume=!
    set tmp=!tmp:\=!
    set newvid=!newvid! !tmp!%2
  )
)
goto :eof

:fsutil
      for /f "delims=:\ " %%a in ('fsutil.exe fsinfo drives^|more') do (
        set tmpdrv=%%a
        if !tmpdrv:~-2! equ %%a set ALLDRV=!ALLDRV! %%a:
      )
goto :eof

:str2hex
set str=%1
set str=!str:"=!
set /a cn=0
set result=

:cstart
set ic=!str:~%cn%,1!
set /a cn+=1
if not defined ic goto :end
if defined ic (
  if "!ic!" equ "!" set hexstr=21 & goto :eof
  rem if "!ic!" equ """ set hexstr=22
  if "!ic!" equ "#" set hexstr=23
  if "!ic!" equ "$" set hexstr=24
  if "!ic!" equ "%" set hexstr=25
  if "!ic!" equ "&" set hexstr=26
  if "!ic!" equ "'" set hexstr=27
  if "!ic!" equ "(" set hexstr=28
  if "!ic!" equ ")" set hexstr=29
  if "!ic!" equ "*" set hexstr=2A
  if "!ic!" equ "+" set hexstr=2B
  if "!ic!" equ "," set hexstr=2C
  if "!ic!" equ "-" set hexstr=2D
  if "!ic!" equ "." set hexstr=2E
  if "!ic!" equ "/" set hexstr=2F
  if "!ic!" equ "0" set hexstr=30
  if "!ic!" equ "1" set hexstr=31
  if "!ic!" equ "2" set hexstr=32
  if "!ic!" equ "3" set hexstr=33
  if "!ic!" equ "4" set hexstr=34
  if "!ic!" equ "5" set hexstr=35
  if "!ic!" equ "6" set hexstr=36
  if "!ic!" equ "7" set hexstr=37
  if "!ic!" equ "8" set hexstr=38
  if "!ic!" equ "9" set hexstr=39
  if "!ic!" equ ":" set hexstr=3A
  if "!ic!" equ ";" set hexstr=3B
  if "!ic!" equ "<" set hexstr=3C
  if "!ic!" equ "=" set hexstr=3D
  if "!ic!" equ ">" set hexstr=3E
  if "!ic!" equ "?" set hexstr=3F
  if "!ic!" equ "@" set hexstr=40
  if "!ic!" equ "A" set hexstr=41
  if "!ic!" equ "B" set hexstr=42
  if "!ic!" equ "C" set hexstr=43
  if "!ic!" equ "D" set hexstr=44
  if "!ic!" equ "E" set hexstr=45
  if "!ic!" equ "F" set hexstr=46
  if "!ic!" equ "G" set hexstr=47
  if "!ic!" equ "H" set hexstr=48
  if "!ic!" equ "I" set hexstr=49
  if "!ic!" equ "J" set hexstr=4A
  if "!ic!" equ "K" set hexstr=4B
  if "!ic!" equ "L" set hexstr=4C
  if "!ic!" equ "M" set hexstr=4D
  if "!ic!" equ "N" set hexstr=4E
  if "!ic!" equ "O" set hexstr=4F
  if "!ic!" equ "P" set hexstr=50
  if "!ic!" equ "Q" set hexstr=51
  if "!ic!" equ "R" set hexstr=52
  if "!ic!" equ "S" set hexstr=53
  if "!ic!" equ "T" set hexstr=54
  if "!ic!" equ "U" set hexstr=55
  if "!ic!" equ "V" set hexstr=56
  if "!ic!" equ "W" set hexstr=57
  if "!ic!" equ "X" set hexstr=58
  if "!ic!" equ "Y" set hexstr=59
  if "!ic!" equ "Z" set hexstr=5A
  if "!ic!" equ "[" set hexstr=5B
  if "!ic!" equ "\" set hexstr=5C
  if "!ic!" equ "]" set hexstr=5D
  if "!ic!" equ "^" set hexstr=5E
  if "!ic!" equ "_" set hexstr=5F
  if "!ic!" equ "`" set hexstr=60
  if "!ic!" equ "a" set hexstr=61
  if "!ic!" equ "b" set hexstr=62
  if "!ic!" equ "c" set hexstr=63
  if "!ic!" equ "d" set hexstr=64
  if "!ic!" equ "e" set hexstr=65
  if "!ic!" equ "f" set hexstr=66
  if "!ic!" equ "g" set hexstr=67
  if "!ic!" equ "h" set hexstr=68
  if "!ic!" equ "i" set hexstr=69
  if "!ic!" equ "j" set hexstr=6A
  if "!ic!" equ "k" set hexstr=6B
  if "!ic!" equ "l" set hexstr=6C
  if "!ic!" equ "m" set hexstr=6D
  if "!ic!" equ "n" set hexstr=6E
  if "!ic!" equ "o" set hexstr=6F
  if "!ic!" equ "p" set hexstr=70
  if "!ic!" equ "q" set hexstr=71
  if "!ic!" equ "r" set hexstr=72
  if "!ic!" equ "s" set hexstr=73
  if "!ic!" equ "t" set hexstr=74
  if "!ic!" equ "u" set hexstr=75
  if "!ic!" equ "v" set hexstr=76
  if "!ic!" equ "w" set hexstr=77
  if "!ic!" equ "x" set hexstr=78
  if "!ic!" equ "y" set hexstr=79
  if "!ic!" equ "z" set hexstr=7A
  if "!ic!" equ "{" set hexstr=7B
  if "!ic!" equ "|" set hexstr=7C
  if "!ic!" equ "}" set hexstr=7D
  if "!ic!" equ "~" set hexstr=7E  
  set result=!result!!hexstr!00
)
goto :cstart

:end