Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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
Windows 运行.bat文件的命令_Windows_Cmd_Command_Prompt - Fatal编程技术网

Windows 运行.bat文件的命令

Windows 运行.bat文件的命令,windows,cmd,command,prompt,Windows,Cmd,Command,Prompt,我试图让我的VisualStudio构建脚本执行一个.bat文件,该文件执行一些重要的操作 以下是我现在想要做的: cd "F:\- Big Packets -\kitterengine\Common\" Template.bat 但它不起作用 我必须这样做才能让它工作: cd "F:\- Big Packets -\kitterengine\Common\" F: Template.bat 但这很难添加到VisualStudio脚本中 如何在一行中完成此任务?解决此任务有多种可能性 1.使

我试图让我的VisualStudio构建脚本执行一个.bat文件,该文件执行一些重要的操作

以下是我现在想要做的:

cd "F:\- Big Packets -\kitterengine\Common\" Template.bat
但它不起作用

我必须这样做才能让它工作:

cd "F:\- Big Packets -\kitterengine\Common\"
F:
Template.bat
但这很难添加到VisualStudio脚本中


如何在一行中完成此任务?

解决此任务有多种可能性

1.使用完整路径运行批处理文件 最简单的解决方案是运行带有完整路径的批处理文件

"F:\- Big Packets -\kitterengine\Common\Template.bat"
call "F:\- Big Packets -\kitterengine\Common\Template.bat"
一旦到达批处理文件
Template.bat
的末尾,如果上面的命令行位于*.bat或*.cmd文件中,则不会返回到上一个脚本

批处理文件
Template.bat的当前目录是当前进程的当前目录。如果
Template.bat
要求此批处理文件的目录为当前目录,则批处理文件
Template.bat
应在
@echo off
之后包含以下命令行作为第二行:

cd /D "%~dp0"
在命令提示符窗口中运行
cd/?
,以获得此命令的帮助,解释参数
/D
。。。更改到另一个驱动器上的指定目录

在命令提示窗口中运行
call/?
,以获得该命令的帮助,该命令也用于2,4中。五,。解决方案和解释也
%~dp0
。。。参数0的驱动器和路径,该参数是批处理文件的名称

cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
2.使用完整路径调用批处理文件 另一种解决方案是调用具有完整路径的批处理文件

"F:\- Big Packets -\kitterengine\Common\Template.bat"
call "F:\- Big Packets -\kitterengine\Common\Template.bat"
与第一种解决方案不同的是,在到达批处理文件
Template.bat的末尾后,批处理将在包含此命令行的批处理脚本中继续进行

对于上面读取的当前目录

3.更改目录并使用一个命令行运行批处理文件 在一个命令行上运行多个命令有3个运算符:
&
&
|

有关详细信息,请参阅上的答案

我建议由
&
操作员执行此任务

cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat
与第一个解决方案一样,如果这是一个*.bat或*.cmd文件,则不会返回到当前脚本,并且在
Template.bat
上更改目录并继续批处理是成功的

4.用一个命令行更改目录并调用批处理文件 此命令行更改目录,并在成功时调用批处理文件

cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
与第三种解决方案不同的是,退出处理
Template.bat时返回到当前批处理脚本

