Windows 批处理文件循环通过目录来处理文件?

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

Windows Batch File Looping Through Directories to Process Files?

windowsrecursionbatch-file

提问by Tyler

Okay, I am a PHP programmer and unfortunately, for reasons I will not announce for brevity, I need to write/use a batch file that processes some imagery for me.

好吧,我是一名 PHP 程序员,不幸的是,出于简洁起见我不会宣布的原因,我需要编写/使用一个批处理文件来为我处理一些图像。

I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.

我有一个充满嵌套文件夹的文件夹,在这些嵌套文件夹中的每一个文件夹中还有一个包含许多 TIF 图像的文件夹,每个文件夹中的图像数量各不相同。我还有一个批处理文件,我们将其称为 Windows 的 ProcessImages.bat,您可以将这些 TIF 文件“拖放到”上(或者在调用 bat 时显然在命令行列表中指定它们);它基于我拥有的 EXE 创建一个包含我所有图像处理的新文件夹。

The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.

好消息是,因为 bat 文件使用您“拖放”到它的文件夹中的路径,所以我可以选择一个文件夹的所有 TIF 并将其拖放以进行处理......但是当我继续手动执行此操作时我拥有 300 个左右的 TIF 文件夹,我发现它使我的系统陷入困境,令人难以置信,如果我一次只能处理这些文件(无需手动处理),那就太好了。

All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?

所有这一切......有人可以指出我正确的方向(对于Windows bat文件AMATEUR),我可以编写一个Windows bat脚本,我可以从目录内部调用该脚本并让它遍历包含在其中的所有目录该目录...并一次对每组图像运行我的处理批处理文件?

Thanks in advance!

提前致谢!

Tyler

泰勒

回答by Aacini

You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:

您可以在 Batch 中编写递归算法,使您可以精确控制在每个嵌套子目录中执行的操作:

@echo off
call :treeProcess
goto :eof

:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
    cd %%d
    call :treeProcess
    cd ..
)
exit /b

回答by Hyman Allan

Aacini's solution works but you can do it in one line:

Aacini 的解决方案有效,但您可以在一行中完成:

for /R %%f in (*.tif) do echo "%%f"

回答by Tomsim

Hyman's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:

Hyman 的解决方案最适合我,但我需要为网络 UNC 路径(cifs/smb 共享)执行此操作,因此需要稍作修改:

for /R "\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"

The original tip for this method is here

此方法的原始提示在这里

回答by Gras Double

Posting here as it seems to be the most popular question about this case.

在这里发帖,因为它似乎是有关此案例的最受欢迎的问题。

Here is an old gem I have finally managed to find back on the internet: sweep.exe.
It executes the provided command in current directory and all subdirectories, simple as that.

这是我终于在互联网上找到的一个旧宝石:sweep.exe
它在当前目录和所有子目录中执行提供的命令,就这么简单。


Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):


假设您有一些程序可以处理目录中的所有文件(但用例确实比这更广泛):

:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever

So, you want to run this program in current directory and all subdirectories:

所以,你想在当前目录和所有子目录中运行这个程序:

:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd

:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages


Pros:You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.


优点:您不必更改原始程序以使其处理子目录。您可以选择仅在需要时处理子目录。而且这个命令使用起来非常简单和愉快。

Con:Might fail with some commands (containing spaces, quotes, I don't know). See this threadfor example.

缺点:某些命令可能会失败(包含空格、引号,我不知道)。例如,请参阅此线程

回答by Manicalic

I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:

我知道这不是递归(通过枚举的子目录迭代?),但它可能对某些应用程序更有效:

for /F "delims=" %%i in ('dir /ad /on /b /s') do (
    pushd %%i
    dir | find /i "Directory of"
    popd
)

Replace the 3rd line with whatever command you may need.

用您可能需要的任何命令替换第 3 行。

dir /ad- list only directories

dir /ad- 只列出目录

The cool thing is pushddoes not need quotes if spaces in path.

如果路径中有空格,很酷的事情是pushd不需要引号。