批处理脚本以在每个子文件夹中执行一些命令 [英] Batch script to execute some commands in each sub-folder

查看:71
本文介绍了批处理脚本以在每个子文件夹中执行一些命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个脚本以在Windows中工作,该脚本在执行时将在某些子目录中运行命令,但是不幸的是,我从未批量执行任何操作,而且我不知道从哪里开始.

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.

以文件夹的示例结构为例:

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.

推荐答案

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

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%

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

Put the commands you need in the lines between ( and ). If you replace C:\temp\ with %1 you 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:

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

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.

这篇关于批处理脚本以在每个子文件夹中执行一些命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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