0

I have a batch script which should go to several files and change it's content. Now I save a top directory as REPO_DIR and call a function.

cd %REPO_DIR%

echo Before call %cd%
CALL :update_config_xml "packages\com.company.sft\meta", package.xml, %mainapp_version%
echo After call %cd%
EXIT /B %ERRORLEVEL% 

That function should change the directory and do the change and should set the current directory back to REPO_DIR.

:update_config_xml
echo  Received directory  %~1
cd %~1
echo  Now cd within the function %cd%

@echo off
setlocal EnableDelayedExpansion
(FOR /F "delims=" %%a in (%~2) do (
set "line=%%a"
set "newLine=!line:Version>=!"
if "!newLine!" neq "!line!" (
  set "newLine=<Version>%~3</Version>"
)
echo !newLine!
))> temp.xml

del %~2
ren temp.xml %~2

cd %REPO_DIR%
echo Now cd is %cd%

EXIT /B 0

My outfit file is something like this;

Before call E:\company\Repo-build
Received directory  packages\com.company.sft\meta
Now cd within the function E:\company\Repo-build\packages\com.company.sft\meta
Now cd is E:\company\Repo-build
After call E:\company\Repo-build\packages\com.company.sft\meta

The last line is the problem. I cannot understand how it automatically changes back to the directory which was changed by the function.

Note that I have tried everything I could find here. I tried pushd and popd too. Got the same result. I don't think It will be a good solution to cd after every time I call this function.

Hareen Laks
  • 1,432
  • 2
  • 17
  • 33
  • 3
    You used `setlocal EnableDelayedExpansion` within your called label, and changed directory. When the script meets the `EXIT /B 0` command, an implicit closing `endlocal` is applied by the interpreter. When your endlocal is applied the current directory will return to that which was applied before your `setlocal`, _(in this case `%1`)_. The fix is therefore to add an `EndLocal` command, prior to using the `cd %REPO_DIR%` command. – Compo Jun 01 '20 at 09:11
  • @Compo, Thank you very much. I understood it pretty well. – Hareen Laks Jun 02 '20 at 09:57

0 Answers0