Windows批处理脚本 - GOTO分支无法按预期工作

时间:2014-06-10 04:40:32

标签: windows scripting batch-processing goto

我创建了一个批处理脚本,用于将文件夹结构从生产同步到所有较低的环境。使用robocopy,我清理较低环境中的文件夹,并仅复制文件夹和子文件夹(减去文件以节省空间)。它按预期工作。

现在我尝试将脚本(1个脚本/环境)合并为1,因此它将环境名称作为参数,并仅触发与该环境相关的代码。我已经编写了多个IF GOTO语句来分支执行流程,但我遇到了障碍,但它只是不起作用。我当然可以用另一副专家眼睛来发现错误并帮助修复它。请找到下面的脚本:现在它要求输入,然后立即执行END语句并结束。非常感谢提前。

@echo off

set /p env = "Enter Target Environment (EnvA, EnvB, EnvC, EnvF): "

if "%env%" == "EnvA" goto :SyncA
if "%env%" == "EnvB" goto :SyncB
if "%env%" == "EnvC" goto :SyncC
if "%env%" == "EnvF" goto :SyncF

:SyncA
cls

echo *******************************************************************************************
echo * Delete Environment A SAN share contents and resync folder structure from production *
echo *******************************************************************************************

set sd=C:\mockdev\Build
set dd=\\testA\TempStorage\BuildEdge
set ipasd=C:\mockdev\Environments
set ipadd=\\testA\TempStorage\Environments
set copy_options=/MIR /XF * /R:3 /W:10
set log_options=/NP /NFL /NDL /NJH
set error=0
set result=0

if not exist "%sd%" echo ERROR: [%sd%] does not exist
if not exist "%dd%" echo ERROR: [%dd%] does not exist

echo This script will delete all files in [%dd%] and resync the folder structure from [%sd%]
echo Continue?

Pause

echo Deleting all files from [%dd%] ...
del %dd%\*.* /s /q

echo Recreating [%dd%] folder structure from [%sd%] ...

Pause

Robocopy %sd% %dd% %copy_options% %log_options% 

Mkdir \\testA\TempStorage\BuildEdge\IpaB
robocopy %ipasd% %ipadd% %copy_options% %log_options%

if ERRORLEVEL 16 set result=*ERROR* Fatal error (Error: %ERRORLEVEL%)
if ERRORLEVEL  8 set result=*ERROR* General failure (Error: %ERRORLEVEL%)
if ERRORLEVEL  4 set result=*WARNING* Check log for possible errors (Error: %ERRORLEVEL%)
set result=*OK* Job completed successfully (%ERRORLEVEL%)

echo ------------------------------------------------------------------------------
echo Script complete [%result%]

exit /b %error%

goto end

:SyncB
cls

echo *******************************************************************************************
echo * Delete Environment B SAN share contents and resync folder structure from production *
echo *******************************************************************************************

set sd=C:\mockdev\Build
set dd=\\testB\TempStorage\BuildEdge
set ipasd=C:\mockdev\Environments
set ipadd=\\testB\TempStorage\Environments
set copy_options=/MIR /XF * /R:3 /W:10
set log_options=/NP /NFL /NDL /NJH
set error=0
set result=0

if not exist "%sd%" echo ERROR: [%sd%] does not exist
if not exist "%dd%" echo ERROR: [%dd%] does not exist

echo This script will delete all files in [%dd%] and resync the folder structure from [%sd%]
echo Continue?

Pause

echo Deleting all files from [%dd%] ...
del %dd%\*.* /s /q

echo Recreating [%dd%] folder structure from [%sd%] ...

Pause

Robocopy %sd% %dd% %copy_options% %log_options% 

Mkdir \\testB\TempStorage\BuildEdge\IpaB
robocopy %ipasd% %ipadd% %copy_options% %log_options%

if ERRORLEVEL 16 set result=*ERROR* Fatal error (Error: %ERRORLEVEL%)
if ERRORLEVEL  8 set result=*ERROR* General failure (Error: %ERRORLEVEL%)
if ERRORLEVEL  4 set result=*WARNING* Check log for possible errors (Error: %ERRORLEVEL%)
set result=*OK* Job completed successfully (%ERRORLEVEL%)

echo ------------------------------------------------------------------------------
echo Script complete [%result%]

exit /b %error%

goto end

:SyncC
cls

echo *******************************************************************************************
echo * Delete Environment C SAN share contents and resync folder structure from production *
echo *******************************************************************************************

set sd=C:\mockdev\Build
set dd=\\testC\TempStorage\BuildEdge
set ipasd=C:\mockdev\Environments
set ipadd=\\test02\TempStorage\Environments
set copy_options=/MIR /XF * /R:3 /W:10
set log_options=/NP /NFL /NDL /NJH
set error=0
set result=0

if not exist "%sd%" echo ERROR: [%sd%] does not exist
if not exist "%dd%" echo ERROR: [%dd%] does not exist

echo This script will delete all files in [%dd%] and resync the folder structure from [%sd%]
echo Continue?

Pause

echo Deleting all files from [%dd%] ...
del %dd%\*.* /s /q

echo Recreating [%dd%] folder structure from [%sd%] ...

Pause

Robocopy %sd% %dd% %copy_options% %log_options% 

Mkdir \\testC\TempStorage\BuildEdge\IpaB
robocopy %ipasd% %ipadd% %copy_options% %log_options%

if ERRORLEVEL 16 set result=*ERROR* Fatal error (Error: %ERRORLEVEL%)
if ERRORLEVEL  8 set result=*ERROR* General failure (Error: %ERRORLEVEL%)
if ERRORLEVEL  4 set result=*WARNING* Check log for possible errors (Error: %ERRORLEVEL%)
set result=*OK* Job completed successfully (%ERRORLEVEL%)

