Archont
Archont

Reputation: 41

Batch cutting result string from command

How to properly cut result from command in batch? I wrote something like that:

@ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
    @ECHO %%F%:~7,4%
)
ENDLOCAL

In my case "whoami" returns "europe\archont". After running this batch I receive "europe\archont:~7,4" instead of "arch" Where I did mistake?

Upvotes: 0

Views: 638

Answers (3)

Stephan
Stephan

Reputation: 56238

instead of relying on fixed string length, just use the \ as delimiter:

for /f "tokens=2 delims=\" %%F in ('whoami') do set "im=%%F"

when needed, you can cut that:

echo %im:~,4%

Upvotes: 0

Compo
Compo

Reputation: 38719

If you have no further functions to perform within your For loop:

@Echo Off
For /F "Delims=" %%A In ('WhoAmI') Do Set "IAm=%%~nxA"
Echo(%IAm:~,4%
Timeout -1

Upvotes: 1

china.gaofeng
china.gaofeng

Reputation: 55

@ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
    set "str=%%F"
    @ECHO !str:~7,4!
)
ENDLOCAL

Upvotes: 0

Related Questions