運行腳本時我得到奇怪的輸出:
@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
它應該顯示我選擇的資料夾位置。相反,我得到迴聲已關閉。
答案1
運行腳本時我得到奇怪的輸出
若要偵錯批次腳本,請註解該@echo off
行,以便您可以看到發生了什麼:
rem @echo off
你的setlocal enabledelayedexpansion
位置不對。將其移動到文件的開頭。
測試.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
範例輸出(我選擇“桌面”):
F:\test>test
C:\Users\DavidPostill\Desktop
F:\test>
筆記:
ECHO is off.
如果您在第一次顯示對話方塊時按“取消”,仍會顯示您的資訊。