1

I'm getting strange output when running my script:

@echo off
setlocal
pushd "%~dp0"

set Mode=batch

if "%Mode%"=="batch" call :BATCH
echo %Loc%
pause>nul
exit

:BATCH
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
set Loc=!folder!
Goto:eof

It should be displaying the folder location I select. instead I get echo is off.

DavidPostill
  • 162,382

1 Answers1

0

I'm getting strange output when running my script

To debug your batch scripts comment the @echo off line so you can see what is happening:

rem @echo off

Your setlocal enabledelayedexpansion is in the wrong place. Move it to the beginning of the file.

test.cmd:

@echo off
setlocal
setlocal enabledelayedexpansion
pushd "%~dp0"

set Mode=batch

if "%Mode%"=="batch" call :BATCH
echo %Loc%
pause>nul

:BATCH
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
set Loc=!folder!
Goto:eof

Example output (I selected "Desktop"):

F:\test>test
C:\Users\DavidPostill\Desktop

F:\test>

Note:

  • You will still get ECHO is off. displayed if you press "Cancel" the first time your dialog is displayed.
DavidPostill
  • 162,382