Last active
November 26, 2018 22:40
-
-
Save marckassay/42c3e37d799fab7519e00400d7164526 to your computer and use it in GitHub Desktop.
Batch script that resolves its symlink's value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: this batch script is intended to get the value of a symlink, so that when the script is executed | |
:: it will resolve the value of the symlink, assigned to '_convertedresult'. this is my first batch | |
:: script coded, so this is probably rubbish to some and unstable too. also very slow in parsing the | |
:: final var, '_convertedresult', i've developed this on Windows 10. also this doesn't rely on any | |
:: additional executables that may or may not be on the computer executing this script. | |
:: | |
:: source: https://gist.github.com/marckassay/42c3e37d799fab7519e00400d7164526 | |
@ECHO OFF | |
@SETLOCAL | |
SETLOCAL enableDelayedExpansion | |
:: skip the first 7 lines of `fsutil reparsepoint query %0`. Even columns 2-16 contains the | |
:: hexadecimal chars we want versus '00'. | |
FOR /F "USEBACKQ tokens=2,4,6,8,10,12,14,16 skip=7" %%G IN (`fsutil reparsepoint query %0`) DO ( | |
SETLOCAL enableDelayedExpansion | |
SET MYVAR=%%G%%H%%I%%J%%K%%L%%M%%N | |
SET _result=!_result!!MYVAR! | |
) | |
:: trim the first 12 chars which seems to be attributes that pertains to this file. These are not | |
:: needed for this script's objective | |
SET _result=!_result:~12! | |
:: replace on '5c3f3f' (\??) to 'Z' so that the FOR /F can use delims=Z. FOR loop's delims option | |
:: is limited to 1 char and the char 'Z' isn't valid for a hexadecimal value, so here its used as | |
:: conveniently as a token. | |
SET _result=!_result:5c3f3f=Z! | |
:: split _result on the 'Z' token and assign the left side to _result | |
FOR /F "delims=Z" %%a IN ("%_result%") DO (SET _result=%%a) | |
ECHO Hexadecimals of resolved symlink value: | |
ECHO !_result! | |
:: code related to the ':loop' function, have been modified from this ref: | |
:: https://stackoverflow.com/a/15009416/648789 | |
:: string terminator: chose something that won't show up in the input file | |
SET strterm=___ENDOFSTRING___ | |
:: add string terminator to input | |
SET tmp=%_result%%strterm% | |
:loop | |
:: get first 2 characters from input | |
SET char=%tmp:~0,2% | |
:: remove first 2 characters from input | |
SET tmp=%tmp:~2% | |
:: format char to hexadecimalchar | |
SET /a hexadecimalchar=0x%char% | |
:: exit should set 'exitcodeAscii' dynamic var | |
CMD /c exit %hexadecimalchar% | |
:: append value of 'exitcodeAscii' to _convertedresult | |
SET _convertedresult=!_convertedresult!!=exitcodeAscii! | |
IF NOT "%tmp%" == "%strterm%" GOTO loop | |
ECHO Convert Hexadecimals to chars: | |
ECHO !_convertedresult! | |
ENDLOCAL | |
@ECHO ON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment