windows 批处理目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5642021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:16:40  来源:igfitidea点击:

Batch process all files in directory

windowsbatch-filecmd

提问by Gabriel

Right now i have a batch job I wrote that calls another file and passes in the variables that executable needs to run (password and filename).

现在我写了一个批处理作业,它调用另一个文件并传入可执行文件需要运行的变量(密码和文件名)。

Ex:

前任:

> cd f:\test\utils 
> admin import-xml -Dimport.file=f:\DB\file1.xml  -Dadmin.db.password=test123

I wrote a job that does this, but found out that there would be multiple files.

我写了一个这样做的工作,但发现会有多个文件。

The username and password never change but the filename differs for like 15 different xml files--with maybe more coming soon.

用户名和密码永远不会改变,但文件名对于 15 个不同的 xml 文件来说是不同的——也许很快就会有更多。

The files will always be located in the same folder. Instead of ending up with like 15-20 jobs (one for each file), can I write something that will process each file located in this directory. And either wait till one is completed before the next or I can add a 3 min sleep before it starts the next file.

这些文件将始终位于同一文件夹中。我可以编写一些内容来处理位于此目录中的每个文件,而不是像 15-20 个作业(每个文件一个)那样结束。要么等到一个完成后再下一个,或者我可以在开始下一个文件之前添加 3 分钟的睡眠时间。

回答by Joey

pushd C:\test\utils
for %%F in (F:\DB\*.xml) do (
   admin import-xml "-Dimport.file=%%~dpnxF" -Dadmin.db.password=test123
)
popd

The %%~dpnxFexpands to d?rive, p?ath, base?n?ame and e?x?tension of the current file.

所述%%~dpnxF膨胀到d?RIVE,p?ATH,基地?n?ame 和 e? x?当前文件的张力。

If you intend to set anduse environment variables (%foo%) in that loop, read help setfirst before you get into trouble.

如果您打算在该循环中设置使用环境变量 ( %foo%),请help set在遇到麻烦之前先阅读。

回答by Mark Wilkins

You can use the forcommand. Something like this in a batch file:

您可以使用该for命令。在批处理文件中是这样的:

for %%f in (*.xml) do call myotherbatch.bat %%f

Assuming that the admincommand you are running doesn't return until the job is finished, the above loop would process them sequentially.

假设admin您正在运行的命令在作业完成之前不会返回,则上述循环将按顺序处理它们。

If you run the command at the prompt (as opposed to in a batch file), only use a single %.

如果在提示符下运行命令(而不是在批处理文件中),则只使用一个 %。

回答by Diego Torres Milano

for file in f:\DB\*
do
   admin import-xml -Dimport.file="$file" -Dadmin.db.password=test123
done