【问题标题】:( goto was unexpected this time error(这次 goto 出乎意料的错误
【发布时间】:2015-04-07 14:34:16
【问题描述】:

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

@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

【问题讨论】:

    标签: batch-file if-statement syntax goto


    【解决方案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
    )
    

    【讨论】:

    • 啊。是的。将删除我的答案。
    • 谢谢..但是在做出改变之后,我仍然得到命令的语法不正确的错误。我现在该怎么办?
    • @Meen,请参阅更新的答案。检查您的所有if 条件是否正常。如果仍有问题,请删除echo off,看看问题出在哪里。
    • 我想我找到了问题...但仍然没有解决方案...:(代码的问题是批处理没有执行 SUB 的所有代码...我意思是当我尝试在潜艇中使用 onlt 1 行时,它们工作正常..但不是多行潜艇
    • set /p FileType=文件类型: 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 :TEXT move /-y "D:\artifacts\Art1\test1.txt" "D:\artifacts\Art2\">>_Deploy.txt 2>&1 exit /b :FTP move /-y "D:\artifacts\Art1\test2.txt" "D:\artifacts\Art2\">>_Deploy.txt 2>&1 退出 /b :bat call testbatchcreatefolder.bat>>_Deploy.txt 2 >&1 move /-y "D:\artifacts\Art1\testbatchcreatefolder.bat" "D:\artifacts\Art2\" exit /b
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多