如何使用BAT文件更改目录? [英] How to change directory with BAT file?

查看:145
本文介绍了如何使用BAT文件更改目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行该bat文件时,我希望下面的代码在C:\Users\zjafri\Desktop\Arthur\Runner2目录中打开,但会在我的桌面目录中打开.

I would expect the below code to open up in the C:\Users\zjafri\Desktop\Arthur\Runner2 directory when running this bat file, but instead it opens up to my desktop directory.

有人知道为什么吗?

%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
CD C:\Users\zjafri\Desktop\Arthur\Runner2

控制台输出为:

C:\Users\zjafri\Desktop>C:\windows\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3

(base) C:\Users\zjafri\Desktop>

推荐答案

使用的命令行对于想要的行为是错误的,该行为正在激活 Anaconda 环境,并将特定目录设置为当前目录,并保持正在运行命令进程,以便手动输入更多要执行的命令.

The used command lines are wrong for the wanted behavior which is activating Anaconda environment with making a specific directory the current directory and keep the command process running for entering manually more commands to execute.

批处理文件是脚本.脚本需要脚本解释器可执行文件. Windows批处理文件的解释器是Windows命令处理器cmd.exe,具有完整的合格文件名%SystemRoot%\System32\cmd.exe%ComSpec%. SystemRootComSpec是两个预定义的 Windows环境变量.

A batch file is a script. A script needs a script interpreter executable. The interpreter for Windows batch files is the Windows command processor cmd.exe which is with full qualified file name %SystemRoot%\System32\cmd.exe or %ComSpec%. SystemRoot and ComSpec are two predefined Windows Environment Variables.

Windows命令处理器在启动可执行文件时暂停批处理文件的处理,直到启动的可执行文件自身终止为止.

Windows command processor halts the processing of a batch file on starting an executable until the started executable terminated itself.

在处理批处理文件的cmd.exe实例执行以下命令行时会发生什么?

What does happen on cmd.exe instance processing the batch file executes the following command line?

%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3

Windows命令处理器会再执行一个命令处理,以处理另一个批处理文件,并在批处理文件执行完成后运行 keeps ,这是因为选项/K用双引号引起来了无用.因此,cmd.exe的已启动第二个实例不会自行终止.因此,cmd.exe的第一个实例等待并等待cmd.exe的第二个实例终止,然后再继续处理批处理文件并读取下一个命令行.

Windows command processor starts one more command process to process the other batch file and keeps running after batch file execution finished because of option /K enclosed uselessly in double quotes. So the started second instance of cmd.exe does not terminate itself. For that reason the first instance of cmd.exe waits and waits and waits for termination of second instance of cmd.exe before it continues processing the batch file with reading the next command line.

因此,仅当用户在第二命令过程中键入exit以终止第二个cmd.exe实例时,才由第一命令过程执行命令行CD C:\Users\zjafri\Desktop\Arthur\Runner2.

Therefore the command line CD C:\Users\zjafri\Desktop\Arthur\Runner2 is executed by first command process only when the user typed exit in second command process to terminate second cmd.exe instance.

一种解决方案是在批处理文件中使用以下命令行:

One solution would be the usage of following command lines in a batch file:

call "C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3"
cd /D "%UserProfile%\Desktop\Arthur\Runner2"

此批处理文件导致执行该批处理文件activate.bat,然后执行命令 CD ,然后完成该批处理文件的处理.

This batch file results in executing the batch file activate.bat, next is executed command CD and then processing of batch file is finished.

接下来会发生什么取决于批处理文件的启动方式.如果仅双击批处理文件,则Windows调用cmd.exe并使用隐式选项/C执行批处理文件,然后关闭命令过程.因此,双击批处理文件,将打开一个控制台窗口,执行两个命令行,并且随着cmd.exe自身终止,控制台窗口将关闭.

What happens next depends on how the batch file was started. If just a double click was made on the batch file, Windows called cmd.exe with using implicit option /C to execute the batch file and then close the command process. So on double clicking the batch file, a console window opens, the two command lines are executed and the console window closes as cmd.exe terminates itself.

但是,如果用户打开命令提示符,这意味着使用隐含的选项/K,并在命令提示符窗口中输入完整的限定文件名来执行该批处理文件,而无需输入或仅通过输入几个字符后按一次或多次 TAB 即可获得完成帮助文件夹/文件名,然后按 RETURN ENTER 来执行批处理文件,命令提示符窗口保持打开状态,并且 Anaconda 环境被激活目录%UserProfile%\Desktop\Arthur\Runner2是当前目录.

But if the user opened a command prompt which means starting cmd.exe with using implicit the option /K, and executes the batch file from within the command prompt window by typing its full qualified file name without or with the completion help by hitting one or more times TAB after entering just a few characters of a folder/file name, and hits RETURN or ENTER to execute the batch file, the command prompt window remains opened and Anaconda environment is activated with directory %UserProfile%\Desktop\Arthur\Runner2 being the current directory.

