(这次出现意外错误

时间:2015-04-07 14:34:16

标签: batch-file if-statement syntax goto

下面的代码是抛出错误。请帮忙。我是批处理文件脚本的新手。它显示语法错误," goto这次出乎意料"错误。 我的动机是创建一个批处理文件,它将接受来自用户的参数。基于该参数,它将执行任务。就像输入是sql一样,它会执行一个sql脚本。如果输入是foldercreation,它将创建一个新文件夹。

@echo off
cls

set /p FileType=Type of file :
if "%FileType%==SQL" (goto sql)
if "%FileType%==TXT" (goto text)
if "%FileType%==BAT" (goto bat)
if "%FileType%==FolderCreation" (goto FolderCreation)
if "%FileType%==FTP" (goto ftp)


:sql
set /p SName=Server Name :
set /p DbName=Database Name :
if exist _Deploy.txt del _Deploy.txt
@echo on
sqlcmd -S %SName% -E -d %DbName% -I -i "D:\Script\Execute_Script.sql" >>_Deploy.txt 2>&1
@notepad _Deploy.txt
exit /b

:text
if exist _Deploy.txt
@echo on
move /-y "D:\artifacts\Art1\test1.txt" "D:\artifacts\Art2\">>_Deploy.txt 2>&1
@notepad _Deploy.txt
exit /b

:bat
if exist _Deploy.txt
@echo on
call testbatchcreatefolder.bat>>_Deploy.txt 2>&1
@notepad _Deploy.txt
move /-y "D:\artifacts\Art1\testbatchcreatefolder.bat" "D:\artifacts\Art2\"
exit /b

:FolderCreation
set /p Mypath=Path please :
set /p Myfoldername=folder name :
set folder_path=%Mypath%\%Myfoldername%
md "%folder_path%"
exit /b

:FTP
if exist _Deploy.txt
@echo on
move /-y "D:\artifacts\Art1\test2.txt" "D:\artifacts\Art2\">>_Deploy.txt 2>&1
@notepad _Deploy.txt
exit /b

pause

set /p delExit=Press the ENTER key to exit...:
:end

1 个答案:

答案 0 :(得分:1)

if "%FileType%==SQL" (goto sql)
   ^...............^ = one string

所以前面的代码相当于

if "this is not valid" (goto sql)

if命令中,您需要一个条件。如果它需要比较两个字符串,它应该是

if "%FileType%"=="SQL" (goto sql)
   ^..........^  ^...^

当解析器用其值替换变量引用时,在==运算符的每一侧都有一个带引号的字符串

应该在所有相似的行中进行此更改。

另外,行

if exist _Deploy.txt

缺少命令部分。如果打算在if之后执行代码块,那么它应该是

if exist _Deploy.txt (
    here the code to execute if the file exist
)