Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 用于设置变量的公共批处理文件_Batch File - Fatal编程技术网

Batch file 用于设置变量的公共批处理文件

Batch file 用于设置变量的公共批处理文件,batch-file,Batch File,我正在处理多个批处理文件,希望它们共享一些变量,因此我创建了一个批处理文件,其中包含所有这些设置SetupEnv: rem General setup :: To pause or not after running a batch file SET isPause = true :: The directory where your source code is located SET directory = D :: The folders where your primary &

我正在处理多个批处理文件,希望它们共享一些变量,因此我创建了一个批处理文件,其中包含所有这些设置
SetupEnv

rem General setup
:: To pause or not after running a batch file
SET isPause = true

:: The directory where your source code is located
SET directory = D

:: The folders where your primary & secondary source code is located
:: I like to have two source code folders, if you don't then just have them pointing to the same folder
SET primary_source_code = \Dev\App
SET secondary_source_code = \Dev\App2

:::::::::::::::::::::::::::::::::::::::::::: XAMPP :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rem If you're using XAMPP then set these up
:: Your destination folder
SET base_destination = C:\xampp\htdocs

:: The base url that is pointing to your destination folder (in most cases it's localhost)
SET base_url = http://10.0.2.65

:::::::::::::::::::::::::::::::::::::::::: Angular :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rem If you're using angular set these up
:: The folder where you built code is copied
SET build_file = dist
从另一个批处理文件中,我首先调用该文件:

::setup
call ../SetupEnv

echo %directory% dir
pause;
问题是,即使文件运行平稳,并且我可以在输出中看到正在设置的内容,变量也不会出现在我调用它的文件中。因此,在该示例中,
%directory%
未被打印

编辑 我还尝试使用:


但这也不起作用,
%directory%
也没有打印出来

调用中设置变量
ed批处理文件可以工作,只要在被调用的批处理文件中不使用
setlocal
(当它返回时会有一个隐式
endlocal
,因此变量会丢失):

对于解决方案的备选方案
,我将其稍微更改为:

for /f "delims=" %%a in ('type b.bat^|findstr /bic:"set "') do %%a
这将只“执行”以
set
开头的行(忽略大小写),因此您可以在该文件中保留任何注释


注:
。。。do set“%%a”
将另一个
set
添加到行中(文件中已经有一个),导致
set“set var=value”
,这显然是您不想要的。

可能有点晚了,但无论如何。 我有一个加载程序脚本实现,用于加载Windows批处理脚本和Unix Bash Shell脚本之间具有通用格式的配置文件

格式说明:

用法示例:

这将从
myvars.vars.in
文件中生成
myvars.vars
,并加载它

在解析实例文件之前,脚本确实会从模板文件生成实例配置文件。实施有点复杂,将来可能会发生变化:

小心

似乎stackoverflow错误地处理了复制粘贴代码中的制表字符(并丢失了其他字符,如
\x01
),因此,如果通过CTRL+C直接复制,下面的代码可能无法工作。请使用上面的链接直接下载脚本

旧的实现(仅举个例子):


call
创建了一个子shell,因此您在那里设置的是变量,而不是父级。@如果我不使用call,尽管它在运行该批处理后关闭file@Gene:无所谓,只要不涉及
setlocal
/
endlocal
。Naguib:使用
set
命令删除
=
周围的空格。它们分别成为变量名和值的一部分。@Stephan感谢Stephan的评论,Stephan,我删除了
=
周围的空格,以便每个集合都是
set isPause=true
,但这也不起作用<代码>%directory%
仍未打印
> type a.bat
set var=old
echo %var%
call b.bat
echo %var%

> type b.bat
set var=new

> a.bat

> set var=old

> echo old
old

> call b.bat

> set var=new

> echo new
new

>
for /f "delims=" %%a in ('type b.bat^|findstr /bic:"set "') do %%a
# FORMAT:
#   [<attributes>] <variable>[:[<class_name>]]=<value>
#
# <attributes>:           Variable space separated attributes: export
# <variable>:             Variable name corresponding to the regex: [_a-zA-Z][_a-zA-Z0-9]*
# <class_name>:           class variant name: OSWIN | OSUNIX | BAT | SH
#   OSWIN:                Apply on Windows system including cygwin/mingw/msys subsystems.
#   OSUNIX:               Apply on Unix/Linux systems excluding cygwin/mingw/msys subsystems.
#   BAT:                  Apply on Windows system when this file has loaded from the Windows batch script loader.
#   SH:                   Apply on any system when this file has loaded from the Bash shell script loader.
#
# <value>:                Can start by the `"` quote character, but two quotes does remove only when exist on both ends of a value.
#
export PYTHON_EXE_PATH:OSWIN="c:/python/x86/38/python.exe"
export PYTHON_EXE_PATH:OSUNIX=python3

export PYTHONDONTWRITEBYTECODE=1

MY_SHELL_SCRIPT:BAT=blabla.bat
MY_SHELL_SCRIPT:SH=blabla.sh
call load_config.bat . . myvars.vars
@echo off

setlocal DISABLEDELAYEDEXPANSION

set "__CONFIG_IN_DIR=%~1"
set "__CONFIG_OUT_DIR=%~2"
set "__CONFIG_FILE=%~3"

if not defined __CONFIG_IN_DIR (
  echo.%~nx0: error: input config directory is not defined.
  exit /b 1
) >&2

if not defined __CONFIG_OUT_DIR (
  echo.%~nx0: error: output config directory is not defined.
  exit /b 2
) >&2

set "__CONFIG_IN_DIR=%__CONFIG_IN_DIR:\=/%"
set "__CONFIG_OUT_DIR=%__CONFIG_OUT_DIR:\=/%"

if "%__CONFIG_IN_DIR:~-1%" == "/" set "__CONFIG_IN_DIR=%__CONFIG_IN_DIR:~0,-1%"
if "%__CONFIG_OUT_DIR:~-1%" == "/" set "__CONFIG_OUT_DIR=%__CONFIG_OUT_DIR:~0,-1%"

if not exist "%__CONFIG_IN_DIR%\" (
  echo.%~nx0: error: input config directory does not exist: "%__CONFIG_IN_DIR%".
  exit /b 10
) >&2

