windows 批处理脚本在每个子文件夹中执行一些命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33276921/
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-08 19:18:32  来源:igfitidea点击:

Batch script to execute some commands in each sub-folder

windowsbatch-file

提问by Kacper

I need to write a script to work in Windows, that when executed will run a command in some of sub-directories, but unfortunately I have never done anything in batch, and I don't know where to start.

我需要编写一个脚本才能在 Windows 中工作,该脚本在执行时会在某些子目录中运行命令,但不幸的是我从来没有做过任何批处理,我不知道从哪里开始。

With the example structure of folders:

使用文件夹的示例结构:

\root
   \one
   \two
   \three
   \four

I want the script to enter the specified folders (e.g. only 'one' and 'four') and then run some command inside every child directories of that folders.

我希望脚本输入指定的文件夹(例如,只有“一个”和“四个”),然后在该文件夹的每个子目录中运行一些命令。

If you could provide any help, maybe some basic tutorial or just names of the commands I will need, I would be very grateful.

如果您能提供任何帮助,也许是一些基本的教程或我需要的命令的名称,我将不胜感激。

回答by Marged

You can tell the batch to iterate directories:

您可以告诉批处理迭代目录:

for /d %i in (C:\temp\*) do ( cd "%i" &  *enter your command here* ) 

Use a percent sign when run directly on the command line, two when run from a batch

直接在命令行上运行时使用百分号,批量运行时使用两个

In a batch this would look something like this:

在批处理中,这看起来像这样:

@echo off
set back=%cd%
for /d %%i in (C:\temp\*) do (
cd "%%i"
echo current directory:
cd
pause
)
cd %back%

Put the commands you need in the lines between (and ). If you replace C:\temp\with %1you can tell the batch to take the value of the directory from the first parameter when you call it. Depending of the amount of directories you then either call the batch for each directory or read them from a list:

将您需要的命令放在(和之间的行中)。如果替换C:\temp\%1,则可以告诉批处理在调用它时从第一个参数中获取目录的值。根据目录的数量,您可以为每个目录调用批处理或从列表中读取它们:

for /f %i in (paths.lst) do call yourbatch %i

The paths.lstwill look like this:

paths.lst会是这样的:

C:\
D:\
Y:\
C:\foo

All of this is written from memory, so you might need to add some quotations marks ;-) Please note that this will only process the first level of directories, that means no child folders of a selected child folder.

所有这些都是从内存中写入的,因此您可能需要添加一些引号 ;-) 请注意,这只会处理第一级目录,这意味着所选子文件夹没有子文件夹。

回答by MichaelS

You should take a look at this. The command you are looking for is FOR /R. Looks something like this:

你应该看看这个。您正在寻找的命令是FOR /R. 看起来像这样:

FOR /R "C:\SomePath\" %%F IN (.) DO (
    some command
)

回答by schlebe

I like answer of Margedthat has been defined as BEST answer (I vote up), but this answer has a big inconvenience.

我喜欢Marged那个被定义为最佳答案的答案(我投票赞成),但是这个答案有很大的不便。

When DOS command between ( and ) contains some errors, the error message returned by DOS is not very explicit.

当 ( 和 ) 之间的 DOS 命令包含一些错误时, DOS 返回的错误信息不是很明确。

For information, this message is

有关信息,此消息是

) was unexpected at this time.

To avoid this situation, I propose the following solution :

为了避免这种情况,我提出以下解决方案:

@echo off

pushd .
for /d %%i in (.\WorkingTime\*.txt) do call :$DoSomething "%%i"
popd

pause
exit /B

::**************************************************
:$DoSomething
::**************************************************

echo current directory: %1
cd %1
echo current directory: %cd%
cd ..

exit /B

The FOR loop call $DoSomething "method" for each directory found passing DIR-NAME has a parameter. Caution: doublequote are passed to %1 parameter in $DoSomething method.

FOR 循环调用 $DoSomething “方法”为找到的每个目录传递 DIR-NAME 都有一个参数。注意:在 $DoSomething 方法中将双引号传递给 %1 参数。

The exit /B command is used to indicate END of method and not END of script.

exit /B 命令用于指示方法的结束而不是脚本的结束。

The result on my PC where I have 2 folders in c:\Temp folder is

在我的 PC 上,我在 c:\Temp 文件夹中有 2 个文件夹的结果是

D:\@Atos\Prestations>call test.bat
current directory: ".\New folder"
current directory: D:\@Atos\Prestations\New folder
current directory: ".\WorkingTime"
current directory: D:\@Atos\Prestations\WorkingTime
Press any key to continue . . .

Caution: in Margeds answer, usage of cd "%%i" is incorrect when folder is relative (folder with . or ..).

注意:在Margeds 答案中,当文件夹是相对的(带有 . 或 .. 的文件夹)时,使用 cd "%%i" 是不正确的。

Why, because the script goto first folder and when it is in first folder it request to goto second folder FROM first folder !

为什么,因为脚本转到第一个文件夹,当它在第一个文件夹中时,它请求从第一个文件夹转到第二个文件夹!

回答by Yehor

If you have a *.cmd or *.bat file with different commands in each folder (needed to be executed from that sub-folder), you can create the cmd file in root folder with following content:

如果你有一个 *.cmd 或 *.bat 文件,每个文件夹中有不同的命令(需要从该子文件夹中执行),你可以在根文件夹中创建具有以下内容的 cmd 文件:

For /R .\ %%a IN (*script_to_execute.cmd) do (
cd "%%~da%%~pa"
%%a)

or

或者

For /R .\ %%a IN (*download.cmd) do (
echo "%%a"
echo "%%~da%%~pa"
cd "%%~da%%~pa"
%%a)

I used it when I needed to download several playlists from youtube. Each folder contained cmd file for downloading specific playlist. Since the download script will download video to the folder it was called from, I needed to change the forking directory first.

我在需要从 youtube 下载多个播放列表时使用它。每个文件夹都包含用于下载特定播放列表的 cmd 文件。由于下载脚本会将视频下载到调用它的文件夹中,因此我需要先更改分叉目录。