5.使用一个命令行更改目录并调用批处理文件以保持当前环境 上述四种解决方案更改了当前目录,并且不知道
Template.bat
对该目录做了什么

  • 当前目录
  • 环境变量
  • 命令扩展状态
  • 延迟膨胀状态
  • 如果重要的是保持当前*.bat或*.cmd脚本的环境不被任何
    模板.bat
    更改,建议使用
    setlocal
    endlocal

    在命令提示窗口中运行
    setlocal/?
    endlocal/?
    ,以显示这两个命令的帮助。并阅读关于更详细地解释这两个命令的作用的答案

    setlocal & cd /D "F:\- Big Packets -\kitterengine\Common" & call Template.bat & endlocal
    
    现在只使用了
    &
    而不是
    &
    ,因为在执行
    setlocal
    之后,最后也会执行命令
    endlocal
    ,这一点很重要


    再来一个音符

    如果批处理文件
    Template.bat
    包含命令
    exit
    ,而没有参数
    /B
    ,并且该命令确实执行,则命令过程始终独立于调用层次结构而退出。因此,请确保
    Template.bat
    包含
    exit/B
    goto:EOF
    ,而不只是
    exit
    (如果批处理文件中使用了
    exit

    “F:\-大数据包-\kitterengine\Common\Template.bat”
    可能以
    call
    开头(请参见
    call//code>)。或者
    Cd/d“F:\-大数据包-\kitterengine\Common\”&Template.bat

    cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
    

    CMD备忘单

    • Cmd.exe

    • 寻求帮助

    • 标点符号

    • 命名文件

    • 启动程序

    • 钥匙

    CMD.exe

    首先要记住的是操作电脑的方法。这是我们在WIMP(窗口、图标、鼠标、弹出菜单)普及之前的做法。它源于CPM、VMS和Unix。它用于启动程序、复制和删除文件。您还可以更改时间和日期

    有关启动CMD的帮助,请键入
    CMD/?
    。除非您只想输入,否则必须使用
    /k
    /c
    开关启动它

    寻求帮助

    需要一般帮助。在命令提示下键入
    Help
    。对于列出的每个命令,键入
    help
    (例如
    help dir
    )或
    /?
    (例如
    dir/?

    有些命令有子命令。例如
    schtasks/create/?

    NET
    命令的帮助不同寻常。键入
    net use/?
    是简短的帮助。键入
    网络帮助使用
    获取完整帮助。这同样适用于根目录-
    net/?
    也是简要帮助,请使用
    net help

    新行为帮助中的参考描述了从OS/2和Windows NT4中的CMD到Windows 2000及更高版本中的当前CMD的更改

    WMIC
    是一个多用途命令。键入
    wmic/?


    标点符号

    &    seperates commands on a line.
    
    &&    executes this command only if previous command's errorlevel is 0.
    
    ||    (not used above) executes this command only if previous command's 
    errorlevel is NOT 0
    
    >    output to a file
    
    >>    append output to a file
    
    <    input from a file
    
    2> Redirects command error output to the file specified. (0 is StdInput, 1 is StdOutput, and 2 is StdError)
    
    2>&1 Redirects command error output to the same location as command output. 
    
    |    output of one command into the input of another command
    
    ^    escapes any of the above, including itself, if needed to be passed 
    to a program
    
    "    parameters with spaces must be enclosed in quotes
    
    + used with copy to concatenate files. E.G. copy file1+file2 newfile
    
    , used with copy to indicate missing parameters. This updates the files 
    modified date. E.G. copy /b file1,,
    
    %variablename% a inbuilt or user set environmental variable
    
    !variablename! a user set environmental variable expanded at execution 
    time, turned with SelLocal EnableDelayedExpansion command
    
    %<number> (%1) the nth command line parameter passed to a batch file. %0 
    is the batchfile's name.
    
    %* (%*) the entire command line.
    
    %CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor (from set /?).
    
    %<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. 
    Single % sign at command prompt and double % sign in a batch file.
    
    \\ (\\servername\sharename\folder\file.ext) access files and folders via UNC naming.
    
    : (win.ini:streamname) accesses an alternative steam. Also separates drive from rest of path.
    
    . (win.ini) the LAST dot in a file path separates the name from extension
    
    . (dir .\*.txt) the current directory
    
    .. (cd ..) the parent directory
    
    
    \\?\ (\\?\c:\windows\win.ini) When a file path is prefixed with \\?\ filename checks are turned off. 
    
    在批处理文件中,批处理将等待程序退出。什么时候 键入命令
    start "" /D F:\- Big Packets -\kitterengine\Common\ /W Template.bat
    
    cmd /c ""Full_Path_Of_Batch_Here.cmd" "