Windows蝙蝠:目录中的循环文件 [英] Windows bat: loop files in directories

查看:115
本文介绍了Windows蝙蝠:目录中的循环文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows。我在目录的子目录中有许多 jpg 文件。我不知道目录的架构,这意味着我不知道子目录有多少级别。我需要做的是根据路径处理 jpg 文件。这里是一个例子:


  dir 
| --- sub dir1
| | ----- sub dir11
| | ----- a.jpg
| | ----- b.jpg
| --- sub dir2
| ----- c.jpg
| ----- d.jpg

所以我需要执行下面的命令:


  process.bat C:\dir\sub dir1\sub dir11\a.jpg C:\dir\sub dir1\sub dir11\b.jpg 
process.bat C:\dir\sub dir2\c.jpg C:\dir\sub dir2\d.jpg

我不知道如何编写这样一个bat文件,特别是一个bat处理路径包含空格。我不知道空格是否会造成一些麻烦。















<
$ b $ pre $ @Echo off& SetLocal EnableExtensions EnableDelayedExpansion

对于/ d %% A(C:\ dir \ *)Do(
)设置Files =
在(%%〜fA\ * .jpg)中为/ f %% B做Set Files =!Files!%%〜fB
如果已定义文件echo process.bat %%〜fA\ * .jpg


pause

当我执行脚本时,我得到如下结果:


  process.bat C:\dir\sub dir1\ * .jpg 
process.bat C:\dir\sub dir2 \ * .jpg

看来, * 被视为正常字符。 $ b

解决方案

  @Echo off& SetLocal EnableExtensions EnableDelayedExpansion 

For / d %% A in (C:\ dir \ *)Do(
SetFiles =
For / Fdelims =%% B in('dir / S / B%%〜fA\ * .jpg')设置文件=!文件!%%〜fB
如果定义了文件echo process.bat!


暂停


I'm working on Windows. I have many jpg files in subdirectories of a directory. I don't know exactly the architecture of the directory, meaning that I don't know how many levels the subdirectories have. What I need to do is to process jpg files according to their paths. Here is an example:

dir
 |---sub dir1
 |      |-----sub dir11
 |               |-----a.jpg
 |               |-----b.jpg
 |---sub dir2
        |-----c.jpg
        |-----d.jpg

So I need to execute commands as below:

process.bat C:\dir\sub dir1\sub dir11\a.jpg C:\dir\sub dir1\sub dir11\b.jpg
process.bat C:\dir\sub dir2\c.jpg C:\dir\sub dir2\d.jpg

I don't know how to write such a bat file especially a bat processing paths contains spaces. I don't know if spaces will make some trouble.

I've tried as below but it doesn't work:

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

For /d %%A in (C:\dir\*) Do (
    Set "Files="
    For /f %%B in ("%%~fA\*.jpg") Do Set Files=!Files! "%%~fB"
    If defined Files echo process.bat %%~fA\*.jpg
)

pause

When I execute the script, I get the result as below:

process.bat C:\dir\sub dir1\*.jpg
process.bat C:\dir\sub dir2\*.jpg

It seems that * is regarded as a normal character.

解决方案

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

For /d %%A in (C:\dir\*) Do (
    Set "Files="
    For /F "delims=" %%B in ('dir /S /B "%%~fA\*.jpg"') Do Set Files=!Files! "%%~fB"
    If defined Files echo process.bat !Files!
)

pause

这篇关于Windows蝙蝠:目录中的循环文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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