为什么echo off和语法错误是我的代码

时间:2014-11-27 04:23:28

标签: batch-file

这是我的代码,用于运行批处理脚本来运行SQL脚本并生成执行日志。帮助我理解

::Echo off will hide all commands written in text file
@echo off

    :: Declaring colour appearance of the batch
    color Af
    setlocal enabledelayedexpansion

    ::Declaration of the SQL Server Name.
    set  servername=192.168.90.34

    :: Declaration of the source path where we will keep script file.
    set  spath=D:\Batch_Test_File\Script_File

    ::Declaration of the destination Path where we will move script file after successful execution.
    set  dpath=D:\Batch_Test_File\Executed_script\Enterprise_Script

        :: Creation of the Execution log folder basis of current date.
        set TIMESTAMP=%DATE:~7,2%%DATE:~4,2%%DATE:~10,4%
        IF EXIST "%D:\Batch_Test_File\Execution_log\%TIMESTAMP%" (
            echo Folder exist
            set  lpath=D:\Batch_Test_File\Execution_log\%TIMESTAMP%
            echo %lpath%
            ) ELSE (
            md "%D:\Batch_Test_File\Execution_log\%TIMESTAMP%"
            set  lpath=D:\Batch_Test_File\Execution_log\%TIMESTAMP%
            echo %lpath%
        )

    :: Declaration of the credential of the SQL Server 2008
    set  uname=sa
    set  pwd=sa@gr8

    :: Defining Flag for capturing error status of script file
    set temp=0
    echo temp value1:-%temp%

    :: Printing ServerName on Which script will apply.
    echo Server_Name:-%servername%

    :: Printing Source path of the script file
    echo File_Path:-%spath%
    pause

    :: Listing all files from Source Path
    set cmd='dir %spath%\*ED*.sql /b/s'
    echo %cmd%
    pause

    ::Creating Log file basis of script file available



    ::Taken a loop as per the count of file in CMD Variable
    FOR /f "delims=" %%G IN (%cmd%) DO (
    echo inside loop
    set _fn=%%G
    set _fn=!_fn:~31,11!
    set logfilepath= %lpath%\output_!_fn!.log
    echo %logfilepath%--Here echo is off coming. If Using echo !logfilepath! then it is printing but not able to capture log further
    echo logfilepath1 file name here
    pause
        FOR /F "tokens=*" %%A in  (D:\Batch_Test_File\EnterpriseDatabaseList.txt) do (
            echo Database_Name:- %%A >> %logfilepath%---here throwing syntax error
            echo ******PROCESSING %%G FILE******
            SQLCMD -S%servername% -U %uname% -P %pwd%  -d %%A -b -i%%G >> %logfilepath%
            echo %%G File
            echo Database_Name:- %%A >> %logfilepath%
            IF !ERRORLEVEL! EQU 0 echo ******PROCESSED %%G FILE****** >> %logfilepath%
            IF !ERRORLEVEL! NEQ 0 echo ******NOT PROCESSED %%G FILE****** >> %logfilepath%
            IF !ERRORLEVEL! NEQ 0 set /a temp+=1
            echo SQL SERVER ERROR:- !ERRORLEVEL! 
        )
    MOVE %%G %dpath% 
    )
::pause
echo temp value2:-%temp%
pause
IF %temp% EQU 0 GOTO Success IF NOT GOTO :OnError

:OnError
echo ERROR ERROR ERROR
echo One\more script(s) failed to execute, terminating path.
echo Check output.log file for more details
echo File not moved  >> %logfilepath%
pause
EXIT /b

:Success
echo ALL the scripts deployed successfully!! >> %logfilepath%
pause
EXIT /b

请帮助我!

1 个答案:

答案 0 :(得分:1)

您正在使用echo %lpath%在您设置lpath的相同括号内。

如果您使用echo !lpath!来使用延迟扩展(已设置)或仅在if () else ()

之后回显

同样适用于logfilepath。这也是为什么当回声线变为:

时你会得到语法错误的原因
echo Database_Name:- %%A >> 
echo ******PROCESSING %%G FILE******
SQLCMD -S%servername% -U %uname% -P %pwd%  -d %%A -b -i%%G >>

您需要使用>> !logfilepath!