if not exist "%__CONFIG_OUT_DIR%\" (
  echo.%~nx0: error: output config directory does not exist: "%__CONFIG_OUT_DIR%".
  exit /b 11
) >&2

if not exist "%__CONFIG_OUT_DIR%\%__CONFIG_FILE%" ^
if exist "%__CONFIG_IN_DIR%/%__CONFIG_FILE%.in" (
  echo."%__CONFIG_IN_DIR%/%__CONFIG_FILE%.in" -^> "%__CONFIG_OUT_DIR%/%__CONFIG_FILE%"
  type "%__CONFIG_IN_DIR:/=\%\%__CONFIG_FILE%.in" > "%__CONFIG_OUT_DIR%/%__CONFIG_FILE%"
)

rem load configuration files
if not exist "%__CONFIG_OUT_DIR%/%__CONFIG_FILE%" (
  echo.%~nx0: error: config file is not found: "%__CONFIG_OUT_DIR%/%__CONFIG_FILE%".
  exit /b 20
) >&2

for /F "usebackq eol=# tokens=* delims=" %%i in ("%__CONFIG_OUT_DIR%/%__CONFIG_FILE%") do (
  endlocal
  setlocal DISABLEDELAYEDEXPANSION
  for /F "eol=# tokens=1,* delims==" %%j in ("%%i") do (
    set "__VAR=%%j"
    set "__VALUE=%%k"
    call :PARSE_EXPR && (
      setlocal ENABLEDELAYEDEXPANSION
      for /F "tokens=1,* delims==" %%i in ("!__VAR!=!__VALUE!") do (
        endlocal
        endlocal
        set "%%i=%%j"
      )
      type nul>nul
    ) || endlocal
  )
)

exit /b 0

:PARSE_EXPR
if not defined __VAR exit /b 1

