1

I have programs with some of them having 64bit versions: foo.exe bar.exe bar64.exe etc. So wanted to extract 2 last characters from a filename (without extension) and do something then...

@echo off
setlocal enableextensions enabledelayedexpansion

for /f "tokens=1,2 delims=." %%G in ('dir /b *.exe') do (
set _test=%%G
set _result=!_test:~-2!

echo !_result!
)

endlocal
exit

This works well if the number of characters to extract IS NOT 2. If it is 2 then echo goes crazy. Is it me doing things wrong or some bug?

2
  • Please define "goes crazy." What does it do that you don't want it to do, or not do that you want it to do, and with what data? Commented May 3, 2017 at 13:30
  • At some point it starts to print out part of the code: > ( set _test=bar64 set _result=!_test:~-2! echo !_result! ) 64 Commented May 3, 2017 at 13:39

1 Answer 1

1

You did use the whole file name, since .exe is four chars the error you describe shouldn't occur. To avoid unexpected behavior simple prepend the name with any two chars.

@echo off&setlocal enableextensions enabledelayedexpansion
for /f "tokens=*" %%G in ('dir /b *.exe') do (
    set "_test=__%%~nG"
    set _result=!_test:~-2!
    echo:!_result!
)

Edit To avoid echo status being reported, use a different command separator than a space.

Sign up to request clarification or add additional context in comments.

2 Comments

This actually didn't work, but gave a helpful hint - removed "delims=." to get the filename with extension and then extraction of last 6 chars succeeded. So it does have something to do with dots and extensions. Thanks.
Found the problem :) One filename ended with "on" which triggered ECHO ON. LOL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.