如何创建一个查看子目录的批处理文件,如果存在特定的子目录,则复制一些东西? [英] How to create a batch file that looks into subdirs and copies some stuff if a specific subdirectory exists?

查看:64
本文介绍了如何创建一个查看子目录的批处理文件,如果存在特定的子目录,则复制一些东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要在几个目录中做这样的事情:



< pre lang =text> copy DIR_A\SUBDIR_B\SUBDIR_C\ * .xyz .. \





有一个很多SUBDIR_B不包含SUBDIR_C - 我不想在那里做任何事情,只在包含SUBDIR_C的SUBDIR_B中。



而不是手动查看DIR_A \ SUBDIR_ *,我想自动化。



我的首选方式是一个简单的.bat文件。有没有人可以给我一个hibt如何实现这个目标?



最好的问候

Dennis

解决方案

我不确定简单,但这里似乎有用。它有嵌套的目录迭代器,可以从起点向下两个级别定位子目录。



顶尖提示:为任何可能有害的命令添加前缀总是一个好主意测试期间的ECHO。



例如,使用 ECHO COPY src dest 查看未实际复制的内容。



 @ ECHO OFF 
ECHO处理从起点开始归档两个目录级别
REM LEVEL_1 \ LEVEL_2 \LEVEL_3 \ *。*

SET LEVEL_1 = c:\dir1 \dir2
SET FILESPEC = * .txt

PUSHD%LEVEL_1%
ECHO。
ECHO起始点是%CD%
ECHO搜索匹配%FILESPEC%
ECHO的文件。

REM枚举LEVEL_2目录
FOR / D %% A IN(%LEVEL_1%\ *)DO(
REM枚举LEVEL_3目录
FOR / D %% B IN(%% A \ *)DO(
PUSHD %% B

REM显示目录,如果它包含匹配的文件
如果存在%FILESPEC%(
ECHO。
ECHO LEVEL_3 %% B

REM枚举LEVEL_3子目录中的匹配文件
FOR %% F IN(%FILESPEC%)DO(
REM~nx修饰符只能得到名称和扩展名
REM~dp修饰符只能得到驱动器和路径
ECHO COPY%% ~nxF%% ~dpF ..


POPD


POPD
PAUSE





Alan


Hello all,

I need to do something like this in several directories:

copy DIR_A\SUBDIR_B\SUBDIR_C\*.xyz ..\



There are a lot of SUBDIR_B that do NOT contain SUBDIR_C - I do not want to do anything in there, only in SUBDIR_B that contain SUBDIR_C.

Instead of manually looking into DIR_A\SUBDIR_*, I want to automate that.

My preferred way would be a simple .bat file. Does anybody can give me a hibt how to accomplish this?

Best Regards
Dennis

解决方案

I''m not sure about simple but here is something that appears to work. It has nested directory iterators to locate subdirectories two levels down from the start point.

Top tip: It''s always a good idea to prefix any potentially harmful commands with ECHO during testing.

e.g Use ECHO COPY src dest to see what would be copied without actually doing it.

@ECHO OFF
ECHO Process files two directory levels down from the start point
REM LEVEL_1\LEVEL_2\LEVEL_3\*.*

SET LEVEL_1=c:\dir1\dir2
SET FILESPEC=*.txt

PUSHD %LEVEL_1%
ECHO.
ECHO Start point is %CD%
ECHO   Searching for files matching %FILESPEC%
ECHO.

REM enumerate LEVEL_2 directories
FOR /D %%A IN (%LEVEL_1%\*) DO (
  REM enumerate LEVEL_3 directories
  FOR /D  %%B IN (%%A\*) DO (
      PUSHD %%B
      
      REM Show directory if it contains matching files
      IF EXIST %FILESPEC% (
        ECHO.
        ECHO LEVEL_3 %%B

        REM enumerate matching files in the LEVEL_3 subdirectory
        FOR %%F IN (%FILESPEC%) DO (
          REM ~nx modifier to get name and extension only
          REM ~dp modifier to get drive and path only
          ECHO   COPY "%%~nxF" "%%~dpF.."
        )
      )
      POPD
  )
)
POPD
PAUSE



Alan.


这篇关于如何创建一个查看子目录的批处理文件,如果存在特定的子目录,则复制一些东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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