如何编写一个批处理文件,以打开命令行并自动执行其中的命令?

huangapple go评论73阅读模式
英文:

How to write a batch file that opens the command line and automatically executes commands in that command line?

问题

最初,我尝试自动化更新我管理的两个静态GitHub网站的站点地图。这个过程是一个Python程序,它进入sitemap.xml文件并在需要时更新其中的部分。

为了自动化这个过程,我决定编写一个批处理脚本来运行站点地图更新程序。然而,由于其他命令行中未激活sqlite3插件,我想在Anaconda Prompt上运行批处理脚本;它只在Anaconda Prompt上有效。因此,我决定采取一种方法,打开Anaconda Prompt命令行并逐个运行命令。

首先,我有一个文本文件,其中包含类似以下的命令行代码:

@echo off
pushd Desktop
py updateSitemap.py

这是我正在尝试运行的实际批处理脚本:

@echo off
"Anaconda3 (64-bit)\Anaconda Prompt (Anaconda).lnk" < "sitemapUpdateCommands.txt"

根据语法,这应该将文本文件中的所有信息放入命令行,但这没有起作用。我是否有什么混淆?感激任何帮助。

英文:

I have recently been trying to automate a process that I use to update sitemaps for two static GitHub websites that I manage. The process is a Python program that enters the sitemap.xml file and updates parts of it when needed.

In order to automate this process, I decided to write a batch script that could run the sitemap-updating program. However, I wanted to run the batch script on Anaconda Prompt since the sqlite3 plugin has not been activated on the other command lines; it only works with Anaconda Prompt. Because of this, I decided to take an approach that opens the Anaconda Prompt command line and runs the commands one by one.

First, I had a text file that had command line code like this:

@echo off
pushd Desktop
py updateSitemap.py

Here is the actual batch script that I am trying to run:

@echo off
&quot;Anaconda3 (64-bit)\Anaconda Prompt (Anaconda).lnk&quot; &lt; &quot;sitemapUpdateCommands.txt&quot;

According to the syntax, this should put all of the information from the text file into the command line, but that did not work.

Is there something that I am confused about? Any help can be appreciated.

答案1

得分: 1

the anaconda prompt is just calling cmd.exe and activating the anaconda script in it, you can do that in your script, if Anaconda3 is installed in C:\ProgramData\ (default) the script will look as follows.

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat
python -c &quot;import sys; print(sys.executable)&quot;

or for your case

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat
call yourscript.bat

where yourscript.bat is the .bat script you are trying to have anaconda execute in its command prompt. (which calls your python script).


to activate a certain environment, put it at the end of your activate command as follows.

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat myenv
call yourscript.bat
英文:

the anaconda prompt is just calling cmd.exe and activating the anaconda script in it, you can do that in your script, if Anaconda3 is installed in C:\ProgramData\ (default) the script will look as follows.

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat
python -c &quot;import sys; print(sys.executable)&quot;

or for your case

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat
call yourscript.bat

where yourscript.bat is the .bat script you are trying to have anaconda execute in its command prompt. (which calls your python script).


to activate a certain environment, put it at the end of you activate command as follows.

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat myenv
call yourscript.bat

huangapple
  • 本文由 发表于 2023年4月13日 22:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定