I’ve a batch script in which I loop trough all the filenames in a folder and then I invoke a command in order to remove the extension from the filename. I’d like to store at every iteration the filename (without extension) in a variable named result for later reuse.
The RemoveExtension function works fine. However I’m not able to retrieve the result and store it in the _result variable. When I print it, it’s always empty. Thanks for your help!
@echo OFF
SETLOCAL EnableDelayedExpansion
set "_result="
for /f "delims=" %%i in ('dir "%~1\*.txt" /b') DO (
echo.filepath: "%~1\%%i"
call :RemoveExtension "%%i"
echo._result: "%_result%" // The "_result" variable is always EMPTY ""
)
goto :eof
:RemoveExtension
SETLOCAL
REM echo "%~1"
set "filename=%~1"
:loop
if "%filename:~-1%" NEQ "." (
set "filename=%filename:~0,-1%
goto :loop
)
set "filename=%filename:~0,-1%"
echo "%filename%"
ENDLOCAL & set "_result=%filename%"
goto :eof
Try this…much simpler way to remove the extension. Because you are changing
_resultinside aFORloop, you need to access it using!instead of%.