echo ------------------------------------------------------------------------------
echo Script complete [%result%]

exit /b %error%

goto end

:SyncF
cls

echo *******************************************************************************************
echo * Delete Environment F SAN share contents and resync folder structure from production *
echo *******************************************************************************************

set sd=C:\mockdev\Build
set dd=\\testF\TempStorage\BuildEdge
set ipasd=C:\mockdev\Environments
set ipadd=\\testF\TempStorage\Environments
set copy_options=/MIR /XF * /R:3 /W:10
set log_options=/NP /NFL /NDL /NJH
set error=0
set result=0

if not exist "%sd%" echo ERROR: [%sd%] does not exist
if not exist "%dd%" echo ERROR: [%dd%] does not exist

echo This script will delete all files in [%dd%] and resync the folder structure from [%sd%]
echo Continue?

Pause

echo Deleting all files from [%dd%] ...
del %dd%\*.* /s /q

echo Recreating [%dd%] folder structure from [%sd%] ...

Pause

Robocopy %sd% %dd% %copy_options% %log_options% 

Mkdir \\testF\TempStorage\BuildEdge\IpaB
robocopy %ipasd% %ipadd% %copy_options% %log_options%

if ERRORLEVEL 16 set result=*ERROR* Fatal error (Error: %ERRORLEVEL%)
if ERRORLEVEL  8 set result=*ERROR* General failure (Error: %ERRORLEVEL%)
if ERRORLEVEL  4 set result=*WARNING* Check log for possible errors (Error: %ERRORLEVEL%)
set result=*OK* Job completed successfully (%ERRORLEVEL%)

echo ------------------------------------------------------------------------------
echo Script complete [%result%]

exit /b %error%

goto end

:end
Press any key to exit
pause>nul
Exit

1 个答案:

答案 0 :(得分:0)

set /p env = "Enter Target Environment (EnvA, EnvB, EnvC, EnvF): "
----------^ This space gets included into the variable name

更改为

set /p "env=Enter Target Environment (EnvA, EnvB, EnvC, EnvF): "

这本身会使选择无效。

如果环境之间的唯一差异可以移动到变量,请不要复制代码,只复制变量。它更容易维护

在此示例中,为每个环境调用常规配置,然后设置环境的特定变量。这样,如果由于任何原因任何环境需要覆盖一般配置,则可以完成。配置完属性后,将启动文件管理过程。

@echo off
    setlocal enableextensions

:ask    

    set "env="
    echo(
    set /p "env=Enter Target Environment (EnvA, EnvB, EnvC, EnvF): "

           if /i "%env%"=="EnvA" ( call :SyncA 
    ) else if /i "%env%"=="EnvB" ( call :SyncB 
    ) else if /i "%env%"=="EnvC" ( call :SyncC 
    ) else if /i "%env%"=="EnvF" ( call :SyncF 
    ) else set "env="

    if defined env goto :doWork

    goto :ask

:SyncConfig
    set "sd=C:\mockdev\Build"
    set "ipasd=C:\mockdev\Environments"
    set "copy_options=/MIR /XF * /R:3 /W:10"
    set "log_options=/NP /NFL /NDL /NJH"
    set "error=0"
    set "result=0"
    goto :eof

:SyncA
    call :SyncConfig
    set "dd=\\testA\TempStorage\BuildEdge"
    set "ipadd=\\testA\TempStorage\Environments"
    goto :eof

:SyncB
    call :SyncConfig
    set "dd=\\testB\TempStorage\BuildEdge"
    set "ipadd=\\testB\TempStorage\Environments"
    goto :eof

:SyncC
    call :SyncConfig
    set "dd=\\testC\TempStorage\BuildEdge"
    set "ipadd=\\test02\TempStorage\Environments"
    goto :eof

:SyncD    
    call :SyncConfig
    set "dd=\\testF\TempStorage\BuildEdge"
    set "ipadd=\\testF\TempStorage\Environments"
    goto :eof

:doWork    
    cls

    echo *******************************************************************************************
    echo * Delete Environment %env% SAN share contents and resync folder structure from production *
    echo *******************************************************************************************

    if not exist "%sd%" echo ERROR: [%sd%] does not exist
    if not exist "%dd%" echo ERROR: [%dd%] does not exist

    echo This script will delete all files in [%dd%] and resync the folder structure from [%sd%]
    echo Continue?

    Pause

    echo Deleting all files from [%dd%] ...
    del %dd%\*.* /s /q

    echo Recreating [%dd%] folder structure from [%sd%] ...

    Pause

    Robocopy %sd% %dd% %copy_options% %log_options% 

    Mkdir "%dd%\IpaB"
    robocopy %ipasd% %ipadd% %copy_options% %log_options%

            if ERRORLEVEL 16 ( set "error=%errorlevel%" & set "result=*ERROR* Fatal error (Error: %ERRORLEVEL%)"
    ) else  if ERRORLEVEL  8 ( set "error=%errorlevel%" & set "result=*ERROR* General failure (Error: %ERRORLEVEL%)"
    ) else  if ERRORLEVEL  4 ( set "error=%errorlevel%" & set "result=*WARNING* Check log for possible errors (Error: %ERRORLEVEL%)"
    ) else                     set "error=%errorlevel%" & set "result=*OK* Job completed successfully (%ERRORLEVEL%)"

    echo(------------------------------------------------------------------------------
    echo(Script complete [%result%]

    Press any key to exit
    pause>nul
    endlocal & exit /b %error%