rem CAUTION:
rem Inplace trim of surrounded white spaces ONLY from left and right sequences as a whole for performance reasons.
rem

:TRIM_VAR_NAME
:TRIM_VAR_NAME_LEFT_LOOP
if not defined __VAR exit /b 1
if not ^%__VAR:~0,1%/ == ^ / if not ^%__VAR:~0,1%/ == ^ / goto TRIM_VAR_NAME_RIGHT_LOOP
set "__VAR=%__VAR:~1%"
goto TRIM_VAR_NAME_LEFT_LOOP

:TRIM_VAR_NAME_RIGHT_LOOP
if not ^%__VAR:~-1%/ == ^ / if not ^%__VAR:~-1%/ == ^   / goto TRIM_VAR_NAME_RIGHT_LOOP_END
set "__VAR=%__VAR:~0,-1%"
if not defined __VAR exit /b 1
goto TRIM_VAR_NAME_RIGHT_LOOP

:TRIM_VAR_NAME_RIGHT_LOOP_END

if not defined __VALUE exit /b 0

rem Replace a value quote characters by the \x01 character.
set "__VALUE=%__VALUE:"=%"

:TRIM_VAR_VALUE
setlocal DISABLEDELAYEDEXPANSION

:TRIM_VAR_VALUE_LEFT_LOOP
if not defined __VALUE exit /b 0
if not ^%__VALUE:~0,1%/ == ^ / if not ^%__VALUE:~0,1%/ == ^ / goto TRIM_VAR_VALUE_RIGHT_LOOP
set "__VALUE=%__VALUE:~1%"
goto TRIM_VAR_VALUE_LEFT_LOOP

:TRIM_VAR_VALUE_RIGHT_LOOP
if not ^%__VALUE:~-1%/ == ^ / if not ^%__VALUE:~-1%/ == ^   / goto TRIM_VAR_VALUE_RIGHT_LOOP_END
set "__VALUE=%__VALUE:~0,-1%"
if not defined __VALUE exit /b 0
goto TRIM_VAR_VALUE_RIGHT_LOOP

:TRIM_VAR_VALUE_RIGHT_LOOP_END
(
  endlocal
  set "__VALUE=%__VALUE%"
)

for /F "eol=     tokens=1,* delims=:" %%i in ("%__VAR%") do (
  set "__VAR=%%i"
  set "__PLATFORM=%%j"
)

if not defined __VAR exit /b 1

if defined __PLATFORM ^
if not "%__PLATFORM%" == "BAT" ^
if not "%__PLATFORM%" == "WIN" ^
if not "%__PLATFORM%" == "OSWIN" exit /b 1

for /F "eol=# tokens=1,* delims=     " %%i in ("%__VAR%") do (
  set "__ATTR=%%i"
  set "__VAR=%%j"
)

if not defined __VAR (
  set "__VAR=%__ATTR%"
  set "__ATTR="
)

if not defined __VAR exit /b 1

if ^/ == ^%__VALUE:~1,1%/ goto PREPARSE_VALUE
if not ^/ == ^%__VALUE:~0,1%/ goto PREPARSE_VALUE
if not ^/ == ^%__VALUE:~-1%/ goto PREPARSE_VALUE

:REMOVE_QUOTES
for /F "tokens=* delims=" %%i in ("%__VALUE:~1,-1%") do set "__VALUE=%%i"

if not defined __VALUE exit /b 0

goto PARSE_VALUE

:PREPARSE_VALUE
set __HAS_VALUE=0
for /F "eol=# tokens=* delims=" %%i in ("%__VALUE%") do set "__HAS_VALUE=1"

if %__HAS_VALUE% EQU 0 (
  set "__VALUE="
  exit /b 0
)

:PARSE_VALUE
rem recode quote and exclamation characters
set "__ESC__=^"
set __QUOT__=^"
set "__EXCL__=!"
set "__VALUE=%__VALUE:!=!__EXCL__!%"
set "__VALUE=%__VALUE:^=!__ESC__!%"
set "__VALUE=%__VALUE:=!__QUOT__!%"

exit /b 0