1

I have 2 issues with my Batch file, I think they are due to EnableDelayedExpansion.

I am basing my script in this post from SO.

I need EnableDelayedExpansion for another part of my script, so I need to keep it.

This is my script:

@echo off
set myPath=Subfolder1
set folderList=input.txt
set originalPath=%~dp0  

cd %myPath%
setlocal EnableDelayedExpansion

:process

for /F "tokens=*" %%S in (%~dp0\%folderList%) do (
    echo Folder %%S
    REM echo Folder %%S prints: Folder folderName
    set testPath=C:\BatchTests\%%S\
    echo test path: %testPath%
    REM echo test path: %testPath% prints: test path:
)
echo %originalPath%
REM echo %originalPath% prints: C:\BatchTests\
cd %originalPath%
pause

testPath seems to be always empty, and the line cd %originalPath%.

What am I doing wrong? What is the right way to use/set testPath?

The second issue I am having is different, so I opened a separate question here.

Community
  • 1
  • 1
Dzyann
  • 5,062
  • 11
  • 63
  • 95
  • 5
    Try it with `!testPath!` – jeb Nov 01 '15 at 21:25
  • You do not need to store the working directory in a variable when using `pushd %myPath%` to change it temporarily and `popd` to restore the original one... – aschipfl Nov 01 '15 at 21:29
  • @jeb - That works! is that another way to use the variable? – Dzyann Nov 01 '15 at 21:30
  • @aschipfl - I didnt know that technique, but it doesnt work either :S – Dzyann Nov 01 '15 at 21:32
  • Supposing `Subfolder1` (stored in `%myPath%`) exists and is accessible, this does work; note that the batch file parent directory `%~dp0` and the current working directory are _not_ the same thing; to query the latter, use `%CD%`... – aschipfl Nov 01 '15 at 21:36
  • 2
    @Dzyann That is the point of using delayed expansion, exclamation mark expansion i enabled by `seltlocal EnableDelayedExpansion` see also `set /?` – jeb Nov 01 '15 at 21:38
  • @aschipfl - the pushd works, it moves to the new directory, but the popd doesnt, so it stays in the new directory. – Dzyann Nov 01 '15 at 21:39
  • @jeb - Thanks a lot! If you post that I can accept your answer. – Dzyann Nov 01 '15 at 21:41
  • @Dzyann, `popd` _is_ restoring the former directory, but I just recognized that `setlocal` also stores the current directory; after the batch file finished execution, an implicit `endlocal` is done, so the most recent directory _before_ `setlocal` is restored... sorry for confusion!! so just put `cd %myPath%` down below `setlocal` and everything is fine... – aschipfl Nov 01 '15 at 21:57
  • 1
    @aschipfl - I realize now that both issues are different, so i posted a different question for the issue you addressed. If you post your answer there I will accept it. Thank you!: [my new question](http://stackoverflow.com/questions/33484008/why-is-my-cd-myvar-being-ignored) – Dzyann Nov 02 '15 at 18:04
  • Alright, @Dzyann; feel free to accept the **best** answer there... ;-) – aschipfl Nov 02 '15 at 21:36

1 Answers1

1

To use delayed expansion in batch you need to parts.

First you have to enable it with setlocal EnableDelayedExpansion.
And then you can expand any variable with exclamation marks instead of percent signs.

setlocal EnableDelayedExpansion
set var=origin
(
  set var=New Value
  echo Percent: %var%
  echo delayed: !var!
)

The output is

Percent: origin
delayed: New Value

Percent expansion is evaluated when a command or block is parsed.
Delayed expansion is evaluated when a command is executed.

jeb
  • 78,592
  • 17
  • 171
  • 225