好吧,首先打开命令提示符窗口,然后手动运行具有完整路径的批处理文件并不是很方便.批处理文件可以用短文件名存储在其中一个路径存在于环境变量PATH的值(如Windows目录)中的文件夹之一中.这样便可以打开命令提示符,仅键入批处理文件的文件名,然后按 RETURN ENTER 键执行该命令.

Well, first opening a command prompt window and manually running the batch file with full path is not really handy. The batch file could be stored with a short file name in one of the folders of which path is present in value of environment variable PATH like the Windows directory. This would make it possible to open a command prompt, type just the file name of the batch file and hit key RETURN or ENTER to execute it.

另一种便捷的解决方案是在用户桌面上创建文件名为Anaconda3的快捷方式文件(* .lnk),在该快捷方式文件中配置快捷方式的属性:

Another handy solution would be creating on user's desktop a shortcut file (*.lnk) with file name Anaconda3 on which in properties of the shortcut is configured:

目标:%SystemRoot%\System32\cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
开始于: C:\Users\zjafri\Desktop\Arthur\Runner2
评论: Opens a command prompt and activates Anaconda3 in Runner2

Target: %SystemRoot%\System32\cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Start in: C:\Users\zjafri\Desktop\Arthur\Runner2
Comment: Opens a command prompt and activates Anaconda3 in Runner2

使用快捷方式文件有多个优点.可以定义快捷键来启动Windows命令处理器并执行批处理文件以激活 Anaconda 环境,这与当前哪个应用程序具有输入焦点以及桌面是否位于桌面后面有关.应用程序窗口或所有其他窗口的前面.在快捷方式属性的 Options Font Layout Colors 选项卡上,可以定义选项和外观应该在使用 Anaconda 时使用它,例如默认使用具有更多行/列和更大字体的控制台窗口.

There are multiple advantages on using a shortcut file. A shortcut key can be defined to start Windows command processor and execute the batch file to activate Anaconda environment independent on which application has currently the input focus and if the desktop is behind an application window or in front of all other windows. On the tabs Options, Font, Layout, Colors of the shortcut properties the options and the look can be defined which should be used on using Anaconda like using a console window with more lines/columns and with a larger font as by default.

尽管使用快捷方式文件绝对是此用例的最佳解决方案,但也可以双击运行批处理文件以打开命令提示符窗口,以执行批处理文件来激活 Anaconda 环境,并使命令进程在当前目录设置为通缉目录的情况下运行.

Although the usage of a shortcut file would be definitely the best solution for this use case, it is also possible to run a batch file with a double click to open a command prompt window to execute the batch file to activate Anaconda environment and keep the command process running with current directory set to wanted directory.

这样的批处理文件只需要一个命令行:

Such a batch file would require just one command line:

@start "Anaconda3" /D"%UserProfile%\Desktop\Arthur\Runner2" %ComSpec% /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3

在这种情况下,cmd.exe使用此命令行处理批处理文件将启动一个带有选项/K的新命令进程,以在执行批处理文件后通过以Anaconda3为标题启动cmd.exe来保持命令进程运行.用于新的控制台窗口,并在启动cmd.exe之前将目录CD C:\Users\zjafri\Desktop\Arthur\Runner2作为当前目录进行设置. cmd.exe在这种情况下不等待启动的第二个cmd.exe终止,因此使用单个命令行处理批处理文件的cmd.exe实例立即终止.

In this case cmd.exe processing the batch file with this command line starts a new command process with option /K to keep the command process running after execution of the batch file by started cmd.exe with Anaconda3 as title for the new console window and with setting before starting cmd.exe the directory CD C:\Users\zjafri\Desktop\Arthur\Runner2 as current directory. cmd.exe does not wait for termination of the started second cmd.exe in this case and so the cmd.exe instance processing the batch file with this single command line terminates immediately.

此批处理文件解决方案的缺点在于,用户无法配置激活 Anaconda 环境后保持运行的命令过程的选项,字体,布局,颜色.

The disadvantage of this batch file solution is that the user has no possibility to configure the options, font, layout, colors of the command process which keeps running after activating the Anaconda environment.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,然后在此处执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cd /?
  • cmd /?
  • start /?
  • call /?
  • cd /?
  • cmd /?
  • start /?

PS:我既未安装 Anaconda ,也未阅读其文档.所以我不知道执行C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3时会发生什么,执行此批处理文件后当前目录是什么.如果批处理文件编码正确,并且参数C:\ProgramData\Anaconda3并不意味着将当前目录更改为该目录,则所有操作均应如上所述.否则,所提供的解决方案需要根据activate.bat在环境上进行的更改(即更改当前目录的时间)进行调整.

PS: I have installed neither Anaconda nor read its documentation. So I don't know what happens on execution of C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3 and what is the current directory after execution of this batch file. If the batch file is good coded and the argument C:\ProgramData\Anaconda3 does not mean to change the current directory to this directory, then everything should work as written above. Otherwise the provided solutions need to be adapted depending on what activate.bat changes on environment, i.e. when it changes the current directory.

这篇关于如何使用BAT文件更改目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