The value of %sourcedir% is a relative path (input), which is going to be related to the current working directory (%CD%), which is not the same as the parent directory of the script (which is referred to by %~dp0.).
The dir /B command (without /S option) always returns pure file names, even when you specify a longer path (like %sourcedir%\*.jpg), so %%G is assumed to be in the current working directory. Hence you will need to use %sourcedir%\%%G instead.
Here is a working suggestion:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Specify sub-directory of parent directory of this script:
set "sourcedir=%~dp0input"
for /F "eol=| delims=" %%G in ('dir /B /A:-D "%sourcedir%\*.jpg" 2^> nul') do (
rem /* Avoid expansion of `for` meta-variables during delayed expansion in order to
rem avoid loss of `!` (though this is not an issue with the file names at hand): */
set "filename=%%G" & set "basename=0%%~nG" & set "ext=%%~xG"
setlocal EnableDelayedExpansion
ren "!sourcedir!\!filename!" "!basename:~-2!!ext!"
endlocal
)
To avoid multiple specifications of the directory path use cd /D to specify it once:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Specify sub-directory of parent directory of this script:
set "sourcedir=%~dp0input"
cd /D "%sourcedir%" && (
for /F "eol=| delims=" %%G in ('dir /B /A:-D "*.jpg" 2^> nul') do (
set "filename=%%G" & set "basename=0%%~nG" & set "ext=%%~xG"
setlocal EnableDelayedExpansion
ren "!filename!" "!basename:~-2!!ext!"
endlocal
)
)
Or alternatively pushd/popd:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Specify sub-directory of parent directory of this script:
set "sourcedir=%~dp0input"
pushd "%sourcedir%" && (
for /F "eol=| delims=" %%G in ('dir /B /A:-D "*.jpg" 2^> nul') do (
set "filename=%%G" & set "basename=0%%~nG" & set "ext=%%~xG"
setlocal EnableDelayedExpansion
ren "!filename!" "!basename:~-2!!ext!"
endlocal
)
popd
)