简体   繁体   English

批处理-仅对具有特定名称的文件夹中的文件执行操作

[英]Batch - Act upon files that are ONLY in folders with specific name

I have the following FOR statement: 我有以下FOR语句:

 FOR /R %%i IN ("*.wmv") DO "7za.exe" -sdel a -mx0 -tzip -pPASSWORDHERE -mem=AES256 -y "%%~dpni.zip" "%%i" 

that when ran at the root folder of any given directory, it will zip all files matching the defined wildcard (wmv files, in this case), producing an encrypted ZIP file, keeping the original name. 当在任何给定目录的根文件夹中运行时,它将压缩与定义的通配符匹配的所有文件(在本例中为wmv文件),生成一个加密的ZIP文件,并保留原始名称。

What I need to do is to tell this script to act ONLY on folders that have a specific name. 我需要做的就是告诉此脚本仅对具有特定名称的文件夹执行操作。 Here's an example: 这是一个例子:

I have a directory structure that resembles this (and will contain wmv files in subdirectories within them): 我有一个类似于此的目录结构(并将在其中的子目录中包含wmv文件):

root
 |-20120211
   |-20
   |-21
 |-20130425
   |-34
   |-21
 |-20140516
   |-10
   |-22
 |-20150728
   |-230
   |-213
 and so on...

And what I need to do is, for example, to only zip all the files that are inside of the last folder, which filename starts with 2015* 我需要做的是例如仅压缩最后一个文件夹中的所有文件,该文件名以2015 *开头

To accomplish this, I've tried concatenating FOR statements, more specifically, something like: 为此,我尝试了串联FOR语句,更具体地说,类似于:

 FOR /D %%P IN (2015*) DO (run my other FOR here)

but the only thing it accomplishes is just to verify if a folder's name within that structure matches "2015*", and if it does... well... it just zips everything, all the files in all folders, not just those that start with "2015". 但是它唯一要做的就是验证该结构中的文件夹名称是否匹配“ 2015 *”,如果匹配,那么...好吧...它只是压缩所有内容,所有文件夹中的所有文件,而不仅仅是压缩那些从“ 2015”开始。

I've searched the web but found no actual example that could help me with this. 我已经在网上搜索过,但是没有找到可以帮助我解决这个问题的实际示例。 If you could aid me in achieving this, I'll be beholden to you =) 如果您能帮助我实现这一目标,我将为您着迷=)

Thanks a million in advance! 预先感谢一百万!

FOR /D %%P IN (2015*) DO PUSHD "%%P"&(FOR /R %%i IN ("*.wmv") DO ECHO %%i)&POPD

should accomplish the task, given the description. 应该完成任务,给出描述。

Filename simply echo ed. 文件名只是echo ed。 Remember that the current directory will be the file-to-be-archived's directory at the time the echo is executed, so replacing the echo with the archive instruction will require the required destination directory be specified 请记住,当前目录将是执行echo显时的要归档文件的目录,因此,用archive指令替换echo将要求指定所需的目标目录。

Assuming that the items 20 , 21 , etc., are folders as well, which contain all the *.wmv files, the following should work: 假设项目2021 ,等等,都是文件夹以及,其中包含所有的*.wmv文件,下面应该工作:

for /D %%D in ("root\2015*") do (
    for /D %%E in ("%%~D\*") do (
        for %%F in ("%%~E\*.wmv") do (
            "7za.exe" -sdel a -mx0 -tzip -pPASSWORDHERE -mem=AES256 -y "%%~dpnF.zip" "%%~F"
        )
    )
)

In case the *.wmv files may also be located in the parent folders like 20150728 , you could nest a for /R loop in a for /D loop, like this: 如果*.wmv文件也可能位于父文件夹(如20150728 ,则可以在for /D循环中嵌套for /R循环,如下所示:

for /D %%D in ("root\2015*") do (
    call :SUB "%%~D"
)
exit /B

:SUB
for /R "%~1" %%F in ("*.wmv") do (
    "7za.exe" -sdel a -mx0 -tzip -pPASSWORDHERE -mem=AES256 -y "%%~dpnF.zip" "%%~F"
)
exit /B

You need a sub-routine here, because for /R does not accept other for variables to be used as the root after /R . 在这里你需要一个子程序,因为for /R不接受其他for变量作为后根/R This is due to the fact that the root is applied in a very early parsing stage where for variables are not yet processed/expanded. 这是由于根是在非常早的解析阶段施加的事实for变量尚未处理